Просмотр исходного кода

remove invalid cahrs from attribute names

Gildas 7 лет назад
Родитель
Сommit
97cc3724a0
1 измененных файлов с 9 добавлено и 8 удалено
  1. 9 8
      lib/single-file/serializer.js

+ 9 - 8
lib/single-file/serializer.js

@@ -85,26 +85,27 @@ this.serializer = this.serializer || (() => {
 		const tagName = element.tagName.toLowerCase();
 		const tagName = element.tagName.toLowerCase();
 		let content = "<" + tagName;
 		let content = "<" + tagName;
 		Array.from(element.attributes).forEach(attribute => {
 		Array.from(element.attributes).forEach(attribute => {
+			const name = attribute.name.replace(/["'>/=]/g, "");
 			let value = attribute.value;
 			let value = attribute.value;
-			if (attribute.name == "class") {
+			if (name == "class") {
 				value = Array.from(element.classList).map(className => className.trim()).join(" ");
 				value = Array.from(element.classList).map(className => className.trim()).join(" ");
 			}
 			}
 			value = value.replace(/&/g, "&amp;").replace(/\u00a0/g, "&nbsp;").replace(/"/g, "&quot;");
 			value = value.replace(/&/g, "&amp;").replace(/\u00a0/g, "&nbsp;").replace(/"/g, "&quot;");
 			const validUnquotedValue = value.match(/^[^ \t\n\f\r"'`=<>]+$/);
 			const validUnquotedValue = value.match(/^[^ \t\n\f\r"'`=<>]+$/);
 			content += " ";
 			content += " ";
 			if (!attribute.namespace) {
 			if (!attribute.namespace) {
-				content += attribute.name;
+				content += name;
 			} else if (attribute.namespaceURI == "http://www.w3.org/XML/1998/namespace") {
 			} else if (attribute.namespaceURI == "http://www.w3.org/XML/1998/namespace") {
-				content += "xml:" + attribute.name;
+				content += "xml:" + name;
 			} else if (attribute.namespaceURI == "http://www.w3.org/2000/xmlns/") {
 			} else if (attribute.namespaceURI == "http://www.w3.org/2000/xmlns/") {
-				if (attribute.name !== "xmlns") {
+				if (name !== "xmlns") {
 					content += "xmlns:";
 					content += "xmlns:";
 				}
 				}
-				content += attribute.name;
+				content += name;
 			} else if (attribute.namespaceURI == "http://www.w3.org/1999/xlink") {
 			} else if (attribute.namespaceURI == "http://www.w3.org/1999/xlink") {
-				content += "xlink:" + attribute.name;
+				content += "xlink:" + name;
 			} else {
 			} else {
-				content += attribute.name;
+				content += name;
 			}
 			}
 			if (value != "") {
 			if (value != "") {
 				content += "=";
 				content += "=";
@@ -121,7 +122,7 @@ this.serializer = this.serializer || (() => {
 		Array.from(element.childNodes).forEach(childNode => content += serialize(childNode));
 		Array.from(element.childNodes).forEach(childNode => content += serialize(childNode));
 		const omittedEndTag = OMITTED_END_TAGS.find(omittedEndTag => {
 		const omittedEndTag = OMITTED_END_TAGS.find(omittedEndTag => {
 			const nextSibling = element.nextSibling;
 			const nextSibling = element.nextSibling;
-			return tagName == omittedEndTag.tagName && (!nextSibling || (nextSibling.nodeType == Node.ELEMENT_NODE && omittedEndTag.followings && omittedEndTag.followings.includes(nextSibling.tagName)));
+			return tagName == omittedEndTag.tagName && (!nextSibling || (nextSibling.nodeType == Node.ELEMENT_NODE && omittedEndTag.followings && omittedEndTag.followings.includes(nextSibling.tagName.toLowerCase())));
 		});
 		});
 		if (!omittedEndTag && !SELF_CLOSED_TAG_NAMES.includes(tagName)) {
 		if (!omittedEndTag && !SELF_CLOSED_TAG_NAMES.includes(tagName)) {
 			content += "</" + tagName + ">";
 			content += "</" + tagName + ">";