From a8d4d4b9ff1076e0755fa357870845323ae6c4a7 Mon Sep 17 00:00:00 2001
From: hackademix <giorgio@maone.net>
Date: Sun, 2 Sep 2018 18:06:36 +0200
Subject: Fixes serialization of modified HTML documents erases DOCTYPE and
 root element information, possibly causing rendering issues.

---
 main_background.js | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/main_background.js b/main_background.js
index e2c3635..8e4665a 100644
--- a/main_background.js
+++ b/main_background.js
@@ -930,6 +930,22 @@ async function handle_script(response, whitelisted){
 	return Array.isArray(edited) ? edited[0] : edited;
 }
 
+/**
+* Serializes HTMLDocument objects including the root element and 
+*	the DOCTYPE declaration
+*/
+function doc2HTML(doc) {
+	let s = doc.documentElement.outerHTML;
+	if (doc.doctype) {
+		let dt = doc.doctype;
+		let sDoctype = `<!DOCTYPE ${dt.name || "html"}`;
+		if (dt.publicId) sDoctype += ` PUBLIC "${dt.publicId}"`;
+		if (dt.systemId) sDoctype += ` "${dt.systemId}"`;
+		s = `${sDoctype}>\n${s}`;
+	}
+	return s;
+}
+
 /**
 *	Removes noscript tags with name "librejs-path" leaving the inner content to load.
 */
@@ -940,7 +956,7 @@ function remove_noscripts(html_doc){
 		}
 	}
 	
-	return html_doc.documentElement.innerHTML;
+	return doc2HTML(html_doc);
 }
 
 /**
-- 
cgit v1.2.3