r102043 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r102042‎ | r102043 | r102044 >
Date:20:38, 4 November 2011
Author:tparscal
Status:deferred
Tags:
Comment:
Added es.Transaction.optimize and added in a test that neilk sent a patch for
Modified paths:
  • /trunk/extensions/VisualEditor/modules/es/es.Transaction.js (modified) (history)
  • /trunk/extensions/VisualEditor/modules/es/models/es.DocumentModel.js (modified) (history)
  • /trunk/extensions/VisualEditor/tests/es/es.DocumentModel.test.js (modified) (history)

Diff [purge]

Index: trunk/extensions/VisualEditor/tests/es/es.DocumentModel.test.js
@@ -301,7 +301,7 @@
302302 );
303303 } );
304304
305 -test( 'es.DocumentModel.prepareRemoval', 3, function() {
 305+test( 'es.DocumentModel.prepareRemoval', 4, function() {
306306 var documentModel = es.DocumentModel.newFromPlainObject( esTest.obj );
307307
308308 // Test 1
@@ -356,6 +356,22 @@
357357 ],
358358 'prepareRemoval merges two list items'
359359 );
 360+
 361+ // Test 4
 362+ deepEqual(
 363+ documentModel.prepareRemoval( new es.Range( 3, 9 ) ).getOperations(),
 364+ [
 365+ { 'type': 'retain', 'length': 3 },
 366+ {
 367+ 'type': 'remove',
 368+ 'data': [
 369+ ['c', { 'type': 'textStyle/italic', 'hash': '#textStyle/italic' }]
 370+ ]
 371+ },
 372+ { 'type': 'retain', 'length': 24 }
 373+ ],
 374+ 'prepareRemoval works across structural nodes'
 375+ );
360376 } );
361377
362378 test( 'es.DocumentModel.prepareInsertion', 11, function() {
Index: trunk/extensions/VisualEditor/modules/es/models/es.DocumentModel.js
@@ -961,6 +961,7 @@
962962 tx.pushRetain( this.data.length - offset );
963963 }
964964
 965+ tx.optimize();
965966 return tx;
966967
967968 /*
@@ -1102,7 +1103,8 @@
11031104 if ( range.end < doc.data.length ) {
11041105 tx.pushRetain( doc.data.length - range.end );
11051106 }
1106 -
 1107+
 1108+ tx.optimize();
11071109 return tx;
11081110 };
11091111
@@ -1165,6 +1167,7 @@
11661168 if ( range.end < this.data.length ) {
11671169 tx.pushRetain( this.data.length - range.end );
11681170 }
 1171+ tx.optimize();
11691172 return tx;
11701173 };
11711174
@@ -1189,6 +1192,7 @@
11901193 if ( offset < this.data.length ) {
11911194 tx.pushRetain( this.data.length - offset );
11921195 }
 1196+ tx.optimize();
11931197 return tx;
11941198 };
11951199
Index: trunk/extensions/VisualEditor/modules/es/es.Transaction.js
@@ -15,6 +15,26 @@
1616 return this.operations;
1717 };
1818
 19+es.Transaction.prototype.optimize = function() {
 20+ for ( var i = 0; i < this.operations.length - 1; i++ ) {
 21+ var a = this.operations[i];
 22+ var b = this.operations[i + 1];
 23+ if ( a.type === b.type ) {
 24+ switch ( a.type ) {
 25+ case 'retain':
 26+ a.length += b.length;
 27+ this.operations.splice( i + 1, 1 );
 28+ break;
 29+ case 'insert':
 30+ case 'remove':
 31+ a.content = a.content.concat( b.content );
 32+ this.operations.splice( i + 1, 1 );
 33+ break;
 34+ }
 35+ }
 36+ }
 37+};
 38+
1939 es.Transaction.prototype.pushRetain = function( length ) {
2040 this.operations.push( {
2141 'type': 'retain',

Status & tagging log