瀏覽代碼

removed usgae of Array#find

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

+ 40 - 7
lib/single-file/css-rules-minifier.js

@@ -22,8 +22,8 @@
 
 this.cssMinifier = this.cssMinifier || (() => {
 
-	const REMOVED_PSEUDO_CLASSES = ["focus", "focus-within", "hover", "link", "visited", "active"];
-	const REMOVED_PSEUDO_ELEMENTS = ["after", "before", "first-line", "first-letter", "placeholder", "-webkit-input-placeholder", "selection", "marker", "cue", "-webkit-progress-bar", "-webkit-progress-value", "-webkit-inner-spin-button", "-webkit-outer-spin-button", "-webkit-search-cancel-button", "-webkit-search-cancel-button"];
+	const REMOVED_PSEUDO_CLASSES = [":focus", ":focus-within", ":hover", ":link", ":visited", ":active"];
+	const REMOVED_PSEUDO_ELEMENTS = ["::after", "::before", "::first-line", "::first-letter", "::placeholder", "::-webkit-input-placeholder", "::selection", "::marker", "::cue", "::-webkit-progress-bar", "::-webkit-progress-value", "::-webkit-inner-spin-button", "::-webkit-outer-spin-button", "::-webkit-search-cancel-button", "::-webkit-search-cancel-button"];
 	const IGNORED_SELECTORS = ["::-webkit-scrollbar", "::-webkit-scrollbar-button", "::-webkit-scrollbar-thumb", "::-webkit-scrollbar-track", "::-webkit-scrollbar-track-piece", "::-webkit-scrollbar-corner", "::-webkit-resizer"];
 
 	return {
@@ -84,10 +84,6 @@ this.cssMinifier = this.cssMinifier || (() => {
 		stats.discarded -= cssRules.length;
 	}
 
-	function testIgnoredSelector(selectorText) {
-		return IGNORED_SELECTORS.find(selector => selectorText.toLowerCase().includes(selector)) || testFilterSelector(selectorText);
-	}
-
 	function processStyle(doc, cssStyle, mediaInfo) {
 		const styleInfo = mediaInfo.styles.get(cssStyle);
 		if (styleInfo) {
@@ -113,8 +109,45 @@ this.cssMinifier = this.cssMinifier || (() => {
 		return sheetContent;
 	}
 
+	function testIgnoredSelector(selectorText) {
+		let indexSelector = 0, found;
+		selectorText = selectorText.toLowerCase();
+		while (indexSelector < IGNORED_SELECTORS.length && !found) {
+			found = selectorText.includes(IGNORED_SELECTORS[indexSelector]);
+			if (!found) {
+				indexSelector++;
+			}
+		}
+		if (!found) {
+			indexSelector = 0;
+			while (indexSelector < IGNORED_SELECTORS.length && !found) {
+				found = testFilterSelector(selectorText);
+				if (!found) {
+					indexSelector++;
+				}
+			}
+		}
+		return found;
+	}
+
 	function testFilterSelector(selector) {
-		return REMOVED_PSEUDO_CLASSES.find(pseudoClass => selector.includes(":" + pseudoClass)) || REMOVED_PSEUDO_ELEMENTS.find(pseudoElement => selector.includes("::" + pseudoElement));
+		let indexPseudoClass = 0, found;
+		while (indexPseudoClass < REMOVED_PSEUDO_CLASSES.length && !found) {
+			found = selector.includes(REMOVED_PSEUDO_CLASSES[indexPseudoClass]);
+			if (!found) {
+				indexPseudoClass++;
+			}
+		}
+		if (!found) {
+			let indexPseudoElement = 0;
+			while (indexPseudoElement < REMOVED_PSEUDO_ELEMENTS.length && !found) {
+				found = selector.includes(REMOVED_PSEUDO_ELEMENTS[indexPseudoElement]);
+				if (!found) {
+					indexPseudoElement++;
+				}
+			}
+		}
+		return found;
 	}
 
 })();