瀏覽代碼

optimized regexp usage

Gildas 7 年之前
父節點
當前提交
7d169e6b11
共有 1 個文件被更改,包括 11 次插入5 次删除
  1. 11 5
      lib/single-file/css-selector-parser.js

+ 11 - 5
lib/single-file/css-selector-parser.js

@@ -117,6 +117,12 @@ this.cssWhat = this.cssWhat || (() => {
 		return stringify;
 	})();
 
+	const REGEXP_WHITESPACE = /[\t\n\v\f:]/;
+	const REGEXP2 = /[ !"#$%&'()*+,./;<=>?@[\\\]^`{|}~]/;
+	const REGEXP_STARTS_UNDERSCORE = /^_/;
+	const REGEXP_STARTS_DASH_DECIMAL = /^-[\d]/;
+	const REGEXP_DECIMAL = /\d/;
+
 	return {
 		parse,
 		stringify
@@ -141,9 +147,9 @@ this.cssWhat = this.cssWhat || (() => {
 				}
 				value = "\\" + charCode.toString(16).toUpperCase() + " ";
 			} else {
-				if (/[\t\n\v\f:]/.test(character)) {
+				if (character.match(REGEXP_WHITESPACE)) {
 					value = "\\" + charCode.toString(16).toUpperCase() + " ";
-				} else if (/[ !"#$%&'()*+,./;<=>?@[\\\]^`{|}~]/.test(character)) {
+				} else if (character.match(REGEXP2)) {
 					value = "\\" + character;
 				} else {
 					value = character;
@@ -151,13 +157,13 @@ this.cssWhat = this.cssWhat || (() => {
 			}
 			output += value;
 		}
-		if (/^_/.test(output)) {
+		if (output.match(REGEXP_STARTS_UNDERSCORE)) {
 			output = "\\_" + output.slice(1);
 		}
-		if (/^-[\d]/.test(output)) {
+		if (output.match(REGEXP_STARTS_DASH_DECIMAL)) {
 			output = "\\-" + output.slice(1);
 		}
-		if (/\d/.test(firstChar)) {
+		if (firstChar.match(REGEXP_DECIMAL)) {
 			output = "\\3" + firstChar + " " + output.slice(1);
 		}
 		return {