Skip to content

Commit

Permalink
see pr: niklasvh#2961
Browse files Browse the repository at this point in the history
Squashed commit of the following:

commit 41fb73e
Author: Frank Cheung <frank.cheung.work@outlook.com>
Date:   Sun Sep 25 13:13:31 2022 +0800

    fix: add <base> to fix relative error in iframe
  • Loading branch information
nangelina committed Feb 9, 2024
1 parent 6248c5a commit bb75a57
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/dom/document-cloner.ts
Expand Up @@ -129,11 +129,17 @@ export class DocumentCloner {
return iframe;
});

const adoptedNode = documentClone.adoptNode(this.documentElement);
/**
* The baseURI of the document will be lost after documentClone.open().
* We can avoid it by adding <base> element.
* */
addBase(adoptedNode, documentClone);
documentClone.open();
documentClone.write(`${serializeDoctype(document.doctype)}<html></html>`);
// 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.replaceChild(adoptedNode, documentClone.documentElement);
documentClone.close();

return iframeLoad;
Expand Down Expand Up @@ -654,3 +660,10 @@ const createStyles = (body: HTMLElement, styles: string) => {
body.appendChild(style);
}
};

const addBase = (targetELement: HTMLElement, referenceDocument: Document) => {
const baseNode = referenceDocument.createElement('base');
baseNode.href = referenceDocument.baseURI;
const headEle = targetELement.getElementsByTagName('head').item(0);
headEle?.insertBefore(baseNode, headEle?.firstChild ?? null);
};

0 comments on commit bb75a57

Please sign in to comment.