[trustedTypes]: Add TrustedTypes support to Svelte

pull/16271/head
Jay Harris 2 months ago
parent eb530c82c4
commit 5673d60424
No known key found for this signature in database

@ -0,0 +1,5 @@
---
'svelte': minor
---
Add TrustedTypes support

@ -166,6 +166,7 @@
"@jridgewell/sourcemap-codec": "^1.5.0", "@jridgewell/sourcemap-codec": "^1.5.0",
"@sveltejs/acorn-typescript": "^1.0.5", "@sveltejs/acorn-typescript": "^1.0.5",
"@types/estree": "^1.0.5", "@types/estree": "^1.0.5",
"@types/trusted-types": "^2.0.7",
"acorn": "^8.12.1", "acorn": "^8.12.1",
"aria-query": "^5.3.1", "aria-query": "^5.3.1",
"axobject-query": "^4.1.0", "axobject-query": "^4.1.0",

@ -1,6 +1,25 @@
/** @import { TrustedTypePolicy } from 'trusted-types' */
/** @type {Pick<TrustedTypePolicy, 'createHTML'> | undefined} */
let policy;
if (globalThis?.window?.trustedTypes) {
policy = globalThis.window.trustedTypes.createPolicy('svelte-trusted-html', {
/** @param {string} html */
createHTML: (html) => {
return html;
}
});
}
/** @param {string} html */
function create_trusted_html(html) {
return /** @type {string} */(policy?.createHTML(html) ?? html);
}
/** @param {string} html */ /** @param {string} html */
export function create_fragment_from_html(html) { export function create_fragment_from_html(html) {
var elem = document.createElement('template'); var elem = document.createElement('template');
elem.innerHTML = html.replaceAll('<!>', '<!---->'); // XHTML compliance elem.innerHTML = create_trusted_html(html.replaceAll('<!>', '<!---->')); // XHTML compliance
return elem.content; return elem.content;
} }

@ -71,6 +71,9 @@ importers:
'@types/estree': '@types/estree':
specifier: ^1.0.5 specifier: ^1.0.5
version: 1.0.6 version: 1.0.6
'@types/trusted-types':
specifier: ^2.0.7
version: 2.0.7
acorn: acorn:
specifier: ^8.12.1 specifier: ^8.12.1
version: 8.14.0 version: 8.14.0
@ -797,6 +800,9 @@ packages:
'@types/semver@7.5.6': '@types/semver@7.5.6':
resolution: {integrity: sha512-dn1l8LaMea/IjDoHNd9J52uBbInB796CDffS6VdIxvqYCPSG0V0DzHp76GpaWnlhg88uYyPbXCDIowa86ybd5A==} resolution: {integrity: sha512-dn1l8LaMea/IjDoHNd9J52uBbInB796CDffS6VdIxvqYCPSG0V0DzHp76GpaWnlhg88uYyPbXCDIowa86ybd5A==}
'@types/trusted-types@2.0.7':
resolution: {integrity: sha512-ScaPdn1dQczgbl0QFTeTOmVHFULt394XJgOQNoyVhZ6r2vLnMLJfBPd53SB52T/3G36VI1/g2MZaX0cwDuXsfw==}
'@typescript-eslint/eslint-plugin@8.26.0': '@typescript-eslint/eslint-plugin@8.26.0':
resolution: {integrity: sha512-cLr1J6pe56zjKYajK6SSSre6nl1Gj6xDp1TY0trpgPzjVbgDwd09v2Ws37LABxzkicmUjhEeg/fAUjPJJB1v5Q==} resolution: {integrity: sha512-cLr1J6pe56zjKYajK6SSSre6nl1Gj6xDp1TY0trpgPzjVbgDwd09v2Ws37LABxzkicmUjhEeg/fAUjPJJB1v5Q==}
engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0} engines: {node: ^18.18.0 || ^20.9.0 || >=21.1.0}
@ -3059,6 +3065,8 @@ snapshots:
'@types/semver@7.5.6': {} '@types/semver@7.5.6': {}
'@types/trusted-types@2.0.7': {}
'@typescript-eslint/eslint-plugin@8.26.0(@typescript-eslint/parser@8.26.0(eslint@9.9.1)(typescript@5.5.4))(eslint@9.9.1)(typescript@5.5.4)': '@typescript-eslint/eslint-plugin@8.26.0(@typescript-eslint/parser@8.26.0(eslint@9.9.1)(typescript@5.5.4))(eslint@9.9.1)(typescript@5.5.4)':
dependencies: dependencies:
'@eslint-community/regexpp': 4.12.1 '@eslint-community/regexpp': 4.12.1

Loading…
Cancel
Save