From b12f9fef0d6357f6507fda2fdb0b306116133244 Mon Sep 17 00:00:00 2001
From: MoyuScript <i@moyu.moe>
Date: Tue, 2 Jan 2018 19:46:46 +0800
Subject: [PATCH] Use correct doctype in cloned Document (Fix #1298)

---
 src/Clone.js                       | 28 +++++++++++++++++++++++++++-
 tests/reftests/images/doctype.html | 11 +++++++++++
 2 files changed, 38 insertions(+), 1 deletion(-)
 create mode 100644 tests/reftests/images/doctype.html

diff --git a/src/Clone.js b/src/Clone.js
index 22af0c4..4def893 100644
--- a/src/Clone.js
+++ b/src/Clone.js
@@ -614,7 +614,7 @@ export const cloneWindow = (
         });
 
         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???
         restoreOwnerScroll(referenceElement.ownerDocument, scrollX, scrollY);
         documentClone.replaceChild(
@@ -626,3 +626,29 @@ export const cloneWindow = (
         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;
+};
diff --git a/tests/reftests/images/doctype.html b/tests/reftests/images/doctype.html
new file mode 100644
index 0000000..76f06ea
--- /dev/null
+++ b/tests/reftests/images/doctype.html
@@ -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>