Browse Source

minor optimizations

Gildas 7 years ago
parent
commit
519b6cb0a4
1 changed files with 12 additions and 5 deletions
  1. 12 5
      lib/single-file/css-rules-matcher.js

+ 12 - 5
lib/single-file/css-rules-matcher.js

@@ -30,7 +30,7 @@ this.RulesMatcher = this.RulesMatcher || (() => {
 		constructor(doc) {
 		constructor(doc) {
 			this.doc = doc;
 			this.doc = doc;
 			this.mediaAllInfo = createMediaInfo(MEDIA_ALL);
 			this.mediaAllInfo = createMediaInfo(MEDIA_ALL);
-			const matchedElementsCache = new Map();
+			const matchedElementsCache = {};
 			const unmatchedSelectorsCache = [];
 			const unmatchedSelectorsCache = [];
 			doc.querySelectorAll("style").forEach((styleElement, styleIndex) => {
 			doc.querySelectorAll("style").forEach((styleElement, styleIndex) => {
 				if (styleElement.sheet) {
 				if (styleElement.sheet) {
@@ -83,7 +83,7 @@ this.RulesMatcher = this.RulesMatcher || (() => {
 	}
 	}
 
 
 	function getMatchedElementsSelector(doc, cssRule, selector, selectorText, selectorsText, mediaInfo, ruleIndex, sheetIndex, matchedElementsCache, unmatchedSelectorsCache) {
 	function getMatchedElementsSelector(doc, cssRule, selector, selectorText, selectorsText, mediaInfo, ruleIndex, sheetIndex, matchedElementsCache, unmatchedSelectorsCache) {
-		let matchedElements = matchedElementsCache.get(selectorText);
+		let matchedElements = matchedElementsCache[selectorText];
 		if (!matchedElements) {
 		if (!matchedElements) {
 			if (isUnmatchedSelector(selectorText, unmatchedSelectorsCache)) {
 			if (isUnmatchedSelector(selectorText, unmatchedSelectorsCache)) {
 				matchedElements = [];
 				matchedElements = [];
@@ -93,7 +93,7 @@ this.RulesMatcher = this.RulesMatcher || (() => {
 					unmatchedSelectorsCache.push(selectorText + " ");
 					unmatchedSelectorsCache.push(selectorText + " ");
 				}
 				}
 			}
 			}
-			matchedElementsCache.set(selectorText, matchedElements);
+			matchedElementsCache[selectorText] = matchedElements;
 		}
 		}
 		if (matchedElements.length) {
 		if (matchedElements.length) {
 			matchedElements.forEach(element => {
 			matchedElements.forEach(element => {
@@ -111,9 +111,16 @@ this.RulesMatcher = this.RulesMatcher || (() => {
 				const specificity = computeSpecificity(selector);
 				const specificity = computeSpecificity(selector);
 				specificity.ruleIndex = ruleIndex;
 				specificity.ruleIndex = ruleIndex;
 				specificity.sheetIndex = sheetIndex;
 				specificity.sheetIndex = sheetIndex;
-				let ruleInfo = elementInfo.find(ruleInfo => ruleInfo.cssRule == cssRule);
+				let elementRuleIndex = 0;
+				while (elementRuleIndex < elementInfo.length && elementInfo[elementRuleIndex].cssRule != cssRule) {
+					elementRuleIndex++;
+				}
+				let ruleInfo;
+				if (elementRuleIndex < elementInfo.length && elementInfo[elementRuleIndex].cssRule == cssRule) {
+					ruleInfo = elementInfo[elementRuleIndex];
+				}
 				if (ruleInfo) {
 				if (ruleInfo) {
-					if (!IGNORED_PSEUDO_CLASSES.find(pseudoClass => selectorText.includes(pseudoClass)) && compareSpecificity(ruleInfo.specificity, specificity) == 1) {
+					if (compareSpecificity(ruleInfo.specificity, specificity) == 1 && !IGNORED_PSEUDO_CLASSES.find(pseudoClass => selectorText.includes(pseudoClass))) {
 						ruleInfo.specificity = specificity;
 						ruleInfo.specificity = specificity;
 						ruleInfo.selectorText = selectorText;
 						ruleInfo.selectorText = selectorText;
 					}
 					}