Skip to content

Commit 5673d60

Browse files
[trustedTypes]: Add TrustedTypes support to Svelte
1 parent eb530c8 commit 5673d60

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

.changeset/witty-lies-happen.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'svelte': minor
3+
---
4+
5+
Add TrustedTypes support

packages/svelte/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
"@jridgewell/sourcemap-codec": "^1.5.0",
167167
"@sveltejs/acorn-typescript": "^1.0.5",
168168
"@types/estree": "^1.0.5",
169+
"@types/trusted-types": "^2.0.7",
169170
"acorn": "^8.12.1",
170171
"aria-query": "^5.3.1",
171172
"axobject-query": "^4.1.0",
Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
1+
/** @import { TrustedTypePolicy } from 'trusted-types' */
2+
3+
/** @type {Pick<TrustedTypePolicy, 'createHTML'> | undefined} */
4+
let policy;
5+
6+
if (globalThis?.window?.trustedTypes) {
7+
policy = globalThis.window.trustedTypes.createPolicy('svelte-trusted-html', {
8+
/** @param {string} html */
9+
createHTML: (html) => {
10+
return html;
11+
}
12+
});
13+
}
14+
15+
/** @param {string} html */
16+
function create_trusted_html(html) {
17+
return /** @type {string} */(policy?.createHTML(html) ?? html);
18+
}
19+
120
/** @param {string} html */
221
export function create_fragment_from_html(html) {
322
var elem = document.createElement('template');
4-
elem.innerHTML = html.replaceAll('<!>', '<!---->'); // XHTML compliance
23+
elem.innerHTML = create_trusted_html(html.replaceAll('<!>', '<!---->')); // XHTML compliance
524
return elem.content;
625
}

pnpm-lock.yaml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)