r99277 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r99276‎ | r99277 | r99278 >
Date:22:21, 7 October 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Got es.DocumentModel.prepareContentAnnotation working (and not infinitely looping) and added a test for it
Modified paths:
  • /trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js (modified) (history)
  • /trunk/parsers/wikidom/tests/hype/es.DocumentModel.test.js (modified) (history)

Diff [purge]

Index: trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js
@@ -264,6 +264,9 @@
265265 };
266266
267267 es.DocumentModel.getIndexOfAnnotation = function( character, annotation ) {
 268+ if ( annotation === undefined || annotation.type === undefined ) {
 269+ throw 'Invalid annotation error. Can not find non-annotation data in character.'
 270+ }
268271 if ( $.isArray( character ) ) {
269272 // Find the index of a comparable annotation (checking for same value, not reference)
270273 for ( var i = 1; i < character.length; i++ ) {
@@ -608,7 +611,9 @@
609612 es.DocumentModel.prototype.prepareContentAnnotation = function( range, method, annotation ) {
610613 var tx = new es.Transaction();
611614 range.normalize();
612 -
 615+ if ( annotation.hash === undefined ) {
 616+ annotation.hash = es.DocumentModel.getAnnotationHash( annotation );
 617+ }
613618 var i = range.start,
614619 span = i,
615620 on = this.data[i].type !== undefined;
@@ -621,10 +626,13 @@
622627 on = false;
623628 }
624629 } else {
625 - var covered = es.DocumentModel.getIndexOfAnnotation( this.data[i] ) !== -1;
 630+ var covered = es.DocumentModel.getIndexOfAnnotation( this.data[i], annotation ) !== -1;
626631 if ( covered && method === 'set' || !covered && method === 'clear' ) {
627632 // Don't set/clear annotations on content that's already set/cleared
628633 if ( on ) {
 634+ if ( span ) {
 635+ tx.pushRetain( span );
 636+ }
629637 tx.pushStopAnnotating( method, annotation );
630638 span = 0;
631639 on = false;
@@ -642,7 +650,14 @@
643651 }
644652 }
645653 span++;
 654+ i++;
646655 }
 656+ if ( on ) {
 657+ if ( span ) {
 658+ tx.pushRetain( span );
 659+ }
 660+ tx.pushStopAnnotating( method, annotation );
 661+ }
647662 if ( range.end < this.data.length ) {
648663 tx.pushRetain( this.data.length - range.end );
649664 }
Index: trunk/parsers/wikidom/tests/hype/es.DocumentModel.test.js
@@ -201,7 +201,7 @@
202202 new es.ParagraphModel( data[25], 1 )
203203 ];
204204
205 -test( 'es.DocumentModel', 15, function() {
 205+test( 'es.DocumentModel', 16, function() {
206206 var documentModel = es.DocumentModel.newFromPlainObject( obj );
207207
208208 deepEqual( documentModel.getData(), data, 'Flattening plain objects results in correct data' );
@@ -303,5 +303,41 @@
304304 );
305305 }
306306
 307+ deepEqual(
 308+ documentModel.prepareContentAnnotation( new es.Range( 1, 4 ), 'set', { 'type': 'bold' } ),
 309+ [
 310+ { 'type': 'retain', 'length': 1 },
 311+ {
 312+ 'type': 'annotate',
 313+ 'method': 'set',
 314+ 'bias': 'start',
 315+ 'annotation': { 'type': 'bold', 'hash': '#bold' }
 316+ },
 317+ { 'type': 'retain', 'length': 1 },
 318+ {
 319+ 'type': 'annotate',
 320+ 'method': 'set',
 321+ 'bias': 'stop',
 322+ 'annotation': { 'type': 'bold', 'hash': '#bold' }
 323+ },
 324+ { 'type': 'retain', 'length': 1 },
 325+ {
 326+ 'type': 'annotate',
 327+ 'method': 'set',
 328+ 'bias': 'start',
 329+ 'annotation': { 'type': 'bold', 'hash': '#bold' }
 330+ },
 331+ { 'type': 'retain', 'length': 1 },
 332+ {
 333+ 'type': 'annotate',
 334+ 'method': 'set',
 335+ 'bias': 'stop',
 336+ 'annotation': { 'type': 'bold', 'hash': '#bold' }
 337+ },
 338+ { 'type': 'retain', 'length': 24 }
 339+ ],
 340+ 'prepareContentAnnotation skips over content that is already set or cleared'
 341+ );
 342+
307343 } );
308344

Status & tagging log