Skip to content

Commit

Permalink
Mimic browser behavior in case of invalid numeric references, fixes #48
Browse files Browse the repository at this point in the history
  • Loading branch information
mdevils committed Jan 25, 2021
1 parent 8042062 commit d0ed012
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 320 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@typescript-eslint/parser": "^4.6.1",
"benchmark": "^2.1.4",
"chai": "^4.2.0",
"entities": "^2.1.0",
"entities": "^2.2.0",
"eslint": "^7.12.1",
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-import": "^2.22.1",
Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ const decodeRegExps: Record<Level, Record<DecodeScope, RegExp>> = {
};

const fromCharCode = String.fromCharCode;
const outOfBoundsChar = fromCharCode(65533);

export function decode(
text: string | undefined | null,
Expand All @@ -113,7 +114,7 @@ export function decode(
secondChar == 'x' || secondChar == 'X' ? parseInt(entity.substr(3), 16) : parseInt(entity.substr(2));

return code >= 0x10ffff
? entity
? outOfBoundsChar
: code > 65535
? fromCodePoint(code)
: fromCharCode(numericUnicodeMap[code] || code);
Expand Down
2 changes: 1 addition & 1 deletion test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ describe('decode()', () => {
expect(decode('')).to.equal('');
});
it('should handle invalid numeric entities', () => {
expect(decode('&#2013266066;')).to.equal('&#2013266066;');
expect(decode('&#2013266066;')).to.equal(String.fromCharCode(65533));
});
it('should decode numeric entities without semicolon', () => {
expect(decode('&#34C&#34')).to.equal('"C"');
Expand Down
Loading

0 comments on commit d0ed012

Please sign in to comment.