Skip to content

Commit

Permalink
Reuse single TrustedTypePolicy object
Browse files Browse the repository at this point in the history
FIX: Don't recreated `TrustedTypePolicy` objects for every paste.

Issue ProseMirror/prosemirror#1493
  • Loading branch information
marijnh committed Nov 5, 2024
1 parent 65dcfa5 commit efd8767
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/clipboard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,16 @@ function detachedDoc() {
return _detachedDoc || (_detachedDoc = document.implementation.createHTMLDocument("title"))
}

let _policy: any = null

function maybeWrapTrusted(html: string): string {
let trustedTypes = (window as any).trustedTypes
if (!trustedTypes) return html
// With the require-trusted-types-for CSP, Chrome will block
// innerHTML, even on a detached document. This wraps the string in
// a way that makes the browser allow us to use its parser again.
return trustedTypes.createPolicy("detachedDocument", {createHTML: (s: string) => s}).createHTML(html)
if (!_policy) _policy = trustedTypes.createPolicy("detachedDocument", {createHTML: (s: string) => s})
return _policy.createHTML(html)
}

function readHTML(html: string) {
Expand Down

0 comments on commit efd8767

Please sign in to comment.