fix(mobile): prevent invalid HTML entities from crashing markdown (#6495)
Co-authored-by: Simone <[email protected]>
This commit is contained in:
1 parent
fd51561b4e
commit
83ad26c3a3
2 files changed
+25
-2
No files matched your search
@@ -70,15 +70,22 @@ const EMPTY_CONTEXT: RunContext = {
|
||||
|
||||
const INLINE_HTML_TAG_PATTERN = /<\/?(?:kbd|mark|sub|sup|u)(?:\s[^>]*)?>/gi;
|
||||
|
||||
function decodeCodePoint(codePoint: number, entity: string): string {
|
||||
if (!Number.isInteger(codePoint) || codePoint < 0 || codePoint > 0x10ffff) {
|
||||
return entity;
|
||||
}
|
||||
return String.fromCodePoint(codePoint);
|
||||
}
|
||||
|
||||
function decodeHtmlEntitiesOnce(value: string): string {
|
||||
return value.replace(
|
||||
/&(?:#(\d+)|#x([0-9a-f]+)|amp|apos|gt|lt|nbsp|quot);/gi,
|
||||
(entity, decimal: string | undefined, hexadecimal: string | undefined) => {
|
||||
if (decimal) {
|
||||
return String.fromCodePoint(Number.parseInt(decimal, 10));
|
||||
return decodeCodePoint(Number.parseInt(decimal, 10), entity);
|
||||
}
|
||||
if (hexadecimal) {
|
||||
return String.fromCodePoint(Number.parseInt(hexadecimal, 16));
|
||||
return decodeCodePoint(Number.parseInt(hexadecimal, 16), entity);
|
||||
}
|
||||
switch (entity.toLowerCase()) {
|
||||
case "&":
|
||||
|
||||
@@ -126,6 +126,22 @@ describe("nativeMarkdownTextRuns", () => {
|
||||
]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
["😀", "😀"],
|
||||
["🚀", "🚀"],
|
||||
["�", "�"],
|
||||
["�", "�"],
|
||||
["&#9999999999;", "�"],
|
||||
["&#x110000;", "�"],
|
||||
])("normalizes numeric entity %s without throwing", (content, expected) => {
|
||||
const node: MarkdownNode = {
|
||||
type: "paragraph",
|
||||
children: [{ type: "text", content }],
|
||||
};
|
||||
|
||||
expect(nativeMarkdownTextRuns(node)).toEqual([{ text: expected }]);
|
||||
});
|
||||
|
||||
it("reads inline content from nested text nodes", () => {
|
||||
const node: MarkdownNode = {
|
||||
type: "paragraph",
|
||||
|
||||
Reference in new issue
Block a user