This commit is contained in:
Alex Yatskov 2017-05-22 21:07:25 -07:00
parent c6673bdf48
commit fd346ae44b
2 changed files with 37 additions and 38 deletions

View File

@ -18,39 +18,40 @@
class TextSourceElement { class TextSourceElement {
constructor(element, length=-1) { constructor(element, content='') {
this.element = element; this.element = element;
this.length = length; this.content = content;
} }
clone() { clone() {
return new TextSourceElement(this.element, this.length); return new TextSourceElement(this.element, this.content);
} }
text() { text() {
const text = this.textRaw(); return this.content;
return this.length < 0 ? text : text.substring(0, this.length);
}
textRaw() {
switch (this.element.nodeName) {
case 'BUTTON':
return this.element.innerHTML;
case 'IMG':
return this.element.getAttribute('alt');
default:
return this.element.value || '';
}
}
setStartOffset(length) {
// NOP
return 0;
} }
setEndOffset(length) { setEndOffset(length) {
this.length = length; switch (this.element.nodeName) {
return length; case 'BUTTON':
this.content = this.element.innerHTML;
break;
case 'IMG':
this.content = this.element.getAttribute('alt');
break;
default:
this.content = this.element.value;
break;
}
this.content = this.content || '';
this.content = this.content.substring(0, length);
return this.content.length;
}
setStartOffset(length) {
return 0;
} }
containsPoint(point) { containsPoint(point) {
@ -71,6 +72,6 @@ class TextSourceElement {
} }
equals(other) { equals(other) {
return other.element && other.textRaw() === this.textRaw(); return other.element === this.element && other.content === this.content;
} }
} }

View File

@ -18,15 +18,13 @@
class TextSourceRange { class TextSourceRange {
constructor(range) { constructor(range, content='') {
this.rng = range; this.range = range;
this.content = ''; this.content = content;
} }
clone() { clone() {
const tmp = new TextSourceRange(this.rng.cloneRange()); return new TextSourceRange(this.range.cloneRange(), this.content);
tmp.content = this.content;
return tmp;
} }
text() { text() {
@ -34,15 +32,15 @@ class TextSourceRange {
} }
setEndOffset(length) { setEndOffset(length) {
const state = TextSourceRange.seekForward(this.rng.startContainer, this.rng.startOffset, length); const state = TextSourceRange.seekForward(this.range.startContainer, this.range.startOffset, length);
this.rng.setEnd(state.node, state.offset); this.range.setEnd(state.node, state.offset);
this.content = state.content; this.content = state.content;
return length - state.remainder; return length - state.remainder;
} }
setStartOffset(length) { setStartOffset(length) {
const state = TextSourceRange.seekBackward(this.rng.startContainer, this.rng.startOffset, length); const state = TextSourceRange.seekBackward(this.range.startContainer, this.range.startOffset, length);
this.rng.setStart(state.node, state.offset); this.range.setStart(state.node, state.offset);
this.content = state.content; this.content = state.content;
return length - state.remainder; return length - state.remainder;
} }
@ -53,11 +51,11 @@ class TextSourceRange {
} }
getRect() { getRect() {
return this.rng.getBoundingClientRect(); return this.range.getBoundingClientRect();
} }
getPaddedRect() { getPaddedRect() {
const range = this.rng.cloneRange(); const range = this.range.cloneRange();
const startOffset = range.startOffset; const startOffset = range.startOffset;
const endOffset = range.endOffset; const endOffset = range.endOffset;
const node = range.startContainer; const node = range.startContainer;
@ -71,7 +69,7 @@ class TextSourceRange {
select() { select() {
const selection = window.getSelection(); const selection = window.getSelection();
selection.removeAllRanges(); selection.removeAllRanges();
selection.addRange(this.rng); selection.addRange(this.range);
} }
deselect() { deselect() {
@ -80,7 +78,7 @@ class TextSourceRange {
} }
equals(other) { 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) { static shouldEnter(node) {