mirror of
https://github.com/niklasvh/html2canvas.git
synced 2023-08-10 21:13:10 +03:00
fix: add <base> to fix relative error in iframe
This commit is contained in:
parent
6020386bbe
commit
41fb73e83e
2
package-lock.json
generated
2
package-lock.json
generated
@ -5,7 +5,7 @@
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"version": "1.4.0",
|
||||
"version": "1.4.1",
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"css-line-break": "^2.1.0",
|
||||
|
@ -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;
|
||||
@ -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);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user