r101716 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r101715‎ | r101716 | r101717 >
Date:22:00, 2 November 2011
Author:tparscal
Status:ok
Tags:
Comment:
Copying serializer code to VisualEditor extension
Modified paths:
  • /trunk/extensions/VisualEditor/modules/es/es.AnnotationSerializer.js (added) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/modules/es/es.AnnotationSerializer.js
@@ -0,0 +1,60 @@
 2+/**
 3+ * Creates an annotation renderer object.
 4+ *
 5+ * @class
 6+ * @constructor
 7+ * @property annotations {Object} List of annotations to be applied
 8+ */
 9+es.AnnotationSerializer = function() {
 10+ this.annotations = {};
 11+};
 12+
 13+/* Static Methods */
 14+
 15+/**
 16+ * Adds a set of annotations to be inserted around a range of text.
 17+ *
 18+ * Insertions for the same range will be nested in order of declaration.
 19+ * @example
 20+ * stack = new es.AnnotationSerializer();
 21+ * stack.add( new es.Range( 1, 2 ), '[', ']' );
 22+ * stack.add( new es.Range( 1, 2 ), '{', '}' );
 23+ * // Outputs: "a[{b}]c"
 24+ * console.log( stack.render( 'abc' ) );
 25+ *
 26+ * @param range {es.Range} Range to insert text around
 27+ * @param pre {String} Text to insert before range
 28+ * @param post {String} Text to insert after range
 29+ */
 30+es.AnnotationSerializer.prototype.add = function( range, pre, post ) {
 31+ // TODO: Once we are using Range objects, we should do a range.normalize(); here
 32+ if ( !( range.start in this.annotations ) ) {
 33+ this.annotations[range.start] = [pre];
 34+ } else {
 35+ this.annotations[range.start].push( pre );
 36+ }
 37+ if ( !( range.end in this.annotations ) ) {
 38+ this.annotations[range.end] = [post];
 39+ } else {
 40+ this.annotations[range.end].unshift( post );
 41+ }
 42+};
 43+
 44+/**
 45+ * Renders annotations into text.
 46+ *
 47+ * @param text {String} Text to apply annotations to
 48+ * @returns {String} Wrapped text
 49+ */
 50+es.AnnotationSerializer.prototype.render = function( text ) {
 51+ var out = '';
 52+ for ( var i = 0, length = text.length; i <= length; i++ ) {
 53+ if ( i in this.annotations ) {
 54+ out += this.annotations[i].join( '' );
 55+ }
 56+ if ( i < length ) {
 57+ out += text[i];
 58+ }
 59+ }
 60+ return out;
 61+};

Status & tagging log