From a2139213c85a0b44e8241f00bca909dd200068e8 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 31 Aug 2019 23:44:24 -0400 Subject: [PATCH] Fix issue with whitespace ranges The size of the rects for these ranges will sometimes be excessively large on Firefox, leading to false positives. --- ext/fg/js/document.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index b6e1f83b..fc8000dd 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -199,10 +199,10 @@ function isPointInRange(x, y, range) { const nodePre = range.endContainer; const offsetPre = range.endOffset; try { - const {node, offset} = TextSourceRange.seekForward(range.endContainer, range.endOffset, 1); + const {node, offset, content} = TextSourceRange.seekForward(range.endContainer, range.endOffset, 1); range.setEnd(node, offset); - if (isPointInAnyRect(x, y, range.getClientRects())) { + if (!isWhitespace(content) && isPointInAnyRect(x, y, range.getClientRects())) { return true; } } finally { @@ -210,10 +210,10 @@ function isPointInRange(x, y, range) { } // Scan backward - const {node, offset} = TextSourceRange.seekBackward(range.startContainer, range.startOffset, 1); + const {node, offset, content} = TextSourceRange.seekBackward(range.startContainer, range.startOffset, 1); range.setStart(node, offset); - if (isPointInAnyRect(x, y, range.getClientRects())) { + if (!isWhitespace(content) && isPointInAnyRect(x, y, range.getClientRects())) { // This purposefully leaves the starting offset as modified and sets teh range length to 0. range.setEnd(node, offset); return true; @@ -223,6 +223,10 @@ function isPointInRange(x, y, range) { return false; } +function isWhitespace(string) { + return string.trim().length === 0; +} + function isPointInAnyRect(x, y, rects) { for (const rect of rects) { if (isPointInRect(x, y, rect)) {