Просмотр исходного кода

fixed minor issue when optimizing style attributes

Gildas 7 лет назад
Родитель
Сommit
f8c6f65187
2 измененных файлов с 21 добавлено и 14 удалено
  1. 7 2
      lib/single-file/css-rules-matcher.js
  2. 14 12
      lib/single-file/css-rules-minifier.js

+ 7 - 2
lib/single-file/css-rules-matcher.js

@@ -194,7 +194,12 @@ this.RulesMatcher = this.RulesMatcher || (() => {
 			} else {
 				const styleInfo = elementStyleInfo.selectorInfo.styleInfo;
 				const cssStyle = styleInfo.cssStyle;
-				mediaAllInfo.styles.set(cssStyle, styleInfo);
+				let styleInfos = mediaAllInfo.matchedStyles.get(cssStyle);
+				if (!styleInfos) {
+					styleInfos = [];
+					mediaAllInfo.matchedStyles.set(cssStyle, styleInfos);
+				}
+				styleInfos.push(styleInfo);
 				const styleValue = styleInfo.style.get(styleName);
 				if (!styleValue) {
 					styleInfo.style.set(styleName, elementStyleInfo.styleValue);
@@ -234,7 +239,7 @@ this.RulesMatcher = this.RulesMatcher || (() => {
 	function createMediaInfo(media) {
 		const mediaInfo = { media: media, elements: new Map(), pseudos: new Map(), medias: new Map(), rules: new Map(), pseudoSelectors: new Set() };
 		if (media == MEDIA_ALL) {
-			mediaInfo.styles = new Map();
+			mediaInfo.matchedStyles = new Map();
 		}
 		return mediaInfo;
 	}

+ 14 - 12
lib/single-file/css-rules-minifier.js

@@ -123,21 +123,23 @@ this.cssMinifier = this.cssMinifier || (() => {
 		return (selectorText || cssRule.selectorText) + "{" + (styleCssText || cssRule.style.cssText) + "}";
 	}
 
-	function processStyleAttribute(cssStyle, mediaInfo) {
+	function processStyleAttribute(cssStyle, mediaAllInfo) {
 		let styleCssText = "";
-		const styleInfo = mediaInfo.styles.get(cssStyle);
-		if (styleInfo) {
-			const stylesInfo = parseCss.parseAListOfDeclarations(cssStyle.cssText);
-			for (let styleIndex = 0; styleIndex < stylesInfo.length; styleIndex++) {
-				const style = stylesInfo[styleIndex];
-				if (styleInfo.style.get(style.name)) {
-					if (styleCssText) {
-						styleCssText += ";";
+		const styleInfos = mediaAllInfo.matchedStyles.get(cssStyle);
+		if (styleInfos) {
+			styleInfos.forEach(styleInfo => {
+				const stylesInfo = parseCss.parseAListOfDeclarations(cssStyle.cssText);
+				for (let styleIndex = 0; styleIndex < stylesInfo.length; styleIndex++) {
+					const style = stylesInfo[styleIndex];
+					if (styleInfo.style.get(style.name)) {
+						if (styleCssText) {
+							styleCssText += ";";
+						}
+						const priority = cssStyle.getPropertyPriority(style.name);
+						styleCssText += style.name + ":" + cssStyle.getPropertyValue(style.name) + (priority && ("!" + priority));
 					}
-					const priority = cssStyle.getPropertyPriority(style.name);
-					styleCssText += style.name + ":" + cssStyle.getPropertyValue(style.name) + (priority && ("!" + priority));
 				}
-			}
+			});
 		}
 		return (styleCssText || cssStyle.cssText);
 	}