From 8e52770f04de82871a9883f6f809af9fc98a9595 Mon Sep 17 00:00:00 2001 From: Jun Date: Thu, 24 Feb 2022 16:48:00 -0800 Subject: [PATCH] Update serialize-doctype.ts --- src/dom/serialize-doctype.ts | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/dom/serialize-doctype.ts b/src/dom/serialize-doctype.ts index 74bf680..2406001 100644 --- a/src/dom/serialize-doctype.ts +++ b/src/dom/serialize-doctype.ts @@ -23,9 +23,7 @@ const htmlEscape = (str: string | null): string => { return escaped; } -const docRule: TrustedTypePolicyOptions = { - // @ts-ignore - createHTML: (ignored: string, doctype?: DocumentType | null): string => { +const createDocType = (ignored: string, doctype?: DocumentType | null): string => { if (!doctype) return ''; @@ -33,16 +31,21 @@ const docRule: TrustedTypePolicyOptions = { const internalSubset = htmlEscape(doctype.internalSubset); const publicId = `"${htmlEscape(doctype.publicId)}"`; const systemId = `"${htmlEscape(doctype.systemId)}"`; - + return ``; +} + +let doctypePolicy: TrustedTypePolicy; +if ((window as any).trustedTypes) { + doctypePolicy = (window as any).trustedTypes.createPolicy('html2canvas', { + createHTML: createDocType + }); +} + +export const serializeDoctype = (doctype?: DocumentType | null): string | TrustedHTML => { + if (doctypePolicy !== undefined) { + return doctypePolicy.createHTML('', doctype); + } else { + return createDocType('', doctype); } } - -let doctypePolicy: TrustedTypePolicy | TrustedTypePolicyOptions = docRule; -if ((window as any).trustedTypes) { - doctypePolicy = (window as any).trustedTypes.createPolicy('html2canvas', docRule); -} - -export const serializeDoctype = (doctype?: DocumentType | null): string => { - return doctypePolicy.createHTML('', doctype); -}