Update serialize-doctype.ts

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

View File

@ -4,7 +4,7 @@ const htmlEscape = (str: string | null): string => {
if (!str) if (!str)
return ''; return '';
const escaped = []; let escaped: string = '';
str.split('').forEach(char => { str.split('').forEach(char => {
if (char == '&') { if (char == '&') {
char = '&'; char = '&';
@ -17,13 +17,14 @@ const htmlEscape = (str: string | null): string => {
} else if (char == '>') { } else if (char == '>') {
char = '>'; char = '>';
} }
escaped.push(char); escaped += char;
}); });
return escaped.join(''); return escaped;
} }
const docRule: TrustedTypePolicyOptions = { const docRule: TrustedTypePolicyOptions = {
// @ts-ignore
createHTML: (ignored: string, doctype?: DocumentType | null): string => { createHTML: (ignored: string, doctype?: DocumentType | null): string => {
if (!doctype) if (!doctype)
return '<html></html>'; return '<html></html>';
@ -37,13 +38,11 @@ const docRule: TrustedTypePolicyOptions = {
} }
} }
let doctypePolicy: TrustedTypePolicy | TrustedTypePolicyOptions; let doctypePolicy: TrustedTypePolicy | TrustedTypePolicyOptions = docRule;
if ((window as any).trustedTypes) { if ((window as any).trustedTypes) {
doctypePolicy = (window as any).trustedTypes.createPolicy('html2canvas', docRule); doctypePolicy = (window as any).trustedTypes.createPolicy('html2canvas', docRule);
} else {
doctypePolicy = docRule;
} }
export const serializeDoctype = (doctype?: DocumentType | null): string => { export const serializeDoctype = (doctype?: DocumentType | null): string => {
doctypePolicy.createHTML('', doctype); return doctypePolicy.createHTML('', doctype);
} }