Update serialize-doctype.ts

This commit is contained in:
Jun 2022-02-24 16:48:00 -08:00 committed by GitHub
parent 89998e258f
commit 8e52770f04
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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 '<html></html>';
@ -33,16 +31,21 @@ const docRule: TrustedTypePolicyOptions = {
const internalSubset = htmlEscape(doctype.internalSubset);
const publicId = `"${htmlEscape(doctype.publicId)}"`;
const systemId = `"${htmlEscape(doctype.systemId)}"`;
return `<!DOCTYPE ${name}${internalSubset}${publicId}${systemId}><html></html>`;
}
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);
}