From 1f3ff6fad012f28e8c21f9b5573ead6daf405afe Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Sat, 25 Jan 2020 11:18:18 -0500 Subject: Fix equals function on TextSourceRange when imposter elements are used --- ext/fg/js/document.js | 5 ++++- ext/fg/js/source.js | 19 ++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) (limited to 'ext/fg') diff --git a/ext/fg/js/document.js b/ext/fg/js/document.js index db9f0cdc..71654b29 100644 --- a/ext/fg/js/document.js +++ b/ext/fg/js/document.js @@ -110,6 +110,7 @@ function docRangeFromPoint(x, y, deepDomScan) { const elements = docElementsFromPoint(x, y, deepDomScan); let imposter = null; let imposterContainer = null; + let imposterSourceElement = null; if (elements.length > 0) { const element = elements[0]; switch (element.nodeName.toUpperCase()) { @@ -117,9 +118,11 @@ function docRangeFromPoint(x, y, deepDomScan) { case 'BUTTON': return new TextSourceElement(element); case 'INPUT': + imposterSourceElement = element; [imposter, imposterContainer] = docImposterCreate(element, false); break; case 'TEXTAREA': + imposterSourceElement = element; [imposter, imposterContainer] = docImposterCreate(element, true); break; } @@ -131,7 +134,7 @@ function docRangeFromPoint(x, y, deepDomScan) { docSetImposterStyle(imposterContainer.style, 'z-index', '-2147483646'); docSetImposterStyle(imposter.style, 'pointer-events', 'none'); } - return new TextSourceRange(range, '', imposterContainer); + return new TextSourceRange(range, '', imposterContainer, imposterSourceElement); } else { if (imposterContainer !== null) { imposterContainer.parentNode.removeChild(imposterContainer); diff --git a/ext/fg/js/source.js b/ext/fg/js/source.js index 5cdf47b5..11d3ff0e 100644 --- a/ext/fg/js/source.js +++ b/ext/fg/js/source.js @@ -25,14 +25,16 @@ const IGNORE_TEXT_PATTERN = /\u200c/; */ class TextSourceRange { - constructor(range, content, imposterContainer) { + constructor(range, content, imposterContainer, imposterSourceElement) { this.range = range; + this.rangeStartOffset = range.startOffset; this.content = content; this.imposterContainer = imposterContainer; + this.imposterSourceElement = imposterSourceElement; } clone() { - return new TextSourceRange(this.range.cloneRange(), this.content, this.imposterContainer); + return new TextSourceRange(this.range.cloneRange(), this.content, this.imposterContainer, this.imposterSourceElement); } cleanup() { @@ -55,6 +57,7 @@ class TextSourceRange { setStartOffset(length) { const state = TextSourceRange.seekBackward(this.range.startContainer, this.range.startOffset, length); this.range.setStart(state.node, state.offset); + this.rangeStartOffset = this.range.startOffset; this.content = state.content; return length - state.remainder; } @@ -79,7 +82,17 @@ class TextSourceRange { } equals(other) { - return other && other.range && other.range.compareBoundaryPoints(Range.START_TO_START, this.range) === 0; + if (other === null) { + return false; + } + if (this.imposterSourceElement !== null) { + return ( + this.imposterSourceElement === other.imposterSourceElement && + this.rangeStartOffset === other.rangeStartOffset + ); + } else { + return this.range.compareBoundaryPoints(Range.START_TO_START, other.range) === 0; + } } static shouldEnter(node) { -- cgit v1.2.3