Skip to content

Commit

Permalink
based off of niklasvh#2961 with changes
Browse files Browse the repository at this point in the history
calling adoptNode before document.open() caused the snapshot to get cropped, so adapt the pr to only get the baseURI first, then call adoptNode and add the <base> element to it afterwards

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 10, 2024
2 parents 96064e5 + e078be6 commit e01268f
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,24 @@ export class DocumentCloner {
return iframe;
});

/**
* The baseURI of the document will be lost after documentClone.open().
* We can avoid it by adding <base> element.
* */
const baseURI = documentClone.baseURI;

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);

const adoptedNode = documentClone.adoptNode(this.documentElement);
const baseNode = documentClone.createElement('base');
baseNode.href = baseURI;
const headEle = adoptedNode.getElementsByTagName('head').item(0);
headEle?.insertBefore(baseNode, headEle?.firstChild ?? null);

documentClone.replaceChild(adoptedNode, documentClone.documentElement);
documentClone.close();

return iframeLoad;
Expand Down

0 comments on commit e01268f

Please sign in to comment.