Divec has uploaded a new change for review.

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

Change subject: Convenience cmpDocumentOrder method
......................................................................

Convenience cmpDocumentOrder method

Change-Id: I69529a4ad59c6c817581264083a261df510db0fe
---
M src/ce/ve.ce.Surface.js
M src/ve.utils.js
2 files changed, 25 insertions(+), 12 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor 
refs/changes/61/179361/1

diff --git a/src/ce/ve.ce.Surface.js b/src/ce/ve.ce.Surface.js
index 6eff240..9322591 100644
--- a/src/ce/ve.ce.Surface.js
+++ b/src/ce/ve.ce.Surface.js
@@ -1181,8 +1181,7 @@
  * @param {boolean} fixupCursor If destroying unicorns, fix the cursor 
position for expected movement
  */
 ve.ce.Surface.prototype.checkUnicorns = function ( fixupCursor ) {
-       var preUnicorn, postUnicorn, range, node, fixup, ancestor,
-               endCursorPos, preUnicornPos;
+       var preUnicorn, postUnicorn, range, node, fixup;
        if ( !this.unicorningNode || !this.unicorningNode.unicorns ) {
                return;
        }
@@ -1210,18 +1209,12 @@
 
        // Selection endpoint is not between unicorns.
        // Test whether it is before or after the pre-unicorn (i.e. 
before/after both unicorns)
-       ancestor = ve.getCommonAncestor( range.endContainer, preUnicorn );
-       if ( ancestor === null ) {
-               throw new Error( 'No common ancestor' );
-       }
-       endCursorPos = ve.getOffsetPath( ancestor, range.endContainer, 
range.endOffset );
-       preUnicornPos = ve.getOffsetPath(
-               ancestor,
+       if ( ve.cmpDocumentOrder(
+               range.endContainer,
+               range.endOffset,
                preUnicorn.parentNode,
                Array.prototype.indexOf.call( preUnicorn.parentNode.childNodes, 
preUnicorn )
-       );
-
-       if ( ve.cmpOffsetPaths( endCursorPos, preUnicornPos ) < 0 ) {
+       ) < 0 ) {
                // before the pre-unicorn
                fixup = -1;
        } else {
diff --git a/src/ve.utils.js b/src/ve.utils.js
index 40330da..7f83e54 100644
--- a/src/ve.utils.js
+++ b/src/ve.utils.js
@@ -1165,6 +1165,26 @@
 };
 
 /**
+ * Compare two nodes for position in document
+ *
+ * @param {Node} node1 First node
+ * @param {number} offset1 First offset
+ * @param {Node} node2 Second node
+ * @param {number} offset2 Second offset
+ * @return {number} negative, zero or positive number
+ */
+ve.cmpDocumentOrder = function ( node1, offset1, node2, offset2 ) {
+       var commonAncestor = ve.getCommonAncestor( node1, node2 );
+       if ( commonAncestor === null ) {
+               throw new Error( 'No common ancestor' );
+       }
+       return ve.cmpOffsetPaths(
+               ve.getOffsetPath( commonAncestor, node1, offset1 ),
+               ve.getOffsetPath( commonAncestor, node2, offset2 )
+       );
+};
+
+/**
  * Get the client platform string from the browser.
  *
  * HACK: This is a wrapper for calling getSystemPlatform() on the current 
platform

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I69529a4ad59c6c817581264083a261df510db0fe
Gerrit-PatchSet: 1
Gerrit-Project: VisualEditor/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Divec <[email protected]>

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

Reply via email to