r100739 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r100738‎ | r100739 | r100740 >
Date:20:31, 25 October 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Fixed logic for set/clear and invert in annotation processing
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/tests/hype/es.DocumentModel.test.js
@@ -499,12 +499,44 @@
500500 );
501501 } );
502502
503 -test( 'es.DocumentModel.commit, es.DocumentModel.rollback', 4, function() {
 503+test( 'es.DocumentModel.commit, es.DocumentModel.rollback', 6, function() {
504504 var documentModel = es.DocumentModel.newFromPlainObject( obj );
505505
 506+ var contentAnnotation = documentModel.prepareContentAnnotation(
 507+ new es.Range( 1, 4 ), 'set', { 'type': 'bold' }
 508+ );
 509+
 510+ // Test 1
 511+ documentModel.commit( contentAnnotation );
 512+ deepEqual(
 513+ documentModel.getData( new es.Range( 0, 5 ) ),
 514+ [
 515+ { 'type': 'paragraph' },
 516+ ['a', { 'type': 'bold', 'hash': '#bold' }],
 517+ ['b', { 'type': 'bold', 'hash': '#bold' }],
 518+ ['c', { 'type': 'italic', 'hash': '#italic' }, { 'type': 'bold', 'hash': '#bold' }],
 519+ { 'type': '/paragraph' }
 520+ ],
 521+ 'commit applies a content annotation transaction to the content'
 522+ );
 523+
 524+ // Test 2
 525+ documentModel.rollback( contentAnnotation );
 526+ deepEqual(
 527+ documentModel.getData( new es.Range( 0, 5 ) ),
 528+ [
 529+ { 'type': 'paragraph' },
 530+ 'a',
 531+ ['b', { 'type': 'bold', 'hash': '#bold' }],
 532+ ['c', { 'type': 'italic', 'hash': '#italic' }],
 533+ { 'type': '/paragraph' }
 534+ ],
 535+ 'rollback reverses the effect of a content annotation transaction on the content'
 536+ );
 537+
506538 var insertion = documentModel.prepareInsertion( 4, ['d'] );
507539
508 - // Test 1
 540+ // Test 3
509541 documentModel.commit( insertion );
510542 deepEqual(
511543 documentModel.getData( new es.Range( 0, 6 ) ),
@@ -519,7 +551,7 @@
520552 'commit applies an insertion transaction to the content'
521553 );
522554
523 - // Test 2
 555+ // Test 4
524556 documentModel.rollback( insertion );
525557 deepEqual(
526558 documentModel.getData( new es.Range( 0, 5 ) ),
@@ -535,7 +567,7 @@
536568
537569 var removal = documentModel.prepareRemoval( new es.Range( 2, 4 ) );
538570
539 - // Test 3
 571+ // Test 5
540572 documentModel.commit( removal );
541573 deepEqual(
542574 documentModel.getData( new es.Range( 0, 3 ) ),
@@ -547,7 +579,7 @@
548580 'commit applies a removal transaction to the content'
549581 );
550582
551 - // Test 4
 583+ // Test 6
552584 documentModel.rollback( removal );
553585 deepEqual(
554586 documentModel.getData( new es.Range( 0, 5 ) ),
Index: trunk/parsers/wikidom/lib/hype/models/es.DocumentModel.js
@@ -126,45 +126,54 @@
127127 if ( this.set.length ) {
128128 for ( i = 0, length = this.set.length; i < length; i++ ) {
129129 annotation = this.set[i];
 130+ // Auto-build annotation hash
 131+ if ( annotation.hash === undefined ) {
 132+ annotation.hash = es.DocumentModel.getAnnotationHash( annotation );
 133+ }
130134 for ( j = this.cursor; j < to; j++ ) {
 135+ // Auto-convert to array
131136 if ( $.isArray( this.data[j] ) ) {
132137 this.data[j].push( annotation );
133138 } else {
134139 this.data[j] = [this.data[j], annotation];
135140 }
136141 }
137 - // Rebuild annotation hash
138 - annotation.hash = es.DocumentModel.getAnnotationHash( annotation );
139142 }
140143 }
141144 if ( this.clear.length ) {
142145 for ( i = 0, length = this.clear.length; i < length; i++ ) {
143146 annotation = this.clear[i];
 147+ // Auto-build annotation hash
 148+ if ( annotation.hash === undefined ) {
 149+ annotation.hash = es.DocumentModel.getAnnotationHash( annotation );
 150+ }
144151 for ( j = this.cursor; j < to; j++ ) {
145152 var index = es.DocumentModel.getIndexOfAnnotation( this.data[j], annotation );
146153 if ( index !== -1 ) {
147154 this.data[j].splice( index, 1 );
148155 }
 156+ // Auto-convert to string
 157+ if ( this.data[j].length === 1 ) {
 158+ this.data[j] = this.data[j][0];
 159+ }
149160 }
150 - // Rebuild annotation hash
151 - annotation.hash = es.DocumentModel.getAnnotationHash( annotation );
152161 }
153162 }
154163 }
155164
156165 function mark( op, invert ) {
157166 var target;
158 - if ( op.method === 'set' || ( op.method === 'clear' && invert ) ) {
 167+ if ( ( op.method === 'set' && !invert ) || ( op.method === 'clear' && invert ) ) {
159168 target = this.set;
160 - } else if ( op.method === 'clear' || ( op.method === 'set' && invert ) ) {
 169+ } else if ( ( op.method === 'clear' && !invert ) || ( op.method === 'set' && invert ) ) {
161170 target = this.clear;
162171 } else {
163172 throw 'Invalid method error. Can not operate attributes this way: ' + method;
164173 }
165174 if ( op.bias === 'start' ) {
166175 target.push( op.annotation );
167 - } else if ( op.bias === 'end' ) {
168 - var index = es.DocumentModel.getIndexOfAnnotation( target[i], op.annotation );
 176+ } else if ( op.bias === 'stop' ) {
 177+ var index = es.DocumentModel.getIndexOfAnnotation( target, op.annotation );
169178 if ( index === -1 ) {
170179 throw 'Annotation stack error. Annotation is missing.';
171180 }
@@ -191,19 +200,19 @@
192201 // Change element attributes
193202 'attribute': {
194203 'commit': function( op ) {
195 - attribute( op, false );
 204+ attribute.call( this, op, false );
196205 },
197206 'rollback': function( op ) {
198 - attribute( op, true );
 207+ attribute.call( this, op, true );
199208 }
200209 },
201210 // Change content annotations
202211 'annotate': {
203212 'commit': function( op ) {
204 - mark( op, false );
 213+ mark.call( this, op, false );
205214 },
206215 'rollback': function( op ) {
207 - mark( op, true );
 216+ mark.call( this, op, true );
208217 }
209218 }
210219 };
@@ -254,14 +263,18 @@
255264 return hash;
256265 };
257266
258 -es.DocumentModel.getIndexOfAnnotation = function( character, annotation ) {
 267+es.DocumentModel.getIndexOfAnnotation = function( annotations, annotation ) {
259268 if ( annotation === undefined || annotation.type === undefined ) {
260269 throw 'Invalid annotation error. Can not find non-annotation data in character.';
261270 }
262 - if ( $.isArray( character ) ) {
 271+ if ( $.isArray( annotations ) ) {
263272 // Find the index of a comparable annotation (checking for same value, not reference)
264 - for ( var i = 1; i < character.length; i++ ) {
265 - if ( character[i].hash === annotation.hash ) {
 273+ for ( var i = 0; i < annotations.length; i++ ) {
 274+ // Skip over character data - used when this is called on a content data item
 275+ if ( typeof annotations[i] === 'string' ) {
 276+ continue;
 277+ }
 278+ if ( annotations[i].hash === annotation.hash ) {
266279 return i;
267280 }
268281 }

Status & tagging log