Browse Source

fxed serialization of template if compressHTML = false

Former-commit-id: e0337ba8d8d867dfda397156c4b145970bcc0e33
Gildas 6 years ago
parent
commit
b00a7d1205
2 changed files with 11 additions and 11 deletions
  1. 10 10
      lib/single-file/modules/html-serializer.js
  2. 1 1
      lib/single-file/single-file-core.js

+ 10 - 10
lib/single-file/modules/html-serializer.js

@@ -73,17 +73,17 @@ this.singlefile.lib.modules.serializer = this.singlefile.lib.modules.serializer
 					docTypeString += " [" + docType.internalSubset + "]";
 				docTypeString += "> ";
 			}
-			return docTypeString + (compressHTML ? serialize(doc.documentElement) : doc.documentElement.outerHTML);
+			return docTypeString + serialize(doc.documentElement, compressHTML);
 		}
 	};
 
-	function serialize(node) {
+	function serialize(node, compressHTML) {
 		if (node.nodeType == Node_TEXT_NODE) {
 			return serializeTextNode(node);
 		} else if (node.nodeType == Node_COMMENT_NODE) {
 			return serializeCommentNode(node);
 		} else if (node.nodeType == Node_ELEMENT_NODE) {
-			return serializeElement(node);
+			return serializeElement(node, compressHTML);
 		}
 	}
 
@@ -107,24 +107,24 @@ this.singlefile.lib.modules.serializer = this.singlefile.lib.modules.serializer
 		return "<!--" + commentNode.textContent + "-->";
 	}
 
-	function serializeElement(element) {
+	function serializeElement(element, compressHTML) {
 		const tagName = element.tagName.toLowerCase();
-		const omittedStartTag = OMITTED_START_TAGS.find(omittedStartTag => tagName == omittedStartTag.tagName && omittedStartTag.accept(element));
+		const omittedStartTag = compressHTML && OMITTED_START_TAGS.find(omittedStartTag => tagName == omittedStartTag.tagName && omittedStartTag.accept(element));
 		let content = "";
 		if (!omittedStartTag || element.attributes.length) {
 			content = "<" + tagName;
-			Array.from(element.attributes, attribute => content += serializeAttribute(attribute, element));
+			Array.from(element.attributes, attribute => content += serializeAttribute(attribute, element, compressHTML));
 			content += ">";
 		}
 		Array.from(element.childNodes, childNode => content += serialize(childNode));
-		const omittedEndTag = OMITTED_END_TAGS.find(omittedEndTag => tagName == omittedEndTag.tagName && omittedEndTag.accept(element.nextSibling, element));
+		const omittedEndTag = compressHTML && OMITTED_END_TAGS.find(omittedEndTag => tagName == omittedEndTag.tagName && omittedEndTag.accept(element.nextSibling, element));
 		if (!omittedEndTag && !SELF_CLOSED_TAG_NAMES.includes(tagName)) {
 			content += "</" + tagName + ">";
 		}
 		return content;
 	}
 
-	function serializeAttribute(attribute, element) {
+	function serializeAttribute(attribute, element, compressHTML) {
 		const name = attribute.name;
 		let content = "";
 		if (!name.match(/["'>/=]/)) {
@@ -135,13 +135,13 @@ this.singlefile.lib.modules.serializer = this.singlefile.lib.modules.serializer
 			let simpleQuotesValue;
 			value = value.replace(/&/g, "&amp;").replace(/\u00a0/g, "&nbsp;");
 			if (value.includes("\"")) {
-				if (value.includes("'")) {
+				if (value.includes("'") || compressHTML) {
 					value = value.replace(/"/g, "&quot;");
 				} else {
 					simpleQuotesValue = true;
 				}
 			}
-			const invalidUnquotedValue = !value.match(/^[^ \t\n\f\r'"`=<>]+$/);
+			const invalidUnquotedValue = !compressHTML || !value.match(/^[^ \t\n\f\r'"`=<>]+$/);
 			content += " ";
 			if (!attribute.namespace) {
 				content += name;

+ 1 - 1
lib/single-file/single-file-core.js

@@ -171,7 +171,7 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 			if (this.options.saveRawPage) {
 				this.options.removeFrames = true;
 			}
-			this.options.content = this.options.content || (this.options.doc ? util.serialize(this.options.doc, false) : null);
+			this.options.content = this.options.content || (this.options.doc ? util.serialize(this.options.doc) : null);
 			this.onprogress = options.onprogress || (() => { });
 		}