Browse Source

extracted serializeAttribute from serializeElement

Gildas 7 years ago
parent
commit
5879a35eb9
1 changed files with 40 additions and 34 deletions
  1. 40 34
      lib/single-file/serializer.js

+ 40 - 34
lib/single-file/serializer.js

@@ -100,40 +100,7 @@ this.serializer = this.serializer || (() => {
 		let content = "";
 		if (!omittedStartTag || element.attributes.length) {
 			content = "<" + tagName;
-			Array.from(element.attributes).forEach(attribute => {
-				const name = attribute.name.replace(/["'>/=]/g, "");
-				let value = attribute.value;
-				if (name == "class") {
-					value = Array.from(element.classList).map(className => className.trim()).join(" ");
-				}
-				value = value.replace(/&/g, "&amp;").replace(/\u00a0/g, "&nbsp;").replace(/"/g, "&quot;");
-				const validUnquotedValue = value.match(/^[^ \t\n\f\r"'`=<>]+$/);
-				content += " ";
-				if (!attribute.namespace) {
-					content += name;
-				} else if (attribute.namespaceURI == "http://www.w3.org/XML/1998/namespace") {
-					content += "xml:" + name;
-				} else if (attribute.namespaceURI == "http://www.w3.org/2000/xmlns/") {
-					if (name !== "xmlns") {
-						content += "xmlns:";
-					}
-					content += name;
-				} else if (attribute.namespaceURI == "http://www.w3.org/1999/xlink") {
-					content += "xlink:" + name;
-				} else {
-					content += name;
-				}
-				if (value != "") {
-					content += "=";
-					if (!validUnquotedValue) {
-						content += "\"";
-					}
-					content += value;
-					if (!validUnquotedValue) {
-						content += "\"";
-					}
-				}
-			});
+			Array.from(element.attributes).forEach(attribute => content += serializeAttribute(attribute, element));
 			content += ">";
 		}
 		Array.from(element.childNodes).forEach(childNode => content += serialize(childNode));
@@ -147,6 +114,45 @@ this.serializer = this.serializer || (() => {
 		return content;
 	}
 
+	function serializeAttribute(attribute, element) {
+		const name = attribute.name;
+		let content = "";
+		if (!name.match(/["'>/=]/)) {
+			let value = attribute.value;
+			if (name == "class") {
+				value = Array.from(element.classList).map(className => className.trim()).join(" ");
+			}
+			value = value.replace(/&/g, "&amp;").replace(/\u00a0/g, "&nbsp;").replace(/"/g, "&quot;");
+			const validUnquotedValue = value.match(/^[^ \t\n\f\r"'`=<>]+$/);
+			content += " ";
+			if (!attribute.namespace) {
+				content += name;
+			} else if (attribute.namespaceURI == "http://www.w3.org/XML/1998/namespace") {
+				content += "xml:" + name;
+			} else if (attribute.namespaceURI == "http://www.w3.org/2000/xmlns/") {
+				if (name !== "xmlns") {
+					content += "xmlns:";
+				}
+				content += name;
+			} else if (attribute.namespaceURI == "http://www.w3.org/1999/xlink") {
+				content += "xlink:" + name;
+			} else {
+				content += name;
+			}
+			if (value != "") {
+				content += "=";
+				if (!validUnquotedValue) {
+					content += "\"";
+				}
+				content += value;
+				if (!validUnquotedValue) {
+					content += "\"";
+				}
+			}
+		}
+		return content;
+	}
+
 	function spaceFirstCharacter(textContent) {
 		return Boolean(textContent.charAt(0).match(/[\u0020\u0009\u000A\u000C\u000D]+/)); // eslint-disable-line no-control-regex
 	}