Skip to content
Open
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
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"react": "^16.8.0 || >=17.0.0"
},
"dependencies": {
"html-entities": "^2.6.0",
"marked": "^16.0.0"
},
"devDependencies": {
Expand Down
20 changes: 6 additions & 14 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,11 @@
const htmlUnescapes: Record<string, string> = {
'&amp;': '&',
'&lt;': '<',
'&gt;': '>',
'&quot;': '"',
'&#39;': "'",
'&nbsp;': ' ',
'&#160;': ' ',
};

/** Used to match HTML entities and HTML characters. */
const reEscapedHtml = /&(?:amp|lt|gt|quot|nbsp|#(?:0+)?(?:39|160));/g;
const reHasEscapedHtml = RegExp(reEscapedHtml.source);
import { decode } from 'html-entities';

export const unescape = (str = '') => {
return reHasEscapedHtml.test(str) ? str.replace(reEscapedHtml, (entity) => htmlUnescapes[entity] || "'") : str;
if (!str || !str.includes('&')) {
return str;
}

return decode(str);
};

export const joinBase = (path: string, base?: string) => {
Expand Down
43 changes: 40 additions & 3 deletions tests/helpers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,52 @@ describe('Helpers', () => {
expect(joinBase('/path', 'http:')).toBe('/path');
});

it('should unescape strings', () => {
it('should unescape basic entities', () => {
expect(unescape('&amp;')).toBe('&');
expect(unescape('&lt;')).toBe('<');
expect(unescape('&gt;')).toBe('>');
expect(unescape('&quot;')).toBe('"');
expect(unescape('&#39;')).toBe("'");
expect(unescape('&nbsp;')).toBe(' ');
expect(unescape('&#160;')).toBe(' ');
});

it('should unescape named entities', () => {
expect(unescape('&nbsp;')).toBe('\u00A0');
expect(unescape('&copy;')).toBe('©');
expect(unescape('&reg;')).toBe('®');
expect(unescape('&trade;')).toBe('™');
expect(unescape('&euro;')).toBe('€');
});

it('should unescape numeric character references', () => {
expect(unescape('&#64;')).toBe('@');
expect(unescape('&#8364;')).toBe('€');
expect(unescape('&#x40;')).toBe('@');
expect(unescape('&#x20AC;')).toBe('€');
expect(unescape('&#160;')).toBe('\u00A0');
});

it('should unescape exotic entities', () => {
expect(unescape('&hearts;')).toBe('♥');
expect(unescape('&spades;')).toBe('♠');
expect(unescape('&clubs;')).toBe('♣');
expect(unescape('&diams;')).toBe('♦');
expect(unescape('&alpha;')).toBe('α');
expect(unescape('&beta;')).toBe('β');
expect(unescape('&gamma;')).toBe('γ');
expect(unescape('&pi;')).toBe('π');
expect(unescape('&sum;')).toBe('∑');
expect(unescape('&infin;')).toBe('∞');
expect(unescape('&asymp;')).toBe('≈');
expect(unescape('&ne;')).toBe('≠');
expect(unescape('&le;')).toBe('≤');
expect(unescape('&ge;')).toBe('≥');
});

it('should handle edge cases', () => {
expect(unescape('Tom &amp; Jerry &lt;div&gt;')).toBe('Tom & Jerry <div>');
expect(unescape('')).toBe('');
expect(unescape()).toBe('');
expect(unescape('No entities here')).toBe('No entities here');
expect(unescape('Just & ampersand')).toBe('Just & ampersand');
});
});