2012-05-01 00:36:22 +00:00
|
|
|
/**
|
|
|
|
* ContentEditable node for text.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-01 00:36:22 +00:00
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @extends {ve.ce.LeafNode}
|
|
|
|
* @param model {ve.dm.TextNode} Model to observe
|
|
|
|
*/
|
|
|
|
ve.ce.TextNode = function( model ) {
|
|
|
|
// Inheritance
|
2012-05-05 00:50:54 +00:00
|
|
|
ve.ce.LeafNode.call( this, 'text', model, $( document.createTextNode('') ) );
|
2012-05-04 00:19:01 +00:00
|
|
|
|
|
|
|
// Events
|
|
|
|
this.model.addListenerMethod( this, 'update', 'onUpdate' );
|
|
|
|
|
|
|
|
// Intialization
|
2012-06-14 04:46:29 +00:00
|
|
|
this.onUpdate( true );
|
2012-05-01 00:36:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Static Members */
|
|
|
|
|
|
|
|
/**
|
2012-05-02 18:29:44 +00:00
|
|
|
* Node rules.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-01 00:36:22 +00:00
|
|
|
* @see ve.ce.NodeFactory
|
2012-05-02 18:29:44 +00:00
|
|
|
* @static
|
|
|
|
* @member
|
2012-05-01 00:36:22 +00:00
|
|
|
*/
|
|
|
|
ve.ce.TextNode.rules = {
|
|
|
|
'canBeSplit': true
|
|
|
|
};
|
|
|
|
|
2012-05-03 05:28:57 +00:00
|
|
|
/**
|
|
|
|
* Mapping of character and HTML entities or renderings.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-03 05:28:57 +00:00
|
|
|
* @static
|
|
|
|
* @member
|
|
|
|
*/
|
|
|
|
ve.ce.TextNode.htmlCharacters = {
|
|
|
|
'&': '&',
|
|
|
|
'<': '<',
|
|
|
|
'>': '>',
|
|
|
|
'\'': ''',
|
|
|
|
'"': '"',
|
2012-06-12 17:40:44 +00:00
|
|
|
'\n': '↵',
|
|
|
|
'\t': '➞'
|
2012-05-03 05:28:57 +00:00
|
|
|
};
|
|
|
|
|
2012-05-01 02:38:42 +00:00
|
|
|
/**
|
|
|
|
* List of annotation rendering implementations.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-01 02:38:42 +00:00
|
|
|
* Each supported annotation renderer must have an open and close property, each either a string or
|
|
|
|
* a function which accepts a data argument.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-01 02:38:42 +00:00
|
|
|
* @static
|
|
|
|
* @member
|
|
|
|
*/
|
|
|
|
ve.ce.TextNode.annotationRenderers = {
|
|
|
|
'textStyle/bold': {
|
|
|
|
'open': '<b>',
|
|
|
|
'close': '</b>'
|
|
|
|
},
|
|
|
|
'textStyle/italic': {
|
|
|
|
'open': '<i>',
|
|
|
|
'close': '</i>'
|
|
|
|
},
|
2012-05-03 05:28:57 +00:00
|
|
|
'textStyle/underline': {
|
|
|
|
'open': '<u>',
|
|
|
|
'close': '</u>'
|
|
|
|
},
|
2012-05-01 02:38:42 +00:00
|
|
|
'textStyle/strong': {
|
2012-06-08 23:20:28 +00:00
|
|
|
'open': '<strong>',
|
|
|
|
'close': '</strong>'
|
2012-05-01 02:38:42 +00:00
|
|
|
},
|
|
|
|
'textStyle/emphasize': {
|
2012-06-08 23:20:28 +00:00
|
|
|
'open': '<em>',
|
|
|
|
'close': '<em>'
|
2012-05-01 02:38:42 +00:00
|
|
|
},
|
|
|
|
'textStyle/big': {
|
2012-06-08 23:20:28 +00:00
|
|
|
'open': '<big>',
|
|
|
|
'close': '</big>'
|
2012-05-01 02:38:42 +00:00
|
|
|
},
|
|
|
|
'textStyle/small': {
|
2012-06-08 23:20:28 +00:00
|
|
|
'open': '<small>',
|
|
|
|
'close': '</small>'
|
2012-05-01 02:38:42 +00:00
|
|
|
},
|
|
|
|
'textStyle/superScript': {
|
2012-06-08 23:20:28 +00:00
|
|
|
'open': '<sup>',
|
|
|
|
'close': '</sup>'
|
2012-05-01 02:38:42 +00:00
|
|
|
},
|
|
|
|
'textStyle/subScript': {
|
2012-06-08 23:20:28 +00:00
|
|
|
'open': '<sub>',
|
|
|
|
'close': '</sub>'
|
2012-05-01 02:38:42 +00:00
|
|
|
},
|
2012-06-11 07:36:20 +00:00
|
|
|
'link/extLink': {
|
2012-05-01 02:38:42 +00:00
|
|
|
'open': function( data ) {
|
2012-06-08 23:20:28 +00:00
|
|
|
return '<a href="' + data.href + '">';
|
2012-05-01 02:38:42 +00:00
|
|
|
},
|
2012-06-08 23:20:28 +00:00
|
|
|
'close': '</a>'
|
2012-05-01 02:38:42 +00:00
|
|
|
},
|
2012-06-11 07:36:20 +00:00
|
|
|
'link/wikiLink': {
|
2012-05-01 02:38:42 +00:00
|
|
|
'open': function( data ) {
|
2012-06-08 23:20:28 +00:00
|
|
|
return '<a href="' + data.href + '">';
|
2012-05-01 02:38:42 +00:00
|
|
|
},
|
2012-06-08 23:20:28 +00:00
|
|
|
'close': '</a>'
|
2012-06-08 22:15:43 +00:00
|
|
|
},
|
|
|
|
'link/unknown': {
|
|
|
|
'open': function( data ) {
|
2012-06-08 23:20:28 +00:00
|
|
|
return '<a href="#">';
|
2012-06-08 22:15:43 +00:00
|
|
|
},
|
2012-06-08 23:20:28 +00:00
|
|
|
'close': '</a>'
|
2012-05-01 02:38:42 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-05-01 00:36:22 +00:00
|
|
|
/* Methods */
|
|
|
|
|
2012-05-01 02:38:42 +00:00
|
|
|
/**
|
2012-05-04 00:19:01 +00:00
|
|
|
* Responds to model update events.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-04 00:19:01 +00:00
|
|
|
* If the source changed since last update the image's src attribute will be updated accordingly.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-01 02:38:42 +00:00
|
|
|
* @method
|
|
|
|
*/
|
2012-06-14 04:46:29 +00:00
|
|
|
ve.ce.TextNode.prototype.onUpdate = function( force ) {
|
|
|
|
if ( force === true || this.getSurface().render === true ) {
|
|
|
|
var $new = $( $( '<span>' + this.getHtml() + '</span>' ).contents() );
|
|
|
|
if ( $new.length === 0 ) {
|
|
|
|
$new = $new.add( document.createTextNode( '' ) );
|
|
|
|
}
|
|
|
|
this.$.replaceWith( $new );
|
|
|
|
this.$ = $new;
|
|
|
|
if ( this.parent ) {
|
|
|
|
this.parent.clean();
|
2012-06-14 22:00:29 +00:00
|
|
|
if ( ve.debug ) {
|
|
|
|
this.parent.$.css('background-color', '#F6F6F6');
|
|
|
|
setTimeout( ve.proxy( function() {
|
|
|
|
this.parent.$.css('background-color', 'transparent');
|
|
|
|
}, this ), 350 );
|
|
|
|
}
|
2012-06-14 04:46:29 +00:00
|
|
|
}
|
2012-06-07 18:33:21 +00:00
|
|
|
}
|
2012-05-01 02:38:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2012-05-02 21:21:41 +00:00
|
|
|
* Gets an HTML rendering of data within content model.
|
2012-05-14 22:05:09 +00:00
|
|
|
*
|
2012-05-01 02:38:42 +00:00
|
|
|
* @method
|
|
|
|
* @param {String} Rendered HTML of data within content model
|
|
|
|
*/
|
2012-05-02 21:21:41 +00:00
|
|
|
ve.ce.TextNode.prototype.getHtml = function() {
|
2012-05-03 02:29:03 +00:00
|
|
|
var data = this.model.getDocument().getDataFromNode( this.model ),
|
|
|
|
htmlChars = ve.ce.TextNode.htmlCharacters,
|
2012-05-03 05:28:57 +00:00
|
|
|
renderers = ve.ce.TextNode.annotationRenderers,
|
2012-05-03 02:29:03 +00:00
|
|
|
out = '',
|
2012-06-01 00:18:28 +00:00
|
|
|
i,
|
|
|
|
j,
|
|
|
|
hash,
|
2012-05-03 02:29:03 +00:00
|
|
|
left = '',
|
|
|
|
right,
|
2012-06-01 00:18:28 +00:00
|
|
|
open,
|
|
|
|
close,
|
|
|
|
index,
|
2012-05-03 02:29:03 +00:00
|
|
|
leftPlain,
|
2012-05-03 05:28:57 +00:00
|
|
|
rightPlain,
|
|
|
|
hashStack = [],
|
|
|
|
annotationStack = {};
|
2012-05-03 02:29:03 +00:00
|
|
|
|
2012-06-14 04:46:29 +00:00
|
|
|
// If first or last char is a space it should be replaced with to be rendered correctly
|
|
|
|
// TODO: Special handling for spaces in <pre> may be required
|
|
|
|
if ( ve.isArray( data[0] ) ) {
|
|
|
|
if ( data[0][0] === ' ' ) {
|
|
|
|
data[0][0] = ' ';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( data[0] === ' ' ) {
|
|
|
|
data[0] = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( ve.isArray( data[data.length - 1] ) ) {
|
|
|
|
if ( data[data.length - 1][0] === ' ' ) {
|
|
|
|
data[data.length - 1][0] = ' ';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ( data[data.length - 1] === ' ' ) {
|
|
|
|
data[data.length - 1] = ' ';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-03 05:28:57 +00:00
|
|
|
var openAnnotations = function( annotations ) {
|
|
|
|
var out = '',
|
|
|
|
annotation;
|
2012-05-03 02:29:03 +00:00
|
|
|
|
|
|
|
for ( var hash in annotations ) {
|
2012-05-03 05:28:57 +00:00
|
|
|
annotation = annotations[hash];
|
2012-06-01 00:18:28 +00:00
|
|
|
out += typeof renderers[annotation.type].open === 'function' ?
|
|
|
|
renderers[annotation.type].open( annotation.data ) :
|
|
|
|
renderers[annotation.type].open;
|
2012-05-03 05:28:57 +00:00
|
|
|
hashStack.push( hash );
|
|
|
|
annotationStack[hash] = annotation;
|
2012-05-03 02:29:03 +00:00
|
|
|
}
|
2012-05-03 05:28:57 +00:00
|
|
|
return out;
|
2012-05-03 02:29:03 +00:00
|
|
|
};
|
|
|
|
|
2012-05-03 05:28:57 +00:00
|
|
|
var closeAnnotations = function( annotations ) {
|
|
|
|
var out = '',
|
|
|
|
annotation;
|
|
|
|
|
2012-05-03 02:29:03 +00:00
|
|
|
for ( var hash in annotations ) {
|
2012-05-03 05:28:57 +00:00
|
|
|
annotation = annotations[hash];
|
2012-06-01 00:18:28 +00:00
|
|
|
out += typeof renderers[annotation.type].close === 'function' ?
|
|
|
|
renderers[annotation.type].close( annotation.data ) :
|
|
|
|
renderers[annotation.type].close;
|
2012-05-03 17:11:39 +00:00
|
|
|
|
|
|
|
// new version
|
|
|
|
hashStack.pop();
|
|
|
|
delete annotationStack[hash];
|
|
|
|
|
|
|
|
// old version
|
|
|
|
/*
|
2012-05-03 05:28:57 +00:00
|
|
|
var depth = hashStack.indexOf( hash );
|
|
|
|
if ( depth !== -1 ) {
|
2012-06-18 20:30:14 +00:00
|
|
|
ve.log(depth, hashStack.length);
|
2012-05-03 05:28:57 +00:00
|
|
|
hashStack.splice( depth, 1 );
|
|
|
|
delete annotationStack[hash];
|
2012-05-03 02:29:03 +00:00
|
|
|
}
|
2012-05-03 17:11:39 +00:00
|
|
|
*/
|
2012-05-03 02:29:03 +00:00
|
|
|
}
|
2012-05-03 05:28:57 +00:00
|
|
|
return out;
|
2012-05-03 02:29:03 +00:00
|
|
|
};
|
|
|
|
|
2012-06-01 00:18:28 +00:00
|
|
|
for ( i = 0; i < data.length; i++ ) {
|
2012-05-03 02:29:03 +00:00
|
|
|
right = data[i];
|
|
|
|
leftPlain = typeof left === 'string';
|
|
|
|
rightPlain = typeof right === 'string';
|
|
|
|
|
|
|
|
if ( !leftPlain && rightPlain ) {
|
2012-05-03 05:28:57 +00:00
|
|
|
// [formatted][plain]
|
2012-06-01 00:18:28 +00:00
|
|
|
close = {};
|
|
|
|
for ( j = hashStack.length - 1; j >= 0; j-- ) {
|
2012-05-03 05:28:57 +00:00
|
|
|
close[hashStack[j]] = annotationStack[hashStack[j]];
|
2012-05-03 02:29:03 +00:00
|
|
|
}
|
2012-06-01 00:18:28 +00:00
|
|
|
out += closeAnnotations( close );
|
2012-05-03 02:29:03 +00:00
|
|
|
} else if ( leftPlain && !rightPlain ) {
|
2012-05-03 05:28:57 +00:00
|
|
|
// [plain][formatted]
|
|
|
|
out += openAnnotations( right[1] );
|
2012-05-03 02:29:03 +00:00
|
|
|
} else if ( !leftPlain && !rightPlain ) {
|
2012-05-03 05:28:57 +00:00
|
|
|
// [formatted][formatted]
|
2012-05-03 02:29:03 +00:00
|
|
|
|
2012-05-03 18:11:46 +00:00
|
|
|
// setting index to undefined is is necessary to it does not use value from
|
|
|
|
// the previous iteration
|
2012-06-01 00:18:28 +00:00
|
|
|
open = {},
|
|
|
|
index = undefined;
|
2012-05-03 02:29:03 +00:00
|
|
|
|
2012-06-01 00:18:28 +00:00
|
|
|
for ( hash in left[1] ) {
|
2012-05-03 05:28:57 +00:00
|
|
|
if ( !( hash in right[1] ) ) {
|
2012-06-01 00:18:28 +00:00
|
|
|
index = ( index === undefined ) ?
|
|
|
|
hashStack.indexOf( hash ) :
|
|
|
|
Math.min( index, hashStack.indexOf( hash ) );
|
2012-05-03 02:29:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-03 18:11:46 +00:00
|
|
|
if ( index !== undefined ) {
|
2012-06-01 00:18:28 +00:00
|
|
|
close = {};
|
|
|
|
for ( j = hashStack.length - 1; j >= index; j-- ) {
|
2012-05-03 05:28:57 +00:00
|
|
|
close[hashStack[j]] = annotationStack[hashStack[j]];
|
2012-05-03 02:29:03 +00:00
|
|
|
}
|
|
|
|
|
2012-06-01 00:18:28 +00:00
|
|
|
for ( j = index; j < hashStack.length; j++ ) {
|
2012-05-03 05:32:34 +00:00
|
|
|
if ( hashStack[j] in right[1] && hashStack[j] in left[1] ) {
|
2012-05-03 05:28:57 +00:00
|
|
|
open[hashStack[j]] = annotationStack[hashStack[j]];
|
2012-05-03 02:29:03 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-03 05:28:57 +00:00
|
|
|
out += closeAnnotations( close );
|
2012-05-03 02:29:03 +00:00
|
|
|
}
|
|
|
|
|
2012-06-01 00:18:28 +00:00
|
|
|
for ( hash in right[1] ) {
|
2012-05-03 05:28:57 +00:00
|
|
|
if ( !( hash in left[1] ) ) {
|
|
|
|
open[hash] = right[1][hash];
|
2012-05-03 02:29:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-03 05:28:57 +00:00
|
|
|
out += openAnnotations( open );
|
2012-05-03 02:29:03 +00:00
|
|
|
}
|
|
|
|
|
2012-05-17 18:23:56 +00:00
|
|
|
var chr = rightPlain ? right : right[0];
|
2012-05-03 02:29:03 +00:00
|
|
|
out += chr in htmlChars ? htmlChars[chr] : chr;
|
|
|
|
left = right;
|
|
|
|
}
|
|
|
|
|
2012-06-01 00:18:28 +00:00
|
|
|
close = {};
|
|
|
|
for ( j = hashStack.length - 1; j >= 0; j-- ) {
|
2012-05-03 05:28:57 +00:00
|
|
|
close[hashStack[j]] = annotationStack[hashStack[j]];
|
2012-05-01 02:38:42 +00:00
|
|
|
}
|
2012-06-01 00:18:28 +00:00
|
|
|
out += closeAnnotations( close );
|
2012-05-03 05:28:57 +00:00
|
|
|
|
2012-06-06 23:29:02 +00:00
|
|
|
// Mulitple spaces should alternate spaces and
|
2012-06-14 04:46:29 +00:00
|
|
|
// TODO: Special handling for spaces in <pre> may be required
|
2012-06-06 23:29:02 +00:00
|
|
|
out = out.replace(/ /g, ' ');
|
|
|
|
|
2012-05-01 02:38:42 +00:00
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
2012-06-14 04:46:29 +00:00
|
|
|
ve.ce.TextNode.prototype.getSurface = function() {
|
|
|
|
var view = this;
|
|
|
|
while( !view.surface ) {
|
|
|
|
view = view.parent;
|
|
|
|
}
|
|
|
|
return view.surface;
|
|
|
|
};
|
|
|
|
|
2012-05-01 00:36:22 +00:00
|
|
|
/* Registration */
|
|
|
|
|
2012-05-31 22:20:58 +00:00
|
|
|
ve.ce.nodeFactory.register( 'text', ve.ce.TextNode );
|
2012-05-01 00:36:22 +00:00
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2012-06-01 00:18:28 +00:00
|
|
|
ve.extendClass( ve.ce.TextNode, ve.ce.LeafNode );
|