Fix issue with whitespace ranges

The size of the rects for these ranges will sometimes be excessively large on Firefox, leading to false positives.
This commit is contained in:
toasted-nutbread 2019-08-31 23:44:24 -04:00
parent ee59b3ab8b
commit a2139213c8

View File

@ -199,10 +199,10 @@ function isPointInRange(x, y, range) {
const nodePre = range.endContainer; const nodePre = range.endContainer;
const offsetPre = range.endOffset; const offsetPre = range.endOffset;
try { 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); range.setEnd(node, offset);
if (isPointInAnyRect(x, y, range.getClientRects())) { if (!isWhitespace(content) && isPointInAnyRect(x, y, range.getClientRects())) {
return true; return true;
} }
} finally { } finally {
@ -210,10 +210,10 @@ function isPointInRange(x, y, range) {
} }
// Scan backward // 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); 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. // This purposefully leaves the starting offset as modified and sets teh range length to 0.
range.setEnd(node, offset); range.setEnd(node, offset);
return true; return true;
@ -223,6 +223,10 @@ function isPointInRange(x, y, range) {
return false; return false;
} }
function isWhitespace(string) {
return string.trim().length === 0;
}
function isPointInAnyRect(x, y, rects) { function isPointInAnyRect(x, y, rects) {
for (const rect of rects) { for (const rect of rects) {
if (isPointInRect(x, y, rect)) { if (isPointInRect(x, y, rect)) {