TextSourceElement surrogate pair support (#2217)
* Update StringUtil * Refactor * Handle UTF-16 surrogate pairs
This commit is contained in:
parent
4194252fe3
commit
e7944d29b0
@ -27,16 +27,19 @@ class StringUtil {
|
|||||||
* @returns {string} The code points from the string.
|
* @returns {string} The code points from the string.
|
||||||
*/
|
*/
|
||||||
static readCodePointsForward(text, position, count) {
|
static readCodePointsForward(text, position, count) {
|
||||||
|
const textLength = text.length;
|
||||||
let result = '';
|
let result = '';
|
||||||
for (; count > 0; --count) {
|
for (; count > 0; --count) {
|
||||||
const char = text[position];
|
const char = text[position];
|
||||||
const charCode = char.charCodeAt(0);
|
|
||||||
result += char;
|
result += char;
|
||||||
if (charCode >= 0xd800 && charCode < 0xdc00 && ++position < text.length) {
|
if (++position >= textLength) { break; }
|
||||||
|
const charCode = char.charCodeAt(0);
|
||||||
|
if (charCode >= 0xd800 && charCode < 0xdc00) {
|
||||||
const char2 = text[position];
|
const char2 = text[position];
|
||||||
const charCode2 = char2.charCodeAt(0);
|
const charCode2 = char2.charCodeAt(0);
|
||||||
if (charCode2 >= 0xdc00 && charCode2 < 0xe000) {
|
if (charCode2 >= 0xdc00 && charCode2 < 0xe000) {
|
||||||
result += char2;
|
result += char2;
|
||||||
|
if (++position >= textLength) { break; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -54,13 +57,15 @@ class StringUtil {
|
|||||||
let result = '';
|
let result = '';
|
||||||
for (; count > 0; --count) {
|
for (; count > 0; --count) {
|
||||||
const char = text[position];
|
const char = text[position];
|
||||||
const charCode = char.charCodeAt(0);
|
|
||||||
result = char + result;
|
result = char + result;
|
||||||
if (charCode >= 0xdc00 && charCode < 0xe000 && position > 0) {
|
if (--position < 0) { break; }
|
||||||
const char2 = text[position - 1];
|
const charCode = char.charCodeAt(0);
|
||||||
|
if (charCode >= 0xdc00 && charCode < 0xe000) {
|
||||||
|
const char2 = text[position];
|
||||||
const charCode2 = char2.charCodeAt(0);
|
const charCode2 = char2.charCodeAt(0);
|
||||||
if (charCode2 >= 0xd800 && charCode2 < 0xdc00) {
|
if (charCode2 >= 0xd800 && charCode2 < 0xdc00) {
|
||||||
result = char2 + result;
|
result = char2 + result;
|
||||||
|
if (--position < 0) { break; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -15,6 +15,10 @@
|
|||||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/* global
|
||||||
|
* StringUtil
|
||||||
|
*/
|
||||||
|
|
||||||
class TextSourceElement {
|
class TextSourceElement {
|
||||||
constructor(element, fullContent=null, startOffset=0, endOffset=0) {
|
constructor(element, fullContent=null, startOffset=0, endOffset=0) {
|
||||||
this._element = element;
|
this._element = element;
|
||||||
@ -61,24 +65,24 @@ class TextSourceElement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
setEndOffset(length, _layoutAwareScan, fromEnd) {
|
setEndOffset(length, _layoutAwareScan, fromEnd) {
|
||||||
if (fromEnd) {
|
const offset = fromEnd ? this._endOffset : this._startOffset;
|
||||||
const delta = Math.min(this._fullContent.length - this._endOffset, length);
|
length = Math.min(this._fullContent.length - offset, length);
|
||||||
this._endOffset += delta;
|
if (length > 0) {
|
||||||
this._content = this._fullContent.substring(this._startOffset, this._endOffset);
|
length = StringUtil.readCodePointsForward(this._fullContent, offset, length).length;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
this._endOffset = offset + length;
|
||||||
|
this._content = this._fullContent.substring(this._startOffset, this._endOffset);
|
||||||
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
setStartOffset(length) {
|
setStartOffset(length) {
|
||||||
const delta = Math.min(this._startOffset, length);
|
length = Math.min(this._startOffset, length);
|
||||||
this._startOffset -= delta;
|
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);
|
this._content = this._fullContent.substring(this._startOffset, this._endOffset);
|
||||||
return delta;
|
return length;
|
||||||
}
|
}
|
||||||
|
|
||||||
collapse(toStart) {
|
collapse(toStart) {
|
||||||
|
Loading…
Reference in New Issue
Block a user