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 ) {
|
2012-06-21 06:20:42 +00:00
|
|
|
if ( !force && !this.root.getSurface ) {
|
|
|
|
throw 'Can not update a text node that is not attached to a document';
|
|
|
|
}
|
|
|
|
if ( force === true || this.root.getSurface().render === true ) {
|
2012-06-14 04:46:29 +00:00
|
|
|
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-21 06:17:48 +00:00
|
|
|
character,
|
|
|
|
nextCharacter,
|
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-20 22:31:50 +00:00
|
|
|
var replaceWithNonBreakingSpace = function( index, data ) {
|
|
|
|
if ( ve.isArray( data[index] ) ) {
|
|
|
|
data[index][0] = ' ';
|
|
|
|
} else {
|
|
|
|
data[index] = ' ';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if ( data.length > 0 ) {
|
2012-06-21 06:17:48 +00:00
|
|
|
character = data[0];
|
|
|
|
if ( ve.isArray( character ) ? character[0] === ' ' : character === ' ' ) {
|
2012-06-20 22:31:50 +00:00
|
|
|
replaceWithNonBreakingSpace( 0, data );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( data.length > 1 ) {
|
2012-06-21 06:17:48 +00:00
|
|
|
character = data[data.length - 1];
|
|
|
|
if ( ve.isArray( character ) ? character[0] === ' ' : character === ' ' ) {
|
2012-06-20 22:31:50 +00:00
|
|
|
replaceWithNonBreakingSpace( data.length - 1, data );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( data.length > 2 ) {
|
2012-06-21 06:17:48 +00:00
|
|
|
for ( i = 1; i < data.length - 1; i++ ) {
|
|
|
|
character = data[i];
|
|
|
|
nextCharacter = data[i + 1];
|
|
|
|
if (
|
|
|
|
( ve.isArray( character ) ? character[0] === ' ' : character === ' ' ) &&
|
|
|
|
( ve.isArray( nextCharacter ) ? nextCharacter[0] === ' ' : nextCharacter === ' ' )
|
|
|
|
) {
|
2012-06-20 22:31:50 +00:00
|
|
|
replaceWithNonBreakingSpace( i + 1, data );
|
2012-06-19 05:24:18 +00:00
|
|
|
i++;
|
|
|
|
}
|
2012-06-14 04:46:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
hashStack.pop();
|
|
|
|
delete annotationStack[hash];
|
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-01 02:38:42 +00:00
|
|
|
return out;
|
|
|
|
};
|
|
|
|
|
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 );
|