Skip to content

[fix]: Add support for TrustedTypes in Svelte #16271

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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/witty-lies-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'svelte': minor
---

Add TrustedTypes support
1 change: 1 addition & 0 deletions packages/svelte/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@
"@jridgewell/sourcemap-codec": "^1.5.0",
"@sveltejs/acorn-typescript": "^1.0.5",
"@types/estree": "^1.0.5",
"@types/trusted-types": "^2.0.7",
"acorn": "^8.12.1",
"aria-query": "^5.3.1",
"axobject-query": "^4.1.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/internal/client/dom/blocks/html.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ export function html(node, get_value, svg = false, mathml = false, skip_warning
// Don't use create_fragment_with_script_from_html here because that would mean script tags are executed.
// @html is basically `.innerHTML = ...` and that doesn't execute scripts either due to security reasons.
/** @type {DocumentFragment | Element} */
var node = create_fragment_from_html(html);
var node = create_fragment_from_html(html, /*untrusted=*/ true);

if (svg || mathml) {
node = /** @type {Element} */ (get_first_child(node));
Expand Down
25 changes: 23 additions & 2 deletions packages/svelte/src/internal/client/dom/reconciler.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,27 @@
/** @import { TrustedTypePolicy } from 'trusted-types' */

const policy = /* @__PURE__ */ globalThis?.window?.trustedTypes?.createPolicy(
'svelte-trusted-html',
{
/** @param {string} html */
createHTML: (html) => {
return html;
}
}
);

/** @param {string} html */
export function create_fragment_from_html(html) {
function create_trusted_html(html) {
return /** @type {string} */ (policy?.createHTML(html) ?? html);
}

/**
* @param {string} html
* @param {boolean} untrusted
*/
export function create_fragment_from_html(html, untrusted = false) {
var elem = document.createElement('template');
elem.innerHTML = html.replaceAll('<!>', '<!---->'); // XHTML compliance
html = html.replaceAll('<!>', '<!---->'); // XHTML compliance
elem.innerHTML = untrusted ? html : create_trusted_html(html);
return elem.content;
}
8 changes: 8 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.