From b04c4d8be79a476bf408eb3ad74648baf7ba8cf5 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 20 May 2017 18:15:51 -0700 Subject: block RT and some other tags when scanning, fixes #50, #30 --- ext/fg/js/source-range.js | 54 +++++++++++++++++++++++++++++------------------ 1 file changed, 34 insertions(+), 20 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/source-range.js b/ext/fg/js/source-range.js index 6d445f54..5db0ffc7 100644 --- a/ext/fg/js/source-range.js +++ b/ext/fg/js/source-range.js @@ -20,27 +20,30 @@ class TextSourceRange { constructor(range) { this.rng = range; + this.content = ''; } clone() { - return new TextSourceRange(this.rng.cloneRange()); + const tmp = new TextSourceRange(this.rng.cloneRange()); + tmp.content = this.content; + return tmp; } text() { - return this.rng.toString(); + return this.content; } setEndOffset(length) { - const lengthAdj = length + this.rng.startOffset; - const state = TextSourceRange.seekForward(this.rng.startContainer, lengthAdj); + const state = TextSourceRange.seekForward(this.rng.startContainer, this.rng.startOffset, length); this.rng.setEnd(state.node, state.offset); + this.content = state.content; return length - state.length; } setStartOffset(length) { - const lengthAdj = length + (this.rng.startContainer.length - this.rng.startOffset); - const state = TextSourceRange.seekBackward(this.rng.startContainer, lengthAdj); + const state = TextSourceRange.seekBackward(this.rng.startContainer, this.rng.startOffset, length); this.rng.setStart(state.node, state.offset); + this.content = state.content; return length - state.length; } @@ -80,8 +83,13 @@ class TextSourceRange { return other.rng && other.rng.compareBoundaryPoints(Range.START_TO_START, this.rng) === 0; } - static seekForward(node, length) { - const state = {node, length, offset: 0}; + static shouldEnter(node) { + const skip = ['RT', 'SCRIPT', 'STYLE']; + return !skip.includes(node.nodeName); + } + + static seekForward(node, offset, length) { + const state = {node, offset, remainder: length, content: ''}; if (!TextSourceRange.seekForwardHelper(node, state)) { return state; } @@ -99,11 +107,14 @@ class TextSourceRange { static seekForwardHelper(node, state) { if (node.nodeType === 3) { - const consumed = Math.min(node.length, state.length); + const offset = state.node === node ? state.offset : 0; + const remaining = node.length - offset; + const consumed = Math.min(remaining, state.remainder); + state.content = state.content + node.nodeValue.substring(offset, offset + consumed); state.node = node; - state.offset = consumed; - state.length -= consumed; - } else { + state.offset = offset + consumed; + state.remainder -= consumed; + } else if (TextSourceRange.shouldEnter(node)) { for (let i = 0; i < node.childNodes.length; ++i) { if (!TextSourceRange.seekForwardHelper(node.childNodes[i], state)) { break; @@ -111,11 +122,11 @@ class TextSourceRange { } } - return state.length > 0; + return state.remainder > 0; } - static seekBackward(node, length) { - const state = {node, length, offset: node.length}; + static seekBackward(node, offset, length) { + const state = {node, offset, remainder: length, content: ''}; if (!TextSourceRange.seekBackwardHelper(node, state)) { return state; } @@ -133,11 +144,14 @@ class TextSourceRange { static seekBackwardHelper(node, state) { if (node.nodeType === 3) { - const consumed = Math.min(node.length, state.length); + const offset = state.node === node ? state.offset : node.length; + const remaining = offset; + const consumed = Math.min(remaining, state.remainder); + state.content = node.nodeValue.substring(offset - consumed, offset) + state.content; state.node = node; - state.offset = node.length - consumed; - state.length -= consumed; - } else { + state.offset = offset - consumed; + state.remainder -= consumed; + } else if (TextSourceRange.shouldEnter(node)) { for (let i = node.childNodes.length - 1; i >= 0; --i) { if (!TextSourceRange.seekBackwardHelper(node.childNodes[i], state)) { break; @@ -145,6 +159,6 @@ class TextSourceRange { } } - return state.length > 0; + return state.remainder > 0; } } -- cgit v1.2.3 From 52f092f0d2709e4fb8b1c00a4aea1fba97920509 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 20 May 2017 18:34:13 -0700 Subject: fixing old variable name --- ext/fg/js/source-range.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'ext/fg/js') diff --git a/ext/fg/js/source-range.js b/ext/fg/js/source-range.js index 5db0ffc7..58b6a415 100644 --- a/ext/fg/js/source-range.js +++ b/ext/fg/js/source-range.js @@ -37,14 +37,14 @@ class TextSourceRange { const state = TextSourceRange.seekForward(this.rng.startContainer, this.rng.startOffset, length); this.rng.setEnd(state.node, state.offset); this.content = state.content; - return length - state.length; + return length - state.remainder; } setStartOffset(length) { const state = TextSourceRange.seekBackward(this.rng.startContainer, this.rng.startOffset, length); this.rng.setStart(state.node, state.offset); this.content = state.content; - return length - state.length; + return length - state.remainder; } containsPoint(point) { -- cgit v1.2.3