Skip to content

Commit

Permalink
fix: \char support for >16-bit Unicode characters (#3006)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
edemaine and ylemkimon committed Aug 28, 2021
1 parent 90dcaef commit ff1734f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
15 changes: 13 additions & 2 deletions src/functions/char.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
},
});
6 changes: 6 additions & 0 deletions test/katex-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -3313,6 +3313,12 @@ describe("A macro expander", function() {
expect(parsedChar[0].type).toEqual("textord");
});

it("\\char handles >16-bit characters", () => {
const parsed = getParsed('\\char"1d7d9');
expect(parsed[0].type).toEqual("textord");
expect(parsed[0].text).toEqual("𝟙");
});

it("should build Unicode private area characters", function() {
expect`\gvertneqq\lvertneqq\ngeqq\ngeqslant\nleqq`.toBuild();
expect`\nleqslant\nshortmid\nshortparallel\varsubsetneq`.toBuild();
Expand Down

0 comments on commit ff1734f

Please sign in to comment.