Преглед изворни кода

added a way to omit some end tags

Gildas пре 7 година
родитељ
комит
1959296dd5
1 измењених фајлова са 20 додато и 1 уклоњено
  1. 20 1
      lib/single-file/serializer.js

+ 20 - 1
lib/single-file/serializer.js

@@ -23,6 +23,21 @@
 this.serializer = this.serializer || (() => {
 this.serializer = this.serializer || (() => {
 
 
 	const SELF_CLOSED_TAG_NAMES = ["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"];
 	const SELF_CLOSED_TAG_NAMES = ["area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr"];
+	const OMITTED_END_TAGS = [
+		{ tagName: "li", followings: ["li"] },
+		{ tagName: "dt", followings: ["dt", "dd"] },
+		{ tagName: "dd", followings: ["dt", "dd"] },
+		{ tagName: "rt", followings: ["rt", "rp"] },
+		{ tagName: "rp", followings: ["rt", "rp"] },
+		{ tagName: "optgroup", followings: ["optgroup"] },
+		{ tagName: "option", followings: ["option", "optgroup"] },
+		{ tagName: "thead", followings: ["tbody", "tfoot"] },
+		{ tagName: "tbody", followings: ["tbody", "tfoot"] },
+		{ tagName: "tfoot" },
+		{ tagName: "tr", followings: ["tr"] },
+		{ tagName: "td", followings: ["td", "th"] },
+		{ tagName: "th", followings: ["td", "th"] },
+	];
 
 
 	return {
 	return {
 		process(doc, compressHTML) {
 		process(doc, compressHTML) {
@@ -102,7 +117,11 @@ this.serializer = this.serializer || (() => {
 		});
 		});
 		content += ">";
 		content += ">";
 		Array.from(element.childNodes).forEach(childNode => content += serialize(childNode));
 		Array.from(element.childNodes).forEach(childNode => content += serialize(childNode));
-		if (!SELF_CLOSED_TAG_NAMES.includes(tagName)) {
+		const omittedEndTag = OMITTED_END_TAGS.find(omittedEndTag => {
+			const nextSibling = element.nextSibling;
+			return tagName == omittedEndTag.tagName && (!nextSibling || (nextSibling.nodeType == Node.ELEMENT_NODE && omittedEndTag.followings && omittedEndTag.followings.includes(nextSibling.tagName)));
+		});
+		if (!omittedEndTag && !SELF_CLOSED_TAG_NAMES.includes(tagName)) {
 			content += "</" + tagName + ">";
 			content += "</" + tagName + ">";
 		}
 		}
 		return content;
 		return content;