Gildas 7 年 前
コミット
b4abfa98be
1 ファイル変更11 行追加12 行削除
  1. 11 12
      lib/single-file/css-rules-matcher.js

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

@@ -70,13 +70,16 @@ this.RulesMatcher = this.RulesMatcher || (() => {
 				getMatchedElementsRules(doc, cssRule.cssRules, ruleMediaInfo, sheetIndex, matchedElementsCache, unmatchedSelectorsCache);
 			} else if (cssRule.type == CSSRule.STYLE_RULE) {
 				if (cssRule.selectorText) {
+					let selectors;
 					try {
-						let selectors = cssWhat.parse(cssRule.selectorText);
-						const selectorsText = selectors.map(selector => cssWhat.stringify([selector]));
-						selectors.forEach((selector, selectorIndex) => getMatchedElementsSelector(doc, cssRule, selector, selectorsText[selectorIndex], selectorsText, mediaInfo, ruleIndex, sheetIndex, matchedElementsCache, unmatchedSelectorsCache));
+						selectors = cssWhat.parse(cssRule.selectorText);
 					} catch (error) {
 						/* ignored */
 					}
+					if (selectors) {
+						const selectorsText = selectors.map(selector => cssWhat.stringify([selector]));
+						selectors.forEach((selector, selectorIndex) => getMatchedElementsSelector(doc, cssRule, selector, selectorsText[selectorIndex], selectorsText, mediaInfo, ruleIndex, sheetIndex, matchedElementsCache, unmatchedSelectorsCache));
+					}
 				}
 			}
 		});
@@ -85,7 +88,11 @@ this.RulesMatcher = this.RulesMatcher || (() => {
 	function getMatchedElementsSelector(doc, cssRule, selector, selectorText, selectorsText, mediaInfo, ruleIndex, sheetIndex, matchedElementsCache, unmatchedSelectorsCache) {
 		let matchedElements = matchedElementsCache[selectorText];
 		if (!matchedElements) {
-			if (isUnmatchedSelector(selectorText, unmatchedSelectorsCache)) {
+			let selectorIndex = 0;
+			while (selectorIndex < unmatchedSelectorsCache.length && !selectorText.startsWith(unmatchedSelectorsCache[selectorIndex])) {
+				selectorIndex++;
+			}
+			if (selectorIndex < unmatchedSelectorsCache.length && selectorText.startsWith(unmatchedSelectorsCache[selectorIndex])) {
 				matchedElements = [];
 			} else {
 				matchedElements = doc.querySelectorAll(selectorText);
@@ -147,14 +154,6 @@ this.RulesMatcher = this.RulesMatcher || (() => {
 		return { elementInfo, ruleInfo, specificity };
 	}
 
-	function isUnmatchedSelector(selector, unmatchedSelectorsCache) {
-		let selectorIndex = 0;
-		while (selectorIndex < unmatchedSelectorsCache.length && !selector.startsWith(unmatchedSelectorsCache[selectorIndex])) {
-			selectorIndex++;
-		}
-		return selector.startsWith(unmatchedSelectorsCache[selectorIndex]);
-	}
-
 	function computeCascade(mediaInfo, parentMediaInfos, mediaAllInfo) {
 		mediaInfo.elements.forEach(elementInfo => getStylesInfo(elementInfo).forEach((elementStyleInfo, styleName) => {
 			let ruleInfo, ascendantMedia;