Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: add <base> to fix relative error in iframe #2961

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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 @@ -635,3 +641,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);
};