Explorar el Código

refactored code

Gildas hace 7 años
padre
commit
9976c9984f
Se han modificado 1 ficheros con 46 adiciones y 39 borrados
  1. 46 39
      lib/single-file/css-rules-matcher.js

+ 46 - 39
lib/single-file/css-rules-matcher.js

@@ -96,48 +96,55 @@ this.RulesMatcher = this.RulesMatcher || (() => {
 			matchedElementsCache[selectorText] = matchedElements;
 		}
 		if (matchedElements.length) {
-			matchedElements.forEach(element => {
-				let elementInfo;
-				if (mediaInfo.elements.has(element)) {
-					elementInfo = mediaInfo.elements.get(element);
-				} else {
-					elementInfo = [];
-					const elementStyle = element.getAttribute("style");
-					if (elementStyle) {
-						elementInfo.push({ cssStyle: element.style });
-					}
-					mediaInfo.elements.set(element, elementInfo);
-				}
-				const specificity = computeSpecificity(selector);
-				specificity.ruleIndex = ruleIndex;
-				specificity.sheetIndex = sheetIndex;
-				let ruleInfo;
-				if (elementInfo.length) {
-					let elementRuleIndex = 0;
-					while (elementRuleIndex < elementInfo.length && elementInfo[elementRuleIndex].cssRule != cssRule) {
-						elementRuleIndex++;
-					}
-					if (elementRuleIndex < elementInfo.length && elementInfo[elementRuleIndex].cssRule == cssRule) {
-						ruleInfo = elementInfo[elementRuleIndex];
-					}
+			matchedElements.forEach(element => addRule(element, cssRule, selector, selectorText, selectorsText, mediaInfo, ruleIndex, sheetIndex));
+		}
+	}
+
+	function addRule(element, cssRule, selector, selectorText, selectorsText, mediaInfo, ruleIndex, sheetIndex) {
+		const info = getInfo(element, cssRule, selector, mediaInfo, ruleIndex, sheetIndex);
+		const { elementInfo, ruleInfo, specificity } = info;
+		if (ruleInfo) {
+			if (compareSpecificity(ruleInfo.specificity, specificity) == 1) {
+				let pseudoClassIndex = 0;
+				while (pseudoClassIndex < IGNORED_PSEUDO_CLASSES.length && !selectorText.includes(IGNORED_PSEUDO_CLASSES[pseudoClassIndex])) {
+					pseudoClassIndex++;
 				}
-				if (ruleInfo) {
-					if (compareSpecificity(ruleInfo.specificity, specificity) == 1) {
-						let pseudoClassIndex = 0;
-						while (pseudoClassIndex < IGNORED_PSEUDO_CLASSES.length && !selectorText.includes(IGNORED_PSEUDO_CLASSES[pseudoClassIndex])) {
-							pseudoClassIndex++;
-						}
-						if (pseudoClassIndex < IGNORED_PSEUDO_CLASSES.length && selectorText.includes(IGNORED_PSEUDO_CLASSES[pseudoClassIndex])) {
-							ruleInfo.specificity = specificity;
-							ruleInfo.selectorText = selectorText;
-						}
-					}
-				} else {
-					ruleInfo = { cssRule, specificity, selectorText, selectorsText };
-					elementInfo.push(ruleInfo);
+				if (pseudoClassIndex < IGNORED_PSEUDO_CLASSES.length && selectorText.includes(IGNORED_PSEUDO_CLASSES[pseudoClassIndex])) {
+					ruleInfo.specificity = specificity;
+					ruleInfo.selectorText = selectorText;
 				}
-			});
+			}
+		} else {
+			elementInfo.push({ cssRule, specificity, selectorText, selectorsText });
+		}
+	}
+
+	function getInfo(element, cssRule, selector, mediaInfo, ruleIndex, sheetIndex) {
+		let elementInfo;
+		if (mediaInfo.elements.has(element)) {
+			elementInfo = mediaInfo.elements.get(element);
+		} else {
+			elementInfo = [];
+			const elementStyle = element.getAttribute("style");
+			if (elementStyle) {
+				elementInfo.push({ cssStyle: element.style });
+			}
+			mediaInfo.elements.set(element, elementInfo);
+		}
+		const specificity = computeSpecificity(selector);
+		specificity.ruleIndex = ruleIndex;
+		specificity.sheetIndex = sheetIndex;
+		let ruleInfo;
+		if (elementInfo.length) {
+			let elementRuleIndex = 0;
+			while (elementRuleIndex < elementInfo.length && elementInfo[elementRuleIndex].cssRule != cssRule) {
+				elementRuleIndex++;
+			}
+			if (elementRuleIndex < elementInfo.length && elementInfo[elementRuleIndex].cssRule == cssRule) {
+				ruleInfo = elementInfo[elementRuleIndex];
+			}
 		}
+		return { elementInfo, ruleInfo, specificity };
 	}
 
 	function isUnmatchedSelector(selector, unmatchedSelectorsCache) {