From 185ea7b9f5435cb782a44b23a77aa543ba021df6 Mon Sep 17 00:00:00 2001 From: Jun Date: Thu, 24 Feb 2022 14:59:51 -0800 Subject: [PATCH 01/14] Update package.json --- package.json | 1 + 1 file changed, 1 insertion(+) diff --git a/package.json b/package.json index cefd436..38b6b3e 100644 --- a/package.json +++ b/package.json @@ -41,6 +41,7 @@ "@types/node": "^16.3.1", "@types/platform": "^1.3.4", "@types/promise-polyfill": "^6.0.3", + "@types/trusted-types": "^2.0.2", "@typescript-eslint/eslint-plugin": "^4.28.2", "@typescript-eslint/parser": "^4.28.2", "appium-ios-simulator": "^3.10.0", From 9103ae2f668faafec85eb710b6db876906014c35 Mon Sep 17 00:00:00 2001 From: Jun Date: Thu, 24 Feb 2022 15:51:22 -0800 Subject: [PATCH 02/14] Create serialize-doctype.ts --- src/dom/serialize-doctype.ts | 49 ++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 src/dom/serialize-doctype.ts diff --git a/src/dom/serialize-doctype.ts b/src/dom/serialize-doctype.ts new file mode 100644 index 0000000..b49899e --- /dev/null +++ b/src/dom/serialize-doctype.ts @@ -0,0 +1,49 @@ +/// + +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 ''; + + const name = htmlEscape(doctype.name); + const internalSubset = htmlEscape(doctype.internalSubset); + const publicId = `"${htmlEscape(doctype.publicId)}"`; + const systemId = `"${htmlEscape(doctype.systemId)}"`; + + return ``; + } +} + +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); +} From d0e63a8f56bd84d655c15e7b2feb2e8f3bb0d38b Mon Sep 17 00:00:00 2001 From: Jun Date: Thu, 24 Feb 2022 15:54:15 -0800 Subject: [PATCH 03/14] Update document-cloner.ts --- src/dom/document-cloner.ts | 29 ++--------------------------- 1 file changed, 2 insertions(+), 27 deletions(-) diff --git a/src/dom/document-cloner.ts b/src/dom/document-cloner.ts index 08faa29..310f84d 100644 --- a/src/dom/document-cloner.ts +++ b/src/dom/document-cloner.ts @@ -24,6 +24,7 @@ import {CSSParsedCounterDeclaration, CSSParsedPseudoDeclaration} from '../css/in import {getQuote} from '../css/property-descriptors/quotes'; import {Context} from '../core/context'; import {DebuggerType, isDebugging} from '../core/debugger'; +import {serializeDoctype} from './serialize-doctype'; export interface CloneOptions { ignoreElements?: (element: Element) => boolean; @@ -130,7 +131,7 @@ export class DocumentCloner { }); documentClone.open(); - documentClone.write(`${serializeDoctype(document.doctype)}`); + documentClone.write(serializeDoctype(document.doctype)); // Chrome scrolls the parent document for some reason after the write to the cloned window??? restoreOwnerScroll(this.referenceElement.ownerDocument, scrollX, scrollY); documentClone.replaceChild(documentClone.adoptNode(this.documentElement), documentClone.documentElement); @@ -568,32 +569,6 @@ export const copyCSSStyles = (style: CSSStyl return target; }; -const serializeDoctype = (doctype?: DocumentType | null): string => { - let str = ''; - if (doctype) { - str += ''; - } - - return str; -}; - const restoreOwnerScroll = (ownerDocument: Document | null, x: number, y: number) => { if ( ownerDocument && From 89998e258fdf621ab89b91a709ae3ecfb015a31d Mon Sep 17 00:00:00 2001 From: Jun Date: Thu, 24 Feb 2022 16:29:46 -0800 Subject: [PATCH 04/14] Update serialize-doctype.ts --- src/dom/serialize-doctype.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/dom/serialize-doctype.ts b/src/dom/serialize-doctype.ts index b49899e..74bf680 100644 --- a/src/dom/serialize-doctype.ts +++ b/src/dom/serialize-doctype.ts @@ -4,7 +4,7 @@ const htmlEscape = (str: string | null): string => { if (!str) return ''; - const escaped = []; + let escaped: string = ''; str.split('').forEach(char => { if (char == '&') { char = '&'; @@ -17,13 +17,14 @@ const htmlEscape = (str: string | null): string => { } else if (char == '>') { char = '>'; } - escaped.push(char); + escaped += char; }); - return escaped.join(''); + return escaped; } const docRule: TrustedTypePolicyOptions = { + // @ts-ignore createHTML: (ignored: string, doctype?: DocumentType | null): string => { if (!doctype) return ''; @@ -37,13 +38,11 @@ const docRule: TrustedTypePolicyOptions = { } } -let doctypePolicy: TrustedTypePolicy | TrustedTypePolicyOptions; +let doctypePolicy: TrustedTypePolicy | TrustedTypePolicyOptions = docRule; 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); + return doctypePolicy.createHTML('', doctype); } From 8e52770f04de82871a9883f6f809af9fc98a9595 Mon Sep 17 00:00:00 2001 From: Jun Date: Thu, 24 Feb 2022 16:48:00 -0800 Subject: [PATCH 05/14] 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); -} From 4b7e8e03b239b7f2658750e9ffa6f29850d3904a Mon Sep 17 00:00:00 2001 From: Jun Date: Thu, 24 Feb 2022 16:48:58 -0800 Subject: [PATCH 06/14] Update serialize-doctype.ts --- src/dom/serialize-doctype.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/dom/serialize-doctype.ts b/src/dom/serialize-doctype.ts index 2406001..ade0e9e 100644 --- a/src/dom/serialize-doctype.ts +++ b/src/dom/serialize-doctype.ts @@ -23,6 +23,7 @@ const htmlEscape = (str: string | null): string => { return escaped; } +// @ts-ignore const createDocType = (ignored: string, doctype?: DocumentType | null): string => { if (!doctype) return ''; From 2b805975410a5f2e08bdb8ee34c15aa80de0c80b Mon Sep 17 00:00:00 2001 From: Jun Date: Thu, 24 Feb 2022 16:50:05 -0800 Subject: [PATCH 07/14] Update document-cloner.ts --- src/dom/document-cloner.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/dom/document-cloner.ts b/src/dom/document-cloner.ts index 310f84d..49b3993 100644 --- a/src/dom/document-cloner.ts +++ b/src/dom/document-cloner.ts @@ -131,7 +131,7 @@ export class DocumentCloner { }); documentClone.open(); - documentClone.write(serializeDoctype(document.doctype)); + documentClone.write(serializeDoctype(document.doctype) as unknown as string); // Chrome scrolls the parent document for some reason after the write to the cloned window??? restoreOwnerScroll(this.referenceElement.ownerDocument, scrollX, scrollY); documentClone.replaceChild(documentClone.adoptNode(this.documentElement), documentClone.documentElement); From 50d61960c2dc1c8af08e973f9e90696d53f60cc2 Mon Sep 17 00:00:00 2001 From: Jun Date: Thu, 24 Feb 2022 21:06:32 -0800 Subject: [PATCH 08/14] Update serialize-doctype.ts --- src/dom/serialize-doctype.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/dom/serialize-doctype.ts b/src/dom/serialize-doctype.ts index ade0e9e..84c17a8 100644 --- a/src/dom/serialize-doctype.ts +++ b/src/dom/serialize-doctype.ts @@ -30,8 +30,8 @@ const createDocType = (ignored: string, doctype?: DocumentType | null): string = const name = htmlEscape(doctype.name); const internalSubset = htmlEscape(doctype.internalSubset); - const publicId = `"${htmlEscape(doctype.publicId)}"`; - const systemId = `"${htmlEscape(doctype.systemId)}"`; + const publicId = doctype.publicId ? `"${htmlEscape(doctype.publicId)}"` : ''; + const systemId = doctype.systemId ? `"${htmlEscape(doctype.systemId)}"` : ''; return ``; } From a6fee57c11fc238228abe9322e854d9c3a4d7ddb Mon Sep 17 00:00:00 2001 From: Jun Date: Fri, 25 Feb 2022 14:59:30 -0800 Subject: [PATCH 09/14] Update serialize-doctype.ts --- src/dom/serialize-doctype.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/dom/serialize-doctype.ts b/src/dom/serialize-doctype.ts index 84c17a8..91dde8e 100644 --- a/src/dom/serialize-doctype.ts +++ b/src/dom/serialize-doctype.ts @@ -23,8 +23,7 @@ const htmlEscape = (str: string | null): string => { return escaped; } -// @ts-ignore -const createDocType = (ignored: string, doctype?: DocumentType | null): string => { +const createDocType = (doctype?: DocumentType | null): string => { if (!doctype) return ''; @@ -39,7 +38,9 @@ const createDocType = (ignored: string, doctype?: DocumentType | null): string = let doctypePolicy: TrustedTypePolicy; if ((window as any).trustedTypes) { doctypePolicy = (window as any).trustedTypes.createPolicy('html2canvas', { - createHTML: createDocType + createHTML: (ignored: string, doctype?: DocumentType | null): TrustedHTML => { + return createDocType(doctype); + } }); } @@ -47,6 +48,6 @@ export const serializeDoctype = (doctype?: DocumentType | null): string | Truste if (doctypePolicy !== undefined) { return doctypePolicy.createHTML('', doctype); } else { - return createDocType('', doctype); + return createDocType(doctype); } } From b119e2a5e2a9093800b4ef32d389c5106f43b6cc Mon Sep 17 00:00:00 2001 From: Jun Date: Fri, 25 Feb 2022 15:01:53 -0800 Subject: [PATCH 10/14] Update serialize-doctype.ts --- src/dom/serialize-doctype.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/dom/serialize-doctype.ts b/src/dom/serialize-doctype.ts index 91dde8e..4b142d1 100644 --- a/src/dom/serialize-doctype.ts +++ b/src/dom/serialize-doctype.ts @@ -38,7 +38,8 @@ const createDocType = (doctype?: DocumentType | null): string => { let doctypePolicy: TrustedTypePolicy; if ((window as any).trustedTypes) { doctypePolicy = (window as any).trustedTypes.createPolicy('html2canvas', { - createHTML: (ignored: string, doctype?: DocumentType | null): TrustedHTML => { + // @ts-ignore + createHTML: (ignored: string, doctype?: DocumentType | null): string => { return createDocType(doctype); } }); From 33b3f272b086b18b4fee945f977bf5e301382edb Mon Sep 17 00:00:00 2001 From: Jun Date: Thu, 12 Jan 2023 09:45:00 -0800 Subject: [PATCH 11/14] Update package.json --- package.json | 1 - 1 file changed, 1 deletion(-) diff --git a/package.json b/package.json index 38b6b3e..cefd436 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,6 @@ "@types/node": "^16.3.1", "@types/platform": "^1.3.4", "@types/promise-polyfill": "^6.0.3", - "@types/trusted-types": "^2.0.2", "@typescript-eslint/eslint-plugin": "^4.28.2", "@typescript-eslint/parser": "^4.28.2", "appium-ios-simulator": "^3.10.0", From 90ef576ad95a68e832a13923afbedfac3d9596f3 Mon Sep 17 00:00:00 2001 From: Jun Date: Thu, 12 Jan 2023 09:45:40 -0800 Subject: [PATCH 12/14] Delete serialize-doctype.ts --- src/dom/serialize-doctype.ts | 54 ------------------------------------ 1 file changed, 54 deletions(-) delete mode 100644 src/dom/serialize-doctype.ts diff --git a/src/dom/serialize-doctype.ts b/src/dom/serialize-doctype.ts deleted file mode 100644 index 4b142d1..0000000 --- a/src/dom/serialize-doctype.ts +++ /dev/null @@ -1,54 +0,0 @@ -/// - -const htmlEscape = (str: string | null): string => { - if (!str) - return ''; - - let escaped: string = ''; - str.split('').forEach(char => { - if (char == '&') { - char = '&'; - } else if (char == '\'') { - char = '''; - } else if (char == '"') { - char = '"'; - } else if (char == '<') { - char = '<'; - } else if (char == '>') { - char = '>'; - } - escaped += char; - }); - - return escaped; -} - -const createDocType = (doctype?: DocumentType | null): string => { - if (!doctype) - return ''; - - const name = htmlEscape(doctype.name); - const internalSubset = htmlEscape(doctype.internalSubset); - const publicId = doctype.publicId ? `"${htmlEscape(doctype.publicId)}"` : ''; - const systemId = doctype.systemId ? `"${htmlEscape(doctype.systemId)}"` : ''; - - return ``; -} - -let doctypePolicy: TrustedTypePolicy; -if ((window as any).trustedTypes) { - doctypePolicy = (window as any).trustedTypes.createPolicy('html2canvas', { - // @ts-ignore - createHTML: (ignored: string, doctype?: DocumentType | null): string => { - return createDocType(doctype); - } - }); -} - -export const serializeDoctype = (doctype?: DocumentType | null): string | TrustedHTML => { - if (doctypePolicy !== undefined) { - return doctypePolicy.createHTML('', doctype); - } else { - return createDocType(doctype); - } -} From 983edf6db2c0b047dfc1a5515fbb2df14181aa36 Mon Sep 17 00:00:00 2001 From: Jun Date: Thu, 12 Jan 2023 09:47:12 -0800 Subject: [PATCH 13/14] Update document-cloner.ts --- src/dom/document-cloner.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/dom/document-cloner.ts b/src/dom/document-cloner.ts index 49b3993..5263f1b 100644 --- a/src/dom/document-cloner.ts +++ b/src/dom/document-cloner.ts @@ -24,7 +24,6 @@ import {CSSParsedCounterDeclaration, CSSParsedPseudoDeclaration} from '../css/in import {getQuote} from '../css/property-descriptors/quotes'; import {Context} from '../core/context'; import {DebuggerType, isDebugging} from '../core/debugger'; -import {serializeDoctype} from './serialize-doctype'; export interface CloneOptions { ignoreElements?: (element: Element) => boolean; @@ -131,10 +130,14 @@ export class DocumentCloner { }); documentClone.open(); - documentClone.write(serializeDoctype(document.doctype) as unknown as string); + if (document.doctype && document.doctype.nodeType === Node.DOCUMENT_TYPE_NODE) { + const doctypeClone = document.doctype.cloneNode(false); + documentClone.append(doctypeClone); + } // Chrome scrolls the parent document for some reason after the write to the cloned window??? restoreOwnerScroll(this.referenceElement.ownerDocument, scrollX, scrollY); documentClone.replaceChild(documentClone.adoptNode(this.documentElement), documentClone.documentElement); + documentClone.append(documentClone.adoptNode(this.documentElement)); documentClone.close(); return iframeLoad; From bf1a93354ffdd1130d1e8fbf05d1ae21f6ff02b2 Mon Sep 17 00:00:00 2001 From: Jun Date: Thu, 12 Jan 2023 09:49:51 -0800 Subject: [PATCH 14/14] Update document-cloner.ts --- src/dom/document-cloner.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/dom/document-cloner.ts b/src/dom/document-cloner.ts index 5263f1b..1d5b8b2 100644 --- a/src/dom/document-cloner.ts +++ b/src/dom/document-cloner.ts @@ -136,7 +136,6 @@ export class DocumentCloner { } // Chrome scrolls the parent document for some reason after the write to the cloned window??? restoreOwnerScroll(this.referenceElement.ownerDocument, scrollX, scrollY); - documentClone.replaceChild(documentClone.adoptNode(this.documentElement), documentClone.documentElement); documentClone.append(documentClone.adoptNode(this.documentElement)); documentClone.close();