Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions apps/mobile/modules/t3-markdown-text/src/nativeMarkdownText.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 "&amp;":
Expand Down
16 changes: 16 additions & 0 deletions apps/mobile/src/lib/nativeMarkdownText.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,22 @@ describe("nativeMarkdownTextRuns", () => {
]);
});

it.each([
["&#128512;", "😀"],
["&#x1f680;", "🚀"],
["&#9999999999;", "&#9999999999;"],
["&#x110000;", "&#x110000;"],
["&amp;#9999999999;", "&#9999999999;"],
["&amp;#x110000;", "&#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",
Expand Down
Loading