diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2022-08-20 14:32:34 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-20 14:32:34 -0400 |
commit | e7944d29b0380d46e44cc316e10a3088e9da3a8c (patch) | |
tree | 1df918c6e507d7b881356f44e36733acb5489792 /ext/js/dom/text-source-element.js | |
parent | 4194252fe363e9737abf4aa407d8ce14e7743559 (diff) |
TextSourceElement surrogate pair support (#2217)
* Update StringUtil
* Refactor
* Handle UTF-16 surrogate pairs
Diffstat (limited to 'ext/js/dom/text-source-element.js')
-rw-r--r-- | ext/js/dom/text-source-element.js | 30 |
1 files changed, 17 insertions, 13 deletions
diff --git a/ext/js/dom/text-source-element.js b/ext/js/dom/text-source-element.js index 257b9df4..fe3fe083 100644 --- a/ext/js/dom/text-source-element.js +++ b/ext/js/dom/text-source-element.js @@ -15,6 +15,10 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ +/* global + * StringUtil + */ + class TextSourceElement { constructor(element, fullContent=null, startOffset=0, endOffset=0) { this._element = element; @@ -61,24 +65,24 @@ class TextSourceElement { } setEndOffset(length, _layoutAwareScan, fromEnd) { - if (fromEnd) { - const delta = Math.min(this._fullContent.length - this._endOffset, length); - this._endOffset += delta; - this._content = this._fullContent.substring(this._startOffset, this._endOffset); - return delta; - } else { - const delta = Math.min(this._fullContent.length - this._startOffset, length); - this._endOffset = this._startOffset + delta; - this._content = this._fullContent.substring(this._startOffset, this._endOffset); - return delta; + const offset = fromEnd ? this._endOffset : this._startOffset; + length = Math.min(this._fullContent.length - offset, length); + if (length > 0) { + length = StringUtil.readCodePointsForward(this._fullContent, offset, length).length; } + this._endOffset = offset + length; + this._content = this._fullContent.substring(this._startOffset, this._endOffset); + return length; } setStartOffset(length) { - const delta = Math.min(this._startOffset, length); - this._startOffset -= delta; + length = Math.min(this._startOffset, length); + if (length > 0) { + length = StringUtil.readCodePointsBackward(this._fullContent, this._startOffset - 1, length).length; + } + this._startOffset -= length; this._content = this._fullContent.substring(this._startOffset, this._endOffset); - return delta; + return length; } collapse(toStart) { |