From 2f09b86b27a4576fc0b3e5a71023aca750663401 Mon Sep 17 00:00:00 2001 From: Yaroslav Bolyukin Date: Sun, 26 Apr 2026 04:18:10 +0200 Subject: [PATCH] feat: support trusted-types api --- packages/fresh/src/runtime/client/partials.ts | 28 ++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/packages/fresh/src/runtime/client/partials.ts b/packages/fresh/src/runtime/client/partials.ts index 008e23a9592..29698ebb2df 100644 --- a/packages/fresh/src/runtime/client/partials.ts +++ b/packages/fresh/src/runtime/client/partials.ts @@ -24,6 +24,29 @@ import type { PartialStateJson } from "../server/preact_hooks.ts"; import { parse } from "../../jsonify/parse.ts"; import { INTERNAL_PREFIX, PARTIAL_SEARCH_PARAM } from "../../constants.ts"; +type FreshTrustedTypes = { createHTML(s: string): string }; +type TrustedTypesWindow = { + trustedTypes?: { + createPolicy(name: string, v: FreshTrustedTypes): FreshTrustedTypes; + }; +}; + +const freshTrustedTypes: FreshTrustedTypes = (() => { + const noop = { createHTML: (s: string) => s }; + const window = globalThis as TrustedTypesWindow; + if (!window.trustedTypes) return noop; + try { + return window.trustedTypes.createPolicy( + "fresh-partials", + { + createHTML: (s: string) => s, + }, + ); + } catch (_e) { + return noop; + } +})(); + export const PARTIAL_ATTR = "f-partial"; class NoPartialsError extends Error {} @@ -402,7 +425,10 @@ export async function applyPartials(res: Response): Promise { const id = res.headers.get("X-Fresh-Id"); const resText = await res.text(); - const doc = new DOMParser().parseFromString(resText, "text/html") as Document; + const doc = new DOMParser().parseFromString( + freshTrustedTypes.createHTML(resText), + "text/html", + ) as Document; const state = doc.querySelector(`#__FRSH_STATE_${id}`); let allProps: DeserializedProps = [];