Pārlūkot izejas kodu

defined regexps as global constants

Gildas 7 gadi atpakaļ
vecāks
revīzija
fb8178f46f
1 mainītis faili ar 6 papildinājumiem un 2 dzēšanām
  1. 6 2
      lib/single-file/html-minifier.js

+ 6 - 2
lib/single-file/html-minifier.js

@@ -121,6 +121,10 @@ this.htmlmini = this.htmlmini || (() => {
 		}
 	};
 
+	const REGEXP_WHITESPACE = /[ \t\f\r]+/g;
+	const REGEXP_NEWLINE = /[\n]+/g;
+	const REGEXP_ENDS_WHITESPACE = /^\s+$/;
+
 	const modules = [
 		collapseBooleanAttributes,
 		mergeTextNodes,
@@ -197,7 +201,7 @@ this.htmlmini = this.htmlmini || (() => {
 				noWhitespace = element && noWhitespaceCollapse(element);
 			}
 			if ((!element || noWhitespace) && textContent.length > 1) {
-				node.textContent = textContent.replace(/[ \t\f\r]+/g, getWhiteSpace(node)).replace(/[\n]+/g, getWhiteSpace(node));
+				node.textContent = textContent.replace(REGEXP_WHITESPACE, getWhiteSpace(node)).replace(REGEXP_NEWLINE, getWhiteSpace(node));
 			}
 		}
 	}
@@ -221,7 +225,7 @@ this.htmlmini = this.htmlmini || (() => {
 			Array.from(node.attributes).forEach(attribute => {
 				if (safeToRemoveAttrs.includes(attribute.name.toLowerCase())) {
 					const attributeValue = node.getAttribute(attribute.name);
-					if (attributeValue == "" || (attributeValue || "").match(/^\s+$/)) {
+					if (attributeValue == "" || (attributeValue || "").match(REGEXP_ENDS_WHITESPACE)) {
 						node.removeAttribute(attribute.name);
 					}
 				}