Cscott has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/84441


Change subject: Add `ve.dm.Transaction.copy` method.
......................................................................

Add `ve.dm.Transaction.copy` method.

Change-Id: I3b9c46b635fec7046ef2d4cabd7f6ee2269cf621
---
M modules/ve/dm/ve.dm.Transaction.js
M modules/ve/test/dm/ve.dm.Transaction.test.js
2 files changed, 99 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/VisualEditor 
refs/changes/41/84441/1

diff --git a/modules/ve/dm/ve.dm.Transaction.js 
b/modules/ve/dm/ve.dm.Transaction.js
index bbc0aa9..90b218f 100644
--- a/modules/ve/dm/ve.dm.Transaction.js
+++ b/modules/ve/dm/ve.dm.Transaction.js
@@ -663,6 +663,23 @@
        return tx;
 };
 
+/**
+ * Create a new transaction from an intention.
+ *
+ * @private
+ * @param {ve.dm.Document} doc Document to generate a transaction for
+ * @param {Array[]} intention The transaction intention.
+ * @returns {ve.dm.Transaction}
+ */
+ve.dm.Transaction.newFromIntention = function ( doc, intention ) {
+       var constructorName = intention[0],
+               args = intention.slice( 1 );
+
+       return ve.dm.Transaction[constructorName].apply(
+               ve.dm.Transaction, [ doc ].concat( args )
+       );
+};
+
 /* Methods */
 
 /**
@@ -683,6 +700,17 @@
 };
 
 /**
+ * Duplicate a transaction.
+ *
+ * @method
+ * @param {ve.dm.Document} doc Document to generate a transaction for
+ * @returns {ve.dm.Transaction} A new transaction performing the same 
operations as this.
+ */
+ve.dm.Transaction.prototype.copy = function ( doc ) {
+       return ve.dm.Transaction.newFromIntention( doc, ve.copy( this.intention 
) );
+};
+
+/**
  * Get all operations.
  *
  * @method
diff --git a/modules/ve/test/dm/ve.dm.Transaction.test.js 
b/modules/ve/test/dm/ve.dm.Transaction.test.js
index 1202cdf..56ce801 100644
--- a/modules/ve/test/dm/ve.dm.Transaction.test.js
+++ b/modules/ve/test/dm/ve.dm.Transaction.test.js
@@ -1370,6 +1370,77 @@
        }
 } );
 
+QUnit.test( 'copy', function ( assert ) {
+       var cases = {
+               'newNoOp': {
+                       'doc': 'data',
+                       'call': [
+                               'newNoOp'
+                       ]
+               },
+               'newFromInsertion': {
+                       'doc': 'data',
+                       'call': [
+                               'newFromInsertion', 0,
+                               [{ 'type': 'paragraph' }, '1', { 'type': 
'/paragraph' }]
+                       ]
+               },
+               'newFromRemoval': {
+                       'doc': 'data',
+                       'call': [
+                               'newFromRemoval', new ve.Range( 1, 3 )
+                       ]
+               },
+               'newFromNodeReplacement': {
+                       'doc': 'internalData',
+                       'call': [
+                               'newFromNodeReplacement', new ve.Range( 7, 12 ),
+                               [ { 'type': 'paragraph' }, 'X', { 'type': 
'/paragraph' } ]
+                       ]
+               },
+               'newFromAttributeChanges': {
+                       'doc': 'data',
+                       'call': [
+                               'newFromAttributeChanges', 0, { 'level': 2 }
+                       ]
+               },
+               'newFromAnnotation': {
+                       'doc': 'data',
+                       'call': [
+                               'newFromAnnotation', new ve.Range( 1, 2 ), 
'set',
+                               ve.dm.example.createAnnotation( 
ve.dm.example.bold )
+                       ]
+               },
+               'newFromContentBranchConversion': {
+                       'doc': 'data',
+                       'call': [
+                               'newFromContentBranchConversion', new ve.Range( 
1, 2 ),
+                               'paragraph'
+                       ]
+               },
+               'newFromWrap': {
+                       'doc': 'listWithMeta',
+                       'call': [
+                               'newFromWrap', new ve.Range( 1, 11 ),
+                               [ { 'type': 'list' } ], [],
+                               [ { 'type': 'listItem', 'attributes': 
{'styles': ['bullet']} } ], []
+                       ]
+               }
+       };
+       // Run tests
+       QUnit.expect( ve.getObjectKeys( cases ).length );
+       $.each( cases, function( _, test ) {
+               var doc, args, tx1, tx2;
+               doc = ve.dm.example.createExampleDocument( test.doc );
+               args = [ doc ].concat( test.call.slice( 1 ) );
+               tx1 = ve.dm.Transaction[ test.call[0] ].apply(
+                       ve.dm.Transaction, args
+               );
+               tx2 = tx1.copy( doc );
+               assert.deepEqualWithDomElements( tx1, tx2 );
+       } );
+} );
+
 QUnit.test( 'pushRetain', 4, function ( assert ) {
        var cases = {
                'retain': {

-- 
To view, visit https://gerrit.wikimedia.org/r/84441
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I3b9c46b635fec7046ef2d4cabd7f6ee2269cf621
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Cscott <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to