Cscott has uploaded a new change for review.

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


Change subject: Cleanups for ve.d.Transaction.
......................................................................

Cleanups for ve.d.Transaction.

Avoid making a copy of the entire data array by using `doc.data` and
`doc.data.getLength()` instead of `doc.getData()` and
`doc.getData().length`.

Get rid of some unnecessary conditionals in `Transaction.newFromWrap`: the
`tx.pushReplace()` and `tx.pushRetain()` methods will already gracefully
no-op if the replaced/retained region is empty; we don't need to add
extra control flow to handle that case.

Fix a minor bug in `Transaction.newFromInsertion`: the final retain didn't
account for the length of the removed region, if `doc.fixupInsertion`
creates one.  (This usually doesn't happen.)

Remove an inaccurate TODO in `Transaction.pushReplaceMetadata()` -- it is
no longer a straight copy/paste of `Transaction.pushReplace`, so a refactor
isn't really called for.

Change-Id: I7d86a2449978365d69d4a5ed43116c2e9945475d
---
M modules/ve/dm/ve.dm.Transaction.js
1 file changed, 9 insertions(+), 13 deletions(-)


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

diff --git a/modules/ve/dm/ve.dm.Transaction.js 
b/modules/ve/dm/ve.dm.Transaction.js
index 38674b4..2fdbf92 100644
--- a/modules/ve/dm/ve.dm.Transaction.js
+++ b/modules/ve/dm/ve.dm.Transaction.js
@@ -31,7 +31,7 @@
  */
 ve.dm.Transaction.newFromInsertion = function ( doc, offset, insertion ) {
        var tx = new ve.dm.Transaction(),
-               data = doc.getData();
+               data = doc.data;
        // Fix up the insertion
        insertion = doc.fixupInsertion( insertion, offset );
        // Retain up to insertion point, if needed
@@ -39,7 +39,7 @@
        // Insert data
        tx.pushReplace( doc, insertion.offset, insertion.remove, insertion.data 
);
        // Retain to end of document, if needed (for completeness)
-       tx.pushRetain( data.length - insertion.offset );
+       tx.pushRetain( data.getLength() - ( insertion.offset + insertion.remove 
) );
        return tx;
 };
 
@@ -73,11 +73,11 @@
                removeStart = null,
                removeEnd = null,
                tx = new ve.dm.Transaction(),
-               data = doc.getData();
+               data = doc.data;
        // Validate range
        if ( range.isCollapsed() ) {
                // Empty range, nothing to remove, retain up to the end of the 
document (for completeness)
-               tx.pushRetain( data.length );
+               tx.pushRetain( data.getLength() );
                return tx;
        }
        // Select nodes and validate selection
@@ -102,7 +102,7 @@
                }
                tx.pushRetain( removeStart );
                tx.addSafeRemoveOps( doc, removeStart, removeEnd );
-               tx.pushRetain( data.length - removeEnd );
+               tx.pushRetain( data.getLength() - removeEnd );
                // All done
                return tx;
        }
@@ -149,7 +149,7 @@
                offset = removeEnd;
        }
        // Retain up to the end of the document
-       tx.pushRetain( data.length - offset );
+       tx.pushRetain( data.getLength() - offset );
        return tx;
 };
 
@@ -592,14 +592,11 @@
                tx.pushRetain( range.end - range.start );
        }
 
-       if ( wrapOuter.length > 0 || unwrapOuter.length > 0 ) {
-               tx.pushReplace( doc, range.end, unwrapOuter.length, 
closingArray( wrapOuter ) );
-       }
+       // this is a no-op if unwrapOuter.length===0 and wrapOuter.length===0
+       tx.pushReplace( doc, range.end, unwrapOuter.length, closingArray( 
wrapOuter ) );
 
        // Retain up to the end of the document
-       if ( range.end < doc.data.getLength() ) {
-               tx.pushRetain( doc.data.getLength() - range.end - 
unwrapOuter.length );
-       }
+       tx.pushRetain( doc.data.getLength() - range.end - unwrapOuter.length );
 
        return tx;
 };
@@ -913,7 +910,6 @@
 
 /**
  * Add a replace metadata operation
- * // TODO: this is a copy/paste of pushReplace (at the moment). Consider a 
refactor.
  *
  * @method
  * @param {Array} remove Metadata to remove

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7d86a2449978365d69d4a5ed43116c2e9945475d
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