2011-11-02 22:00:25 +00:00
|
|
|
/**
|
|
|
|
* Creates an annotation renderer object.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @constructor
|
|
|
|
* @property annotations {Object} List of annotations to be applied
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.AnnotationSerializer = function() {
|
2011-11-02 22:00:25 +00:00
|
|
|
this.annotations = {};
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Static Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Adds a set of annotations to be inserted around a range of text.
|
|
|
|
*
|
|
|
|
* Insertions for the same range will be nested in order of declaration.
|
|
|
|
* @example
|
2012-02-06 23:50:56 +00:00
|
|
|
* stack = new ve.dm.AnnotationSerializer();
|
|
|
|
* stack.add( new ve.Range( 1, 2 ), '[', ']' );
|
|
|
|
* stack.add( new ve.Range( 1, 2 ), '{', '}' );
|
2011-11-02 22:00:25 +00:00
|
|
|
* // Outputs: "a[{b}]c"
|
|
|
|
* console.log( stack.render( 'abc' ) );
|
|
|
|
*
|
2011-11-03 18:15:24 +00:00
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @param {ve.Range} range Range to insert text around
|
2011-11-03 18:15:24 +00:00
|
|
|
* @param {String} pre Text to insert before range
|
|
|
|
* @param {String} post Text to insert after range
|
2011-11-02 22:00:25 +00:00
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.AnnotationSerializer.prototype.add = function( range, pre, post ) {
|
2011-11-03 18:15:24 +00:00
|
|
|
// Normalize the range if it can be normalized
|
|
|
|
if ( typeof range.normalize === 'function' ) {
|
|
|
|
range.normalize();
|
|
|
|
}
|
2011-11-02 22:00:25 +00:00
|
|
|
if ( !( range.start in this.annotations ) ) {
|
|
|
|
this.annotations[range.start] = [pre];
|
|
|
|
} else {
|
|
|
|
this.annotations[range.start].push( pre );
|
|
|
|
}
|
|
|
|
if ( !( range.end in this.annotations ) ) {
|
|
|
|
this.annotations[range.end] = [post];
|
|
|
|
} else {
|
|
|
|
this.annotations[range.end].unshift( post );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-03 18:15:24 +00:00
|
|
|
/**
|
|
|
|
* Adds a set of HTML tags to be inserted around a range of text.
|
|
|
|
*
|
|
|
|
* @method
|
2012-02-06 23:50:56 +00:00
|
|
|
* @param {ve.Range} range Range to insert text around
|
2011-11-03 18:15:24 +00:00
|
|
|
* @param {String} type Tag name
|
|
|
|
* @param {Object} [attributes] List of HTML attributes
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.AnnotationSerializer.prototype.addTags = function( range, type, attributes ) {
|
|
|
|
this.add( range, ve.Html.makeOpeningTag( type, attributes ), ve.Html.makeClosingTag( type ) );
|
2011-11-03 18:15:24 +00:00
|
|
|
};
|
|
|
|
|
2011-11-02 22:00:25 +00:00
|
|
|
/**
|
|
|
|
* Renders annotations into text.
|
|
|
|
*
|
2011-11-03 18:15:24 +00:00
|
|
|
* @method
|
|
|
|
* @param {String} text Text to apply annotations to
|
2011-11-02 22:00:25 +00:00
|
|
|
* @returns {String} Wrapped text
|
|
|
|
*/
|
2012-02-06 23:50:56 +00:00
|
|
|
ve.dm.AnnotationSerializer.prototype.render = function( text ) {
|
2011-11-02 22:00:25 +00:00
|
|
|
var out = '';
|
|
|
|
for ( var i = 0, length = text.length; i <= length; i++ ) {
|
|
|
|
if ( i in this.annotations ) {
|
|
|
|
out += this.annotations[i].join( '' );
|
|
|
|
}
|
|
|
|
if ( i < length ) {
|
|
|
|
out += text[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return out;
|
|
|
|
};
|