aboutsummaryrefslogtreecommitdiff
path: root/ext/fg/js
diff options
context:
space:
mode:
authorAlex Yatskov <alex@foosoft.net>2017-05-20 18:39:03 -0700
committerAlex Yatskov <alex@foosoft.net>2017-05-20 18:39:03 -0700
commit51092e2ff14889ea8dc7681801d01d29444112f2 (patch)
tree8dc5659b28e33bb1d91e93a045e2d67e654287a7 /ext/fg/js
parent89f6cb3074038c8c54b7e85c19dff1161f3ad3ae (diff)
parent52f092f0d2709e4fb8b1c00a4aea1fba97920509 (diff)
Merge branch 'dev'
Diffstat (limited to 'ext/fg/js')
-rw-r--r--ext/fg/js/source-range.js58
1 files changed, 36 insertions, 22 deletions
diff --git a/ext/fg/js/source-range.js b/ext/fg/js/source-range.js
index 6d445f54..58b6a415 100644
--- a/ext/fg/js/source-range.js
+++ b/ext/fg/js/source-range.js
@@ -20,28 +20,31 @@
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);
- return length - state.length;
+ this.content = state.content;
+ return length - state.remainder;
}
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);
- return length - state.length;
+ this.content = state.content;
+ return length - state.remainder;
}
containsPoint(point) {
@@ -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;
}
}