mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
Create serialize-doctype.ts
This commit is contained in:
parent
185ea7b9f5
commit
9103ae2f66
49
src/dom/serialize-doctype.ts
Normal file
49
src/dom/serialize-doctype.ts
Normal file
@ -0,0 +1,49 @@
|
||||
/// <reference types="trusted-types"/>
|
||||
|
||||
const htmlEscape = (str: string | null): string => {
|
||||
if (!str)
|
||||
return '';
|
||||
|
||||
const escaped = [];
|
||||
str.split('').forEach(char => {
|
||||
if (char == '&') {
|
||||
char = '&';
|
||||
} else if (char == '\'') {
|
||||
char = ''';
|
||||
} else if (char == '"') {
|
||||
char = '"';
|
||||
} else if (char == '<') {
|
||||
char = '<';
|
||||
} else if (char == '>') {
|
||||
char = '>';
|
||||
}
|
||||
escaped.push(char);
|
||||
});
|
||||
|
||||
return escaped.join('');
|
||||
}
|
||||
|
||||
const docRule: TrustedTypePolicyOptions = {
|
||||
createHTML: (ignored: string, doctype?: DocumentType | null): string => {
|
||||
if (!doctype)
|
||||
return '<html></html>';
|
||||
|
||||
const name = htmlEscape(doctype.name);
|
||||
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 | TrustedTypePolicyOptions;
|
||||
if ((window as any).trustedTypes) {
|
||||
doctypePolicy = (window as any).trustedTypes.createPolicy('html2canvas', docRule);
|
||||
} else {
|
||||
doctypePolicy = docRule;
|
||||
}
|
||||
|
||||
export const serializeDoctype = (doctype?: DocumentType | null): string => {
|
||||
doctypePolicy.createHTML('', doctype);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user