Text source refactor (#527)

* Update TextSourceElement API to have parity with TextSourceRange

* Make fields private

* Update equality check

* Fix "this.element" being used instead of just "element"
This commit is contained in:
toasted-nutbread 2020-05-10 14:06:25 -04:00 committed by GitHub
parent 6eafdac362
commit 5752b23a8e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -358,13 +358,32 @@ class TextSourceRange {
*/ */
class TextSourceElement { class TextSourceElement {
constructor(element, content='') { constructor(element, fullContent=null, startOffset=0, endOffset=0) {
this.element = element; this._element = element;
this.content = content; this._fullContent = (typeof fullContent === 'string' ? fullContent : TextSourceElement.getElementContent(element));
this._startOffset = startOffset;
this._endOffset = endOffset;
this._content = this._fullContent.substring(this._startOffset, this._endOffset);
}
get element() {
return this._element;
}
get fullContent() {
return this._fullContent;
}
get startOffset() {
return this._startOffset;
}
get endOffset() {
return this._endOffset;
} }
clone() { clone() {
return new TextSourceElement(this.element, this.content); return new TextSourceElement(this._element, this._fullContent, this._startOffset, this._endOffset);
} }
cleanup() { cleanup() {
@ -372,44 +391,32 @@ class TextSourceElement {
} }
text() { text() {
return this.content; return this._content;
} }
setEndOffset(length) { setEndOffset(length, fromEnd=false) {
switch (this.element.nodeName.toUpperCase()) { if (fromEnd) {
case 'BUTTON': const delta = Math.min(this._fullContent.length - this._endOffset, length);
this.content = this.element.textContent; this._endOffset += delta;
break; this._content = this._fullContent.substring(this._startOffset, this._endOffset);
case 'IMG': return delta;
this.content = this.element.getAttribute('alt'); } else {
break; const delta = Math.min(this._fullContent.length - this._startOffset, length);
default: this._endOffset = this._startOffset + delta;
this.content = this.element.value; this._content = this._fullContent.substring(this._startOffset, this._endOffset);
break; return delta;
}
let consumed = 0;
let content = '';
for (const currentChar of this.content || '') {
if (consumed >= length) {
break;
} else if (!currentChar.match(IGNORE_TEXT_PATTERN)) {
consumed++;
content += currentChar;
} }
} }
this.content = content; setStartOffset(length) {
const delta = Math.min(this._startOffset, length);
return this.content.length; this._startOffset -= delta;
} this._content = this._fullContent.substring(this._startOffset, this._endOffset);
return delta;
setStartOffset() {
return 0;
} }
getRect() { getRect() {
return this.element.getBoundingClientRect(); return this._element.getBoundingClientRect();
} }
getWritingMode() { getWritingMode() {
@ -429,8 +436,30 @@ class TextSourceElement {
typeof other === 'object' && typeof other === 'object' &&
other !== null && other !== null &&
other instanceof TextSourceElement && other instanceof TextSourceElement &&
other.element === this.element && this._element === other.element &&
other.content === this.content this._fullContent === other.fullContent &&
this._startOffset === other.startOffset &&
this._endOffset === other.endOffset
); );
} }
static getElementContent(element) {
let content;
switch (element.nodeName.toUpperCase()) {
case 'BUTTON':
content = element.textContent;
break;
case 'IMG':
content = element.getAttribute('alt') || '';
break;
default:
content = `${element.value}`;
break;
}
// Remove zero-width non-joiner
content = content.replace(/\u200c/g, '');
return content;
}
} }