diff --git a/.changeset/two-cats-vanish.md b/.changeset/two-cats-vanish.md new file mode 100644 index 000000000..b63ce069a --- /dev/null +++ b/.changeset/two-cats-vanish.md @@ -0,0 +1,5 @@ +--- +"@headstartwp/core": minor +--- + +Introduces the `decodeHtmlSpecialChars` function. diff --git a/docs/documentation/03- Utilities/sanitization.md b/docs/documentation/03- Utilities/sanitization.md index 53391e990..4baf7138f 100644 --- a/docs/documentation/03- Utilities/sanitization.md +++ b/docs/documentation/03- Utilities/sanitization.md @@ -55,3 +55,12 @@ import { SafeHtml } from '@headstartwp/core/react'; ``` +## decodeHtmlSpeciaChars + +This function will decode a pre-defined set of html special chars. + +```js +import { decodeHtmlSpeciaChars } from '@headstartwp/core'; + +decodeHtmlSpeciaChars('Hello world! – foo bar –'); +``` \ No newline at end of file diff --git a/packages/core/src/utils/__tests__/decodeHtmlEntities.ts b/packages/core/src/utils/__tests__/decodeHtmlEntities.ts deleted file mode 100644 index c19b8bb9f..000000000 --- a/packages/core/src/utils/__tests__/decodeHtmlEntities.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { decodeHtmlEntities } from '../decodeHtmlEntities'; - -describe('decodeHtmlEntities', () => { - it('decodes html entities', () => { - expect(decodeHtmlEntities('’Hello’')).toBe('’Hello’'); - expect(decodeHtmlEntities('’Hi & Hello’')).toBe('’Hi & Hello’'); - expect(decodeHtmlEntities('“Hi & Hello”–Bye')).toBe( - '“Hi & Hello”–Bye', - ); - expect(decodeHtmlEntities('"Hi & Hello"…Bye')).toBe('"Hi & Hello"…Bye'); - }); -}); diff --git a/packages/core/src/utils/__tests__/decodeHtmlSpeciaChars.ts b/packages/core/src/utils/__tests__/decodeHtmlSpeciaChars.ts new file mode 100644 index 000000000..f60ead438 --- /dev/null +++ b/packages/core/src/utils/__tests__/decodeHtmlSpeciaChars.ts @@ -0,0 +1,18 @@ +import { decodeHtmlSpeciaChars } from '../decodeHtmlSpeciaChars'; + +describe('decodeHtmlSpeciaChars', () => { + it('decodes html entities', () => { + expect(decodeHtmlSpeciaChars('’Hello’')).toBe('’Hello’'); + expect(decodeHtmlSpeciaChars('’Hi & Hello’')).toBe('’Hi & Hello’'); + expect(decodeHtmlSpeciaChars('“Hi & Hello”–Bye')).toBe( + '“Hi & Hello”–Bye', + ); + expect(decodeHtmlSpeciaChars('"Hi & Hello"…Bye')).toBe( + '"Hi & Hello"…Bye', + ); + + expect(decodeHtmlSpeciaChars('"<Hi & Hello>"…Bye')).toBe( + '""…Bye', + ); + }); +}); diff --git a/packages/core/src/utils/decodeHtmlEntities.ts b/packages/core/src/utils/decodeHtmlSpeciaChars.ts similarity index 67% rename from packages/core/src/utils/decodeHtmlEntities.ts rename to packages/core/src/utils/decodeHtmlSpeciaChars.ts index 2891da617..99a696741 100644 --- a/packages/core/src/utils/decodeHtmlEntities.ts +++ b/packages/core/src/utils/decodeHtmlSpeciaChars.ts @@ -1,11 +1,11 @@ /** - * Decodes HTML entities + * Decodes HTML special chars entities * * @param text The text we want to decode * * @returns text with decoded html entities */ -export function decodeHtmlEntities(text: string) { +export function decodeHtmlSpeciaChars(text: string) { if (!text) { return ''; } @@ -18,5 +18,7 @@ export function decodeHtmlEntities(text: string) { .replace(/–/g, '–') .replace(/…/g, '…') .replace(/"/g, '"') - .replace(/&/g, '&'); + .replace(/&/g, '&') + .replace(/</g, '<') + .replace(/>/g, '>'); } diff --git a/packages/core/src/utils/index.ts b/packages/core/src/utils/index.ts index 19f76606a..0446a8d50 100644 --- a/packages/core/src/utils/index.ts +++ b/packages/core/src/utils/index.ts @@ -7,4 +7,4 @@ export * from './errors'; export * from './isInternalLink'; export * from './url'; export * from './log'; -export * from './decodeHtmlEntities'; +export * from './decodeHtmlSpeciaChars';