Use correct doctype in cloned Document (Fix #1298)

This commit is contained in:
Niklas von Hertzen 2018-01-02 19:46:46 +08:00
parent ea6062c85b
commit ae019f174c
2 changed files with 38 additions and 1 deletions

View File

@ -614,7 +614,7 @@ export const cloneWindow = (
}); });
documentClone.open(); documentClone.open();
documentClone.write('<!DOCTYPE html><html></html>'); documentClone.write(`${serializeDoctype(document.doctype)}<html></html>`);
// Chrome scrolls the parent document for some reason after the write to the cloned window??? // Chrome scrolls the parent document for some reason after the write to the cloned window???
restoreOwnerScroll(referenceElement.ownerDocument, scrollX, scrollY); restoreOwnerScroll(referenceElement.ownerDocument, scrollX, scrollY);
documentClone.replaceChild( documentClone.replaceChild(
@ -626,3 +626,29 @@ export const cloneWindow = (
return iframeLoad; return iframeLoad;
}); });
}; };
const serializeDoctype = (doctype: ?DocumentType): string => {
let str = '';
if (doctype) {
str += '<!DOCTYPE ';
if (doctype.name) {
str += doctype.name;
}
if (doctype.internalSubset) {
str += doctype.internalSubset;
}
if (doctype.publicId) {
str += `"${doctype.publicId}"`;
}
if (doctype.systemId) {
str += `"${doctype.systemId}"`;
}
str += '>';
}
return str;
};

View File

@ -0,0 +1,11 @@
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="../../test.js"></script>
</head>
<body>
<img src="../../assets/image.jpg" />
<img src="../../assets/image2.jpg" style="display:block;" />
<br>
</body>
</html>