Esanders has uploaded a new change for review.

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

Change subject: Workaround Chrome's Range.getClientRects bug
......................................................................

Workaround Chrome's Range.getClientRects bug

Do some DOM traversal so we only calculate for ranges which
Chrome can handle, then concatenate

See https://code.google.com/p/chromium/issues/detail?id=324437

Change-Id: I63c7a50f7efb73937b65c055675378f3b880e03e
---
M src/ce/ve.ce.Surface.js
1 file changed, 28 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor 
refs/changes/17/161017/1

diff --git a/src/ce/ve.ce.Surface.js b/src/ce/ve.ce.Surface.js
index 0ff903f..84086c7 100644
--- a/src/ce/ve.ce.Surface.js
+++ b/src/ce/ve.ce.Surface.js
@@ -324,7 +324,9 @@
  * @returns {Object[]|null} Selection rectangles
  */
 ve.ce.Surface.prototype.getSelectionRects = function ( range ) {
-       var i, l, rects, nativeRange, surfaceRect, focusNodeRect, rtl, x, 
focusedNode,
+       var i, l, nativeRange, surfaceRect, focusNodeRect, rtl, x, focusedNode,
+               endContainer, endOffset, partialRange,
+               rects = [],
                relativeRects = [];
 
        range = range || this.getModel().getSelection();
@@ -343,7 +345,31 @@
        // * in Firefox on page load when the address bar is still focused
        // * in empty paragraphs
        try {
-               rects = nativeRange.getClientRects();
+               // This block should just be rects = 
nativeRange.getClientRects(), but
+               // Chrome gets the end container rects wrong when spanning
+               // nodes so we need to traverse up the tree from the 
endContainer until
+               // we reach the common ancestor, then we can add on from start 
to where
+               // we got up to
+               // https://code.google.com/p/chromium/issues/detail?id=324437
+               endContainer = nativeRange.endContainer;
+               endOffset = nativeRange.endOffset;
+               partialRange = document.createRange();
+
+               while ( endContainer !== nativeRange.commonAncestorContainer ) {
+                       partialRange.setStart( endContainer, 0 );
+                       partialRange.setEnd( endContainer, endOffset );
+
+                       rects = rects.concat( $.makeArray( 
partialRange.getClientRects() ) );
+
+                       endOffset = ve.indexOf( endContainer, 
endContainer.parentNode.childNodes );
+                       endContainer = endContainer.parentNode;
+               }
+
+               // Once we've reached the common ancestor, add on the range 
from the
+               // original start position to where we ended up.
+               partialRange = nativeRange.cloneRange();
+               partialRange.setEnd( endContainer, endOffset );
+               rects = rects.concat( $.makeArray( 
partialRange.getClientRects() ) );
        } catch ( e ) {
                // When possible, pretend the cursor is the left/right border 
of the node
                // (depending on directionality) as a fallback.

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

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

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

Reply via email to