From fd346ae44b75afa2a081d3ce270568258dd8390b Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 22 May 2017 21:07:25 -0700 Subject: wip --- ext/fg/js/source-element.js | 33 +++++++++++++++++---------------- ext/fg/js/source-range.js | 26 ++++++++++++-------------- 2 files changed, 29 insertions(+), 30 deletions(-) diff --git a/ext/fg/js/source-element.js b/ext/fg/js/source-element.js index 69fbc5ab..a8101382 100644 --- a/ext/fg/js/source-element.js +++ b/ext/fg/js/source-element.js @@ -18,41 +18,42 @@ class TextSourceElement { - constructor(element, length=-1) { + constructor(element, content='') { this.element = element; - this.length = length; + this.content = content; } clone() { - return new TextSourceElement(this.element, this.length); + return new TextSourceElement(this.element, this.content); } text() { - const text = this.textRaw(); - return this.length < 0 ? text : text.substring(0, this.length); + return this.content; } - textRaw() { + setEndOffset(length) { switch (this.element.nodeName) { case 'BUTTON': - return this.element.innerHTML; + this.content = this.element.innerHTML; + break; case 'IMG': - return this.element.getAttribute('alt'); + this.content = this.element.getAttribute('alt'); + break; default: - return this.element.value || ''; + this.content = this.element.value; + break; } + + this.content = this.content || ''; + this.content = this.content.substring(0, length); + + return this.content.length; } setStartOffset(length) { - // NOP return 0; } - setEndOffset(length) { - this.length = length; - return length; - } - containsPoint(point) { const rect = this.getRect(); return point.x >= rect.left && point.x <= rect.right; @@ -71,6 +72,6 @@ class TextSourceElement { } equals(other) { - return other.element && other.textRaw() === this.textRaw(); + return other.element === this.element && other.content === this.content; } } diff --git a/ext/fg/js/source-range.js b/ext/fg/js/source-range.js index 95177f0b..fa73b0a4 100644 --- a/ext/fg/js/source-range.js +++ b/ext/fg/js/source-range.js @@ -18,15 +18,13 @@ class TextSourceRange { - constructor(range) { - this.rng = range; - this.content = ''; + constructor(range, content='') { + this.range = range; + this.content = content; } clone() { - const tmp = new TextSourceRange(this.rng.cloneRange()); - tmp.content = this.content; - return tmp; + return new TextSourceRange(this.range.cloneRange(), this.content); } text() { @@ -34,15 +32,15 @@ class TextSourceRange { } setEndOffset(length) { - const state = TextSourceRange.seekForward(this.rng.startContainer, this.rng.startOffset, length); - this.rng.setEnd(state.node, state.offset); + const state = TextSourceRange.seekForward(this.range.startContainer, this.range.startOffset, length); + this.range.setEnd(state.node, state.offset); this.content = state.content; return length - state.remainder; } setStartOffset(length) { - const state = TextSourceRange.seekBackward(this.rng.startContainer, this.rng.startOffset, length); - this.rng.setStart(state.node, state.offset); + const state = TextSourceRange.seekBackward(this.range.startContainer, this.range.startOffset, length); + this.range.setStart(state.node, state.offset); this.content = state.content; return length - state.remainder; } @@ -53,11 +51,11 @@ class TextSourceRange { } getRect() { - return this.rng.getBoundingClientRect(); + return this.range.getBoundingClientRect(); } getPaddedRect() { - const range = this.rng.cloneRange(); + const range = this.range.cloneRange(); const startOffset = range.startOffset; const endOffset = range.endOffset; const node = range.startContainer; @@ -71,7 +69,7 @@ class TextSourceRange { select() { const selection = window.getSelection(); selection.removeAllRanges(); - selection.addRange(this.rng); + selection.addRange(this.range); } deselect() { @@ -80,7 +78,7 @@ class TextSourceRange { } equals(other) { - return other.rng && other.rng.compareBoundaryPoints(Range.START_TO_START, this.rng) === 0; + return other.range && other.range.compareBoundaryPoints(Range.START_TO_START, this.range) === 0; } static shouldEnter(node) { -- cgit v1.2.3