Skip to content

Commit 23df53a

Browse files
committed
Refactor HTML sanitization into utility function
1 parent 7d6e8ca commit 23df53a

5 files changed

Lines changed: 20 additions & 12 deletions

File tree

packages/comment-widget/src/base-comment-item.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ export class BaseCommentItem extends LitElement {
106106
}
107107
}
108108
109-
@unocss-placeholder;
109+
@unocss-placeholder;
110110
`,
111111
];
112112
}

packages/comment-widget/src/base-form.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import type { ConfigMapData } from './types';
2222
import './comment-editor';
2323
import { ofetch } from 'ofetch';
2424
import type { CommentEditor } from './comment-editor';
25+
import { cleanHtml } from './utils/html';
2526

2627
export class BaseForm extends LitElement {
2728
@consume({ context: baseUrlContext })
@@ -278,7 +279,7 @@ export class BaseForm extends LitElement {
278279
}
279280

280281
private debouncedSubmit = debounce((data: Record<string, unknown>) => {
281-
const content = this.editorRef.value?.editor?.getHTML() || '';
282+
const content = cleanHtml(this.editorRef.value?.editor?.getHTML());
282283
const characterCount =
283284
this.editorRef.value?.editor?.storage.characterCount.characters();
284285

packages/comment-widget/src/comment-content.ts

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { css, html, LitElement, type PropertyValues, unsafeCSS } from 'lit';
22
import { property } from 'lit/decorators.js';
33
import { unsafeHTML } from 'lit/directives/unsafe-html.js';
4-
import sanitizeHtml from 'sanitize-html';
54
import baseStyles from './styles/base';
65
import contentStyles from './styles/content.css?inline';
6+
import { cleanHtml } from './utils/html';
77

88
export class CommentContent extends LitElement {
99
@property({ type: String })
@@ -55,14 +55,7 @@ export class CommentContent extends LitElement {
5555

5656
protected override render() {
5757
return html`
58-
<div class="content">${unsafeHTML(
59-
sanitizeHtml(this.content, {
60-
allowedAttributes: {
61-
...sanitizeHtml.defaults.allowedAttributes,
62-
code: ['class'],
63-
},
64-
})
65-
)}</div>
58+
<div class="content">${unsafeHTML(cleanHtml(this.content))}</div>
6659
`;
6760
}
6861

packages/comment-widget/src/comment-editor.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import contentStyles from './styles/content.css?inline';
88
import './comment-editor-skeleton';
99
import { property } from 'lit/decorators.js';
1010
import baseStyles from './styles/base';
11+
import { cleanHtml } from './utils/html';
1112

1213
interface ActionItem {
1314
name?: string;
@@ -137,7 +138,7 @@ export class CommentEditor extends LitElement {
137138
this.dispatchEvent(
138139
new CustomEvent('update', {
139140
detail: {
140-
content: this.editor?.getHTML(),
141+
content: cleanHtml(this.editor?.getHTML()),
141142
characterCount: this.editor?.storage.characterCount.characters(),
142143
},
143144
})
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import sanitizeHtml from 'sanitize-html';
2+
export function cleanHtml(content?: string) {
3+
if (!content) {
4+
return '';
5+
}
6+
7+
return sanitizeHtml(content, {
8+
allowedAttributes: {
9+
...sanitizeHtml.defaults.allowedAttributes,
10+
code: ['class'],
11+
},
12+
});
13+
}

0 commit comments

Comments
 (0)