jenkins-bot has submitted this change and it was merged.

Change subject: Add a transaction builder for replacing the contents of a node
......................................................................


Add a transaction builder for replacing the contents of a node

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

Approvals:
  Trevor Parscal: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/modules/ve/dm/ve.dm.Transaction.js 
b/modules/ve/dm/ve.dm.Transaction.js
index 787cfd7..df3c542 100644
--- a/modules/ve/dm/ve.dm.Transaction.js
+++ b/modules/ve/dm/ve.dm.Transaction.js
@@ -154,6 +154,31 @@
 };
 
 /**
+ * Generate a transaction that replaces the contents of a branch node.
+ *
+ * The node whose contents are being replaced should be an unrestricted branch 
node.
+ *
+ * @param {ve.dm.Document} doc Document to create transaction for
+ * @param {ve.dm.Node|ve.Range} nodeOrRange Branch node or inner range of such
+ * @param {Array} newData Linear model data to replace the contents of the 
node with
+ * @returns {ve.dm.Transaction} Transaction that replaces the contents of the 
node
+ * @throws {Error} nodeOrRange must be a ve.dm.Node or a ve.Range
+ */
+ve.dm.Transaction.newFromNodeReplacement = function ( doc, nodeOrRange, 
newData ) {
+       var tx = new ve.dm.Transaction(), range = nodeOrRange;
+       if ( range instanceof ve.dm.Node ) {
+               range = range.getRange();
+       }
+       if ( !( range instanceof ve.Range ) ) {
+               throw new Error( 'nodeOrRange must be a ve.dm.Node or a 
ve.Range' );
+       }
+       tx.pushRetain( range.start );
+       tx.pushReplace( doc, range.start, range.end - range.start, newData );
+       tx.pushRetain( doc.data.getLength() - range.end );
+       return tx;
+};
+
+/**
  * Generate a transaction that changes an attribute.
  *
  * @static
diff --git a/modules/ve/test/dm/ve.dm.Transaction.test.js 
b/modules/ve/test/dm/ve.dm.Transaction.test.js
index 7944daa..4a8a08c 100644
--- a/modules/ve/test/dm/ve.dm.Transaction.test.js
+++ b/modules/ve/test/dm/ve.dm.Transaction.test.js
@@ -661,6 +661,40 @@
        runConstructorTests( assert, ve.dm.Transaction.newFromRemoval, cases );
 } );
 
+QUnit.test( 'newFromNodeReplacement', function ( assert ) {
+       var doc = ve.dm.example.createExampleDocument( 'internalData' ),
+               paragraph = [ { 'type': 'paragraph' }, 'H', 'e', 'l', 'l', 'o', 
{ 'type': '/paragraph' } ],
+               secondNode = doc.internalList.getItemNode( 1 ),
+               cases = {
+                       'replacing first internal node with paragraph': {
+                               'args': [doc, new ve.Range( 7, 12 ), paragraph],
+                               'ops': [
+                                       { 'type': 'retain', 'length': 7 },
+                                       {
+                                               'type': 'replace',
+                                               'remove': doc.data.slice( 7, 12 
),
+                                               'insert': paragraph
+                                       },
+                                       { 'type': 'retain', 'length': 15 }
+                               ]
+                       },
+                       'replacing second internal node with two paragraphs': {
+                               'args': [doc, secondNode, paragraph.concat( 
paragraph )],
+                               'ops': [
+                                       { 'type': 'retain', 'length': 14 },
+                                       {
+                                               'type': 'replace',
+                                               'remove': 
doc.data.getDataSlice( secondNode.getRange() ),
+                                               'insert': paragraph.concat( 
paragraph )
+                                       },
+                                       { 'type': 'retain', 'length': 8 }
+                               ]
+                       }
+               };
+       QUnit.expect( ve.getObjectKeys( cases ).length );
+       runConstructorTests( assert, ve.dm.Transaction.newFromNodeReplacement, 
cases );
+} );
+
 QUnit.test( 'newFromAttributeChange', function ( assert ) {
        var doc = ve.dm.example.createExampleDocument(),
                cases = {

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I69d362c4694104313dd2634434eddfbaf9435b22
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Catrope <[email protected]>
Gerrit-Reviewer: Trevor Parscal <[email protected]>
Gerrit-Reviewer: jenkins-bot

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

Reply via email to