Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: introduce decodeHtmlEntitites function #550

Merged
merged 2 commits into from
Jul 11, 2023
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
5 changes: 5 additions & 0 deletions .changeset/two-cats-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@headstartwp/core": minor
---

Introduces the `decodeHtmlSpecialChars` function.
9 changes: 9 additions & 0 deletions docs/documentation/03- Utilities/sanitization.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,12 @@ import { SafeHtml } from '@headstartwp/core/react';
<SafeHtml html="<div><p>hello world</p> div content</div>">
```

## decodeHtmlSpeciaChars

This function will decode a pre-defined set of html special chars.

```js
import { decodeHtmlSpeciaChars } from '@headstartwp/core';

decodeHtmlSpeciaChars('Hello world! &#8211; foo bar &#8211');
```
18 changes: 18 additions & 0 deletions packages/core/src/utils/__tests__/decodeHtmlSpeciaChars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { decodeHtmlSpeciaChars } from '../decodeHtmlSpeciaChars';

describe('decodeHtmlSpeciaChars', () => {
it('decodes html entities', () => {
expect(decodeHtmlSpeciaChars('&#8217;Hello&#8217;')).toBe('’Hello’');
expect(decodeHtmlSpeciaChars('&#8217;Hi &#038; Hello&#8217;')).toBe('’Hi & Hello’');
expect(decodeHtmlSpeciaChars('&#8220;Hi &#038; Hello&#8221;&#8211;Bye')).toBe(
'“Hi & Hello”–Bye',
);
expect(decodeHtmlSpeciaChars('&quot;Hi &amp; Hello&quot;&#8230;Bye')).toBe(
'"Hi & Hello"…Bye',
);

expect(decodeHtmlSpeciaChars('&quot;&lt;Hi &amp; Hello&gt;&quot;&#8230;Bye')).toBe(
'"<Hi & Hello>"…Bye',
);
});
});
24 changes: 24 additions & 0 deletions packages/core/src/utils/decodeHtmlSpeciaChars.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* Decodes HTML special chars entities
*
* @param text The text we want to decode
*
* @returns text with decoded html entities
*/
export function decodeHtmlSpeciaChars(text: string) {
if (!text) {
return '';
}

return text
.replace(/&#8217;/g, '’')
.replace(/&#038;/g, '&')
.replace(/&#8220;/g, '“')
.replace(/&#8221;/g, '”')
.replace(/&#8211;/g, '–')
.replace(/&#8230;/g, '…')
.replace(/&quot;/g, '"')
.replace(/&amp;/g, '&')
.replace(/&lt;/g, '<')
.replace(/&gt;/g, '>');
}
1 change: 1 addition & 0 deletions packages/core/src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export * from './errors';
export * from './isInternalLink';
export * from './url';
export * from './log';
export * from './decodeHtmlSpeciaChars';
Loading