Skip to content

Commit 1bf1ebd

Browse files
committed
add support for stripHtml in NodeJS, decode HTML Entities
1 parent 5ea468e commit 1bf1ebd

File tree

8 files changed

+566
-22
lines changed

8 files changed

+566
-22
lines changed

.changeset/funny-carrots-add.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@chialab/sveltekit-utils": minor
3+
---
4+
5+
Add support for `stripHtml` in NodeJS, decode HTML Entities.

packages/utils/package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@
4848
"dependencies": {
4949
"@heyputer/kv.js": "fquffio/kv.js#fix/strict-mode",
5050
"cookie": "^1.0.2",
51+
"html-entities": "^2.6.0",
5152
"pino": "^9.5.0",
5253
"redis": "^4.7.0",
5354
"xml-js": "^1.6.11"
5455
},
5556
"devDependencies": {
57+
"@chialab/isomorphic-dom": "workspace:*",
5658
"@chialab/sveltekit-dev-utils": "workspace:*",
5759
"@sveltejs/adapter-static": "^3.0.6",
5860
"@sveltejs/kit": "^2.8.1",

packages/utils/src/lib/utils/browser.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
/**
2-
* Sanitize HTML string by removing all tags and returning only text content.
3-
*/
4-
export const sanitizeHtml = (html: string | null | undefined): string => {
5-
const div = document.createElement('div');
6-
div.innerHTML = html ?? '';
7-
8-
return div.innerText;
9-
};
10-
111
/**
122
* Check if is a mobile device.
133
*/

packages/utils/src/lib/utils/html.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { document } from '@chialab/isomorphic-dom';
2+
import { decode } from 'html-entities';
3+
4+
/**
5+
* Strip all HTML tags, attributes and comments and returning only text content.
6+
*
7+
* @param html The string to strip HTML from.
8+
*/
9+
export const stripHtml = (html: string | null | undefined): string => {
10+
const element = document.createElement('div');
11+
element.innerHTML = html ?? '';
12+
13+
return decode(element.innerText);
14+
};
15+
16+
/**
17+
* Strip all HTML tags, attributes and comments and returning only text content.
18+
*
19+
* @deprecated Use `stripHtml` instead. The functionality is identical.
20+
* @param html The string to strip HTML from.
21+
*/
22+
export const sanitizeHtml = stripHtml;

packages/utils/src/lib/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
export * from './browser.js';
22
export * from './collections.js';
33
export * from './fetch.js';
4+
export * from './html.js';
45
export * from './misc.js';
56
export * from './promises.js';
67
export * from './string.js';
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
1-
import { sanitizeHtml } from '$lib/utils/browser';
1+
import { stripHtml } from '$lib/utils/html';
22
import { describe, expect, it } from 'vitest';
33

4-
describe(sanitizeHtml.name, async () => {
4+
describe(stripHtml.name, async () => {
55
const CASES = {
66
'should remove tag': { html: 'hello <a href="https://example.org/">world</a>', expected: 'hello world' },
7+
'should remove comments and HTML entities': {
8+
html: '<span lang="it">questo &egrave; un test <!-- This is a test --></span>',
9+
expected: 'questo è un test ',
10+
},
711
} satisfies Record<string, { html: string | undefined | null; expected: string }>;
812

913
Object.entries(CASES).forEach(([label, { html, expected }]) =>
1014
it(label, () => {
11-
expect(sanitizeHtml(html)).to.equal(expected);
15+
expect(stripHtml(html)).to.equal(expected);
1216
}),
1317
);
1418
});

packages/utils/vitest.workspace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export default defineWorkspace([
44
{
55
extends: './vitest.config.ts',
66
test: {
7-
include: ['tests/**/*.{test,spec}.ts', '!tests/**/*.browser.{test,spec}.ts', '!tests/**/browser.{test,spec}.ts'],
7+
include: ['tests/**/*.{test,spec}.ts', '!tests/**/*.browser.{test,spec}.ts'],
88
name: 'server',
99
environment: 'node',
1010
},

0 commit comments

Comments
 (0)