https://www.mediawiki.org/wiki/Special:Code/MediaWiki/102043
Revision: 102043
Author: tparscal
Date: 2011-11-04 20:38:47 +0000 (Fri, 04 Nov 2011)
Log Message:
-----------
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
trunk/extensions/VisualEditor/modules/es/models/es.DocumentModel.js
trunk/extensions/VisualEditor/tests/es/es.DocumentModel.test.js
Modified: trunk/extensions/VisualEditor/modules/es/es.Transaction.js
===================================================================
--- trunk/extensions/VisualEditor/modules/es/es.Transaction.js 2011-11-04
20:29:28 UTC (rev 102042)
+++ trunk/extensions/VisualEditor/modules/es/es.Transaction.js 2011-11-04
20:38:47 UTC (rev 102043)
@@ -15,6 +15,26 @@
return this.operations;
};
+es.Transaction.prototype.optimize = function() {
+ for ( var i = 0; i < this.operations.length - 1; i++ ) {
+ var a = this.operations[i];
+ var b = this.operations[i + 1];
+ if ( a.type === b.type ) {
+ switch ( a.type ) {
+ case 'retain':
+ a.length += b.length;
+ this.operations.splice( i + 1, 1 );
+ break;
+ case 'insert':
+ case 'remove':
+ a.content = a.content.concat( b.content
);
+ this.operations.splice( i + 1, 1 );
+ break;
+ }
+ }
+ }
+};
+
es.Transaction.prototype.pushRetain = function( length ) {
this.operations.push( {
'type': 'retain',
Modified: trunk/extensions/VisualEditor/modules/es/models/es.DocumentModel.js
===================================================================
--- trunk/extensions/VisualEditor/modules/es/models/es.DocumentModel.js
2011-11-04 20:29:28 UTC (rev 102042)
+++ trunk/extensions/VisualEditor/modules/es/models/es.DocumentModel.js
2011-11-04 20:38:47 UTC (rev 102043)
@@ -961,6 +961,7 @@
tx.pushRetain( this.data.length - offset );
}
+ tx.optimize();
return tx;
/*
@@ -1102,7 +1103,8 @@
if ( range.end < doc.data.length ) {
tx.pushRetain( doc.data.length - range.end );
}
-
+
+ tx.optimize();
return tx;
};
@@ -1165,6 +1167,7 @@
if ( range.end < this.data.length ) {
tx.pushRetain( this.data.length - range.end );
}
+ tx.optimize();
return tx;
};
@@ -1189,6 +1192,7 @@
if ( offset < this.data.length ) {
tx.pushRetain( this.data.length - offset );
}
+ tx.optimize();
return tx;
};
Modified: trunk/extensions/VisualEditor/tests/es/es.DocumentModel.test.js
===================================================================
--- trunk/extensions/VisualEditor/tests/es/es.DocumentModel.test.js
2011-11-04 20:29:28 UTC (rev 102042)
+++ trunk/extensions/VisualEditor/tests/es/es.DocumentModel.test.js
2011-11-04 20:38:47 UTC (rev 102043)
@@ -301,7 +301,7 @@
);
} );
-test( 'es.DocumentModel.prepareRemoval', 3, function() {
+test( 'es.DocumentModel.prepareRemoval', 4, function() {
var documentModel = es.DocumentModel.newFromPlainObject( esTest.obj );
// Test 1
@@ -356,6 +356,22 @@
],
'prepareRemoval merges two list items'
);
+
+ // Test 4
+ deepEqual(
+ documentModel.prepareRemoval( new es.Range( 3, 9 )
).getOperations(),
+ [
+ { 'type': 'retain', 'length': 3 },
+ {
+ 'type': 'remove',
+ 'data': [
+ ['c', { 'type': 'textStyle/italic',
'hash': '#textStyle/italic' }]
+ ]
+ },
+ { 'type': 'retain', 'length': 24 }
+ ],
+ 'prepareRemoval works across structural nodes'
+ );
} );
test( 'es.DocumentModel.prepareInsertion', 11, function() {
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs