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

Change subject: Make ve.ce.Surface.getSelectionRect() tollerate browser bugs
......................................................................


Make ve.ce.Surface.getSelectionRect() tollerate browser bugs

.getClientRects() somestimes returns an empty collection which causes an
exception to be thrown by rangy. This corresponds to
.getBoundingDocumentRect() returning a hash full of zeroes.

Detect this and handle such ranges separately, by inserting dummy DOM
elements at the selection's beginning and end, then using their
position to determine where the actual selection was.

This behavior is seen sometimes in Opera, and in Chrome by using the link tool 
on text at the beginning of the document.

Bug: 47772
Change-Id: I4bad882d1d6fb83bcdcfd0de3bfc9af52960c2ff
---
M modules/ve/ce/ve.ce.Surface.js
1 file changed, 55 insertions(+), 5 deletions(-)

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



diff --git a/modules/ve/ce/ve.ce.Surface.js b/modules/ve/ce/ve.ce.Surface.js
index 125a961..b1f7c42 100644
--- a/modules/ve/ce/ve.ce.Surface.js
+++ b/modules/ve/ce/ve.ce.Surface.js
@@ -136,14 +136,64 @@
  * @static
  */
 ve.ce.Surface.getSelectionRect = function () {
+       var sel, rect, $span, startRange, startOffset, endRange, endOffset, 
scrollLeft, scrollTop;
+
        if ( !rangy.initialized ) {
                rangy.init();
        }
-       var rangySel = rangy.getSelection();
-       return {
-               start: rangySel.getStartDocumentPos(),
-               end: rangySel.getEndDocumentPos()
-       };
+
+       sel = rangy.getSelection();
+
+       // We can't do anything if there's no selection
+       if ( sel.rangeCount === 0 ) {
+               return null;
+       }
+
+       rect = sel.getBoundingDocumentRect();
+
+       // Sometimes the selection will have invalid bounding rect information, 
which presents as all
+       // rectangle dimensions being 0 which causes #getStartDocumentPos and 
#getEndDocumentPos to
+       // throw exceptions
+       if ( rect.top === 0 || rect.bottom === 0 || rect.left === 0 || 
rect.right === 0 ) {
+               // Use dummy DOM elements into the selection and then using 
their position
+               $span = $( '<span>' );
+
+               // Calculate starting range position
+               startRange = sel.getRangeAt( 0 );
+               startRange.insertNode( $span[0] );
+               startOffset = $span.offset();
+               $span.detach();
+
+               // Calculate ending range position
+               endRange = startRange.cloneRange();
+               endRange.collapse( false );
+               endRange.insertNode( $span[0] );
+               endOffset = $span.offset();
+               $span.detach();
+
+               // Restore the selection
+               startRange.refresh();
+
+               // Return the selection bounding rectangle
+               scrollLeft = $( window ).scrollLeft();
+               scrollTop = $( window ).scrollTop();
+               return {
+                       'start': {
+                               x: startOffset.left - scrollLeft,
+                               y: startOffset.top - scrollTop
+                       },
+                       'end': {
+                               x: endOffset.left - scrollLeft,
+                               // Adjust the vertical position by the 
line-height to get the bottom dimension
+                               y: endOffset.top - scrollTop + parseInt( 
$span.css( 'line-height' ), 10 )
+                       }
+               };
+       } else {
+               return {
+                       start: sel.getStartDocumentPos(),
+                       end: sel.getEndDocumentPos()
+               };
+       }
 };
 
 /* Methods */

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I4bad882d1d6fb83bcdcfd0de3bfc9af52960c2ff
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/VisualEditor
Gerrit-Branch: master
Gerrit-Owner: Matmarex <[email protected]>
Gerrit-Reviewer: Catrope <[email protected]>
Gerrit-Reviewer: Christian <[email protected]>
Gerrit-Reviewer: Inez <[email protected]>
Gerrit-Reviewer: Matmarex <[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