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
14 changes: 11 additions & 3 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,20 @@ export function escapeHtmlEntities(html: string, encode?: boolean) {
* replacement character.
*/
export function decodeNumericCharacterReferences(text: string) {
return text.replace(other.numericCharacterReference, (_, dec: string, hex: string) => {
const code = dec === undefined ? Number.parseInt(hex, 16) : Number.parseInt(dec, 10);
if (!other.numericCharacterReference.test(text)) {
return text;
}
// A decoded character must not join its neighbours into a new reference,
// so every `&` that is not already one is escaped here.
return text.replace(other.numericCharacterReferenceOrAmpersand, (ref: string, dec?: string, hex?: string) => {
if (dec === undefined && hex === undefined) {
return '&';
}
const code = dec === undefined ? Number.parseInt(hex!, 16) : Number.parseInt(dec, 10);
if (code === 0 || code > 0x10ffff || (code >= 0xd800 && code <= 0xdfff)) {
return '�';
}
return String.fromCodePoint(code);
return code === 38 ? '&amp;' : String.fromCodePoint(code);
});
}

Expand Down
3 changes: 2 additions & 1 deletion src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ export const other = {
endAngleBracket: />$/,
pedanticHrefTitle: /^([^'"]*[^\s])\s+(['"])(.*)\2/,
unicodeAlphaNumeric: /[\p{L}\p{N}]/u,
numericCharacterReference: /&#(?:(\d{1,7})|[Xx]([A-Fa-f0-9]{1,6}));/g,
numericCharacterReference: /&#(?:(\d{1,7})|[Xx]([A-Fa-f0-9]{1,6}));/,
numericCharacterReferenceOrAmpersand: /&#(?:(\d{1,7})|[Xx]([A-Fa-f0-9]{1,6}));|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/g,
escapeTest: /[&<>"']/,
escapeReplace: /[&<>"']/g,
escapeTestNoEncode: /[<>"']|&(?!(#\d{1,7}|#[Xx][a-fA-F0-9]{1,6}|\w+);)/,
Expand Down
2 changes: 2 additions & 0 deletions test/specs/new/numeric_character_references.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
<p>�</p>
<p>&amp;#abcdef0;</p>
<p><code>&amp;#35;</code></p>
<p>&amp;amp; &amp;lt;</p>
<p>&amp;#35; &amp;copy; &amp;amp;</p>
4 changes: 4 additions & 0 deletions test/specs/new/numeric_character_references.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ renderExact: true
&#abcdef0;

`&#35;`

&#38;amp; &#x26;lt;

&&#35;35; &co&#112;y; &amp&#59;
Loading