fix: \char support for >16-bit Unicode characters (#3006)

* fix: \char support for >16-bit Unicode characters

`String.fromCharCode` only supports Unicode characters up to 16-bit.
`String.fromCodePoint` from ECMAScript 2015 supports all Unicode code points.
Unfortunately, IE doesn't support the latter, so use former as fallback.

Fixes #3004

Co-authored-by: ylemkimon <y@ylem.kim>
This commit is contained in:
Erik Demaine
2021-08-28 18:55:05 -04:00
committed by GitHub
parent 90dcaef2bd
commit ff1734f7c4
2 changed files with 19 additions and 2 deletions

View File

@@ -21,14 +21,25 @@ defineFunction({
const node = assertNodeType(group[i], "textord");
number += node.text;
}
const code = parseInt(number);
let code = parseInt(number);
let text;
if (isNaN(code)) {
throw new ParseError(`\\@char has non-numeric argument ${number}`);
// If we drop IE support, the following code could be replaced with
// text = String.fromCodePoint(code)
} else if (code < 0 || code >= 0x10ffff) {
throw new ParseError(`\\@char with invalid code point ${number}`);
} else if (code <= 0xffff) {
text = String.fromCharCode(code);
} else { // Astral code point; split into surrogate halves
code -= 0x10000;
text = String.fromCharCode((code >> 10) + 0xd800,
(code & 0x3ff) + 0xdc00);
}
return {
type: "textord",
mode: parser.mode,
text: String.fromCharCode(code),
text: text,
};
},
});