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 {
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);
}
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;
return this.content;
}
setEndOffset(length) {
this.length = length;
return length;
switch (this.element.nodeName) {
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) {
@ -71,6 +72,6 @@ class TextSourceElement {
}
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 {
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) {