Jelajahi Sumber

renamed cssRule to ruleData when cssRule stores data

Gildas 7 tahun lalu
induk
melakukan
2cdc903302

+ 26 - 26
lib/single-file/modules/css-fonts-alt-minifier.js

@@ -97,27 +97,27 @@ this.fontsAltMinifier = this.fontsAltMinifier || (() => {
 
 	function getFontsDetails(doc, cssRules, sheetIndex, mediaFontsDetails) {
 		let mediaIndex = 0, supportsIndex = 0;
-		cssRules.forEach(cssRule => {
-			if (cssRule.type == "Atrule" && cssRule.name == "media" && cssRule.block && cssRule.block.children && cssRule.prelude) {
-				const mediaText = cssTree.generate(cssRule.prelude);
+		cssRules.forEach(ruleData => {
+			if (ruleData.type == "Atrule" && ruleData.name == "media" && ruleData.block && ruleData.block.children && ruleData.prelude) {
+				const mediaText = cssTree.generate(ruleData.prelude);
 				const fontsDetails = createFontsDetailsInfo();
 				mediaFontsDetails.medias.set("media-" + sheetIndex + "-" + mediaIndex + "-" + mediaText, fontsDetails);
 				mediaIndex++;
-				getFontsDetails(doc, cssRule.block.children, sheetIndex, fontsDetails);
-			} else if (cssRule.type == "Atrule" && cssRule.name == "supports" && cssRule.block && cssRule.block.children && cssRule.prelude) {
-				const supportsText = cssTree.generate(cssRule.prelude);
+				getFontsDetails(doc, ruleData.block.children, sheetIndex, fontsDetails);
+			} else if (ruleData.type == "Atrule" && ruleData.name == "supports" && ruleData.block && ruleData.block.children && ruleData.prelude) {
+				const supportsText = cssTree.generate(ruleData.prelude);
 				const fontsDetails = createFontsDetailsInfo();
 				mediaFontsDetails.supports.set("supports-" + sheetIndex + "-" + supportsIndex + "-" + supportsText, fontsDetails);
 				supportsIndex++;
-				getFontsDetails(doc, cssRule.block.children, sheetIndex, fontsDetails);
-			} else if (cssRule.type == "Atrule" && cssRule.name == "font-face" && cssRule.block && cssRule.block.children) {
-				const fontKey = getFontKey(cssRule);
+				getFontsDetails(doc, ruleData.block.children, sheetIndex, fontsDetails);
+			} else if (ruleData.type == "Atrule" && ruleData.name == "font-face" && ruleData.block && ruleData.block.children) {
+				const fontKey = getFontKey(ruleData);
 				let fontInfo = mediaFontsDetails.fonts.get(fontKey);
 				if (!fontInfo) {
 					fontInfo = [];
 					mediaFontsDetails.fonts.set(fontKey, fontInfo);
 				}
-				const src = getPropertyValue(cssRule, "src");
+				const src = getPropertyValue(ruleData, "src");
 				if (src) {
 					const fontSources = src.match(REGEXP_URL_FUNCTION);
 					if (fontSources) {
@@ -189,7 +189,7 @@ this.fontsAltMinifier = this.fontsAltMinifier || (() => {
 		removedRules.forEach(cssRule => cssRules.remove(cssRule));
 	}
 
-	function processFontFaceRule(cssRule, fontInfo, stats) {
+	function processFontFaceRule(ruleData, fontInfo, stats) {
 		const findSource = fontFormat => fontInfo.find(source => !source.src.match(EMPTY_URL_SOURCE) && source.format == fontFormat);
 		const filterSource = fontSource => fontInfo.filter(source => source == fontSource || source.src.startsWith(LOCAL_SOURCE));
 		stats.fonts.processed += fontInfo.length;
@@ -210,24 +210,24 @@ this.fontsAltMinifier = this.fontsAltMinifier || (() => {
 		}
 		stats.fonts.discarded -= fontInfo.length;
 		const removedNodes = [];
-		for (let node = cssRule.block.children.head; node; node = node.next) {
+		for (let node = ruleData.block.children.head; node; node = node.next) {
 			if (node.data.property == "src") {
 				removedNodes.push(node);
 			}
 		}
 		removedNodes.pop();
-		removedNodes.forEach(node => cssRule.block.children.remove(node));
-		const srcDeclaration = cssRule.block.children.filter(node => node.property == "src").tail;
+		removedNodes.forEach(node => ruleData.block.children.remove(node));
+		const srcDeclaration = ruleData.block.children.filter(node => node.property == "src").tail;
 		if (srcDeclaration) {
 			fontInfo.reverse();
 			srcDeclaration.data.value = cssTree.parse(fontInfo.map(fontSource => fontSource.src).join(","), { context: "value" });
 		}
 	}
 
-	function getPropertyValue(cssRule, propertyName) {
+	function getPropertyValue(ruleData, propertyName) {
 		let property;
-		if (cssRule.block.children) {
-			property = cssRule.block.children.filter(node => node.property == propertyName).tail;
+		if (ruleData.block.children) {
+			property = ruleData.block.children.filter(node => node.property == propertyName).tail;
 		}
 		if (property) {
 			try {
@@ -238,16 +238,16 @@ this.fontsAltMinifier = this.fontsAltMinifier || (() => {
 		}
 	}
 
-	function getFontKey(cssRule) {
+	function getFontKey(ruleData) {
 		return JSON.stringify([
-			getFontFamily(getPropertyValue(cssRule, "font-family")),
-			getFontWeight(getPropertyValue(cssRule, "font-weight") || "400"),
-			getPropertyValue(cssRule, "font-style") || "normal",
-			getPropertyValue(cssRule, "unicode-range"),
-			getFontStretch(getPropertyValue(cssRule, "font-stretch")),
-			getPropertyValue(cssRule, "font-variant") || "normal",
-			getPropertyValue(cssRule, "font-feature-settings"),
-			getPropertyValue(cssRule, "font-variation-settings")
+			getFontFamily(getPropertyValue(ruleData, "font-family")),
+			getFontWeight(getPropertyValue(ruleData, "font-weight") || "400"),
+			getPropertyValue(ruleData, "font-style") || "normal",
+			getPropertyValue(ruleData, "unicode-range"),
+			getFontStretch(getPropertyValue(ruleData, "font-stretch")),
+			getPropertyValue(ruleData, "font-variant") || "normal",
+			getPropertyValue(ruleData, "font-feature-settings"),
+			getPropertyValue(ruleData, "font-variation-settings")
 		]);
 	}
 

+ 24 - 24
lib/single-file/modules/css-fonts-minifier.js

@@ -81,21 +81,21 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 	};
 
 	function getFontsInfo(cssRules, fontsInfo) {
-		cssRules.forEach(cssRule => {
-			if (cssRule.type == "Atrule" && cssRule.name == "media" && cssRule.block && cssRule.block.children) {
-				getFontsInfo(cssRule.block.children, fontsInfo);
-			} else if (cssRule.type == "Rule") {
-				const fontFamilyNames = getFontFamilyNames(cssRule.block);
+		cssRules.forEach(ruleData => {
+			if (ruleData.type == "Atrule" && ruleData.name == "media" && ruleData.block && ruleData.block.children) {
+				getFontsInfo(ruleData.block.children, fontsInfo);
+			} else if (ruleData.type == "Rule") {
+				const fontFamilyNames = getFontFamilyNames(ruleData.block);
 				if (fontFamilyNames.length) {
 					fontsInfo.used.push(fontFamilyNames);
 				}
 			} else {
-				if (cssRule.type == "Atrule" && cssRule.name == "font-face") {
-					const fontFamily = getFontFamily(getPropertyValue(cssRule, "font-family"));
+				if (ruleData.type == "Atrule" && ruleData.name == "font-face") {
+					const fontFamily = getFontFamily(getPropertyValue(ruleData, "font-family"));
 					if (fontFamily) {
-						const fontWeight = getFontWeight(getPropertyValue(cssRule, "font-weight") || "400");
-						const fontStyle = getPropertyValue(cssRule, "font-style") || "normal";
-						const fontVariant = getPropertyValue(cssRule, "font-variant") || "normal";
+						const fontWeight = getFontWeight(getPropertyValue(ruleData, "font-weight") || "400");
+						const fontStyle = getPropertyValue(ruleData, "font-style") || "normal";
+						const fontVariant = getPropertyValue(ruleData, "font-variant") || "normal";
 						fontsInfo.declared.push({ fontFamily, fontWeight, fontStyle, fontVariant });
 					}
 				}
@@ -122,13 +122,13 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 		removedRules.forEach(cssRule => cssRules.remove(cssRule));
 	}
 
-	function testUsedFont(cssRule, familyName, declaredFonts, filteredUsedFonts) {
+	function testUsedFont(ruleData, familyName, declaredFonts, filteredUsedFonts) {
 		let test;
 		const optionalUsedFonts = filteredUsedFonts && filteredUsedFonts.get(familyName);
 		if (optionalUsedFonts && optionalUsedFonts.length) {
-			const fontStyle = getPropertyValue(cssRule, "font-style") || "normal";
-			const fontWeight = getFontWeight(getPropertyValue(cssRule, "font-weight") || "400");
-			const fontVariant = getPropertyValue(cssRule, "font-variant") || "normal";
+			const fontStyle = getPropertyValue(ruleData, "font-style") || "normal";
+			const fontWeight = getFontWeight(getPropertyValue(ruleData, "font-weight") || "400");
+			const fontVariant = getPropertyValue(ruleData, "font-variant") || "normal";
 			const declaredFontsWeights = declaredFonts
 				.filter(fontInfo => fontInfo.fontFamily == familyName && fontInfo.fontStyle == fontStyle && testFontVariant(fontInfo, fontVariant))
 				.map(fontInfo => fontInfo.fontWeight)
@@ -141,10 +141,10 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 		return test;
 	}
 
-	function getPropertyValue(cssRule, propertyName) {
+	function getPropertyValue(ruleData, propertyName) {
 		let property;
-		if (cssRule.block.children) {
-			property = cssRule.block.children.filter(node => node.property == propertyName).tail;
+		if (ruleData.block.children) {
+			property = ruleData.block.children.filter(node => node.property == propertyName).tail;
 		}
 		if (property) {
 			try {
@@ -237,14 +237,14 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 	}
 
 	function getPseudoElementsContent(doc, cssRules) {
-		return cssRules.toArray().map(cssRule => {
-			if (cssRule.block && cssRule.block.children && cssRule.prelude && cssRule.prelude.children) {
-				if (cssRule.type == "Atrule" && cssRule.name == "media") {
-					return getPseudoElementsContent(doc, cssRule.block.children);
-				} else if (cssRule.type == "Rule") {
-					const selector = cssTree.generate(cssRule.prelude); // TODO use OM
+		return cssRules.toArray().map(ruleData => {
+			if (ruleData.block && ruleData.block.children && ruleData.prelude && ruleData.prelude.children) {
+				if (ruleData.type == "Atrule" && ruleData.name == "media") {
+					return getPseudoElementsContent(doc, ruleData.block.children);
+				} else if (ruleData.type == "Rule") {
+					const selector = cssTree.generate(ruleData.prelude); // TODO use OM
 					if (testPseudoElements(selector)) {
-						const value = docHelper.removeQuotes(getPropertyValue(cssRule, "content") || "");
+						const value = docHelper.removeQuotes(getPropertyValue(ruleData, "content") || "");
 						if (value) {
 							const styleElement = doc.createElement("style");
 							styleElement.textContent = "tmp { content:\"" + value + "\"}";

+ 22 - 22
lib/single-file/modules/css-matched-rules.js

@@ -93,20 +93,20 @@ this.matchedRules = this.matchedRules || (() => {
 			startTime = Date.now();
 			log("  -- STARTED getMatchedElementsRules", " index =", sheetIndex, "rules.length =", cssRules.length);
 		}
-		cssRules.forEach(cssRule => {
-			if (cssRule.block && cssRule.block.children && cssRule.prelude && cssRule.prelude.children) {
-				if (cssRule.type == "Atrule" && cssRule.name == "media") {
-					const mediaText = cssTree.generate(cssRule.prelude);
+		cssRules.forEach(ruleData => {
+			if (ruleData.block && ruleData.block.children && ruleData.prelude && ruleData.prelude.children) {
+				if (ruleData.type == "Atrule" && ruleData.name == "media") {
+					const mediaText = cssTree.generate(ruleData.prelude);
 					const ruleMediaInfo = createMediaInfo(mediaText);
 					mediaInfo.medias.set("rule-" + sheetIndex + "-" + mediaIndex + "-" + mediaText, ruleMediaInfo);
 					mediaIndex++;
-					getMatchedElementsRules(doc, cssRule.block.children, ruleMediaInfo, sheetIndex, styles, matchedElementsCache, workStylesheet);
-				} else if (cssRule.type == "Rule") {
-					const selectors = cssRule.prelude.children.toArray();
-					const selectorsText = cssRule.prelude.children.toArray().map(selector => cssTree.generate(selector));
-					const ruleInfo = { cssRule, mediaInfo, ruleIndex, sheetIndex, matchedSelectors: new Set(), declarations: new Set(), selectors, selectorsText };
+					getMatchedElementsRules(doc, ruleData.block.children, ruleMediaInfo, sheetIndex, styles, matchedElementsCache, workStylesheet);
+				} else if (ruleData.type == "Rule") {
+					const selectors = ruleData.prelude.children.toArray();
+					const selectorsText = ruleData.prelude.children.toArray().map(selector => cssTree.generate(selector));
+					const ruleInfo = { ruleData, mediaInfo, ruleIndex, sheetIndex, matchedSelectors: new Set(), declarations: new Set(), selectors, selectorsText };
 					if (!invalidSelector(selectorsText.join(","), workStylesheet)) {
-						for (let selector = cssRule.prelude.children.head, selectorIndex = 0; selector; selector = selector.next, selectorIndex++) {
+						for (let selector = ruleData.prelude.children.head, selectorIndex = 0; selector; selector = selector.next, selectorIndex++) {
 							const selectorText = selectorsText[selectorIndex];
 							const selectorInfo = { selector, selectorText, ruleInfo };
 							getMatchedElementsSelector(doc, selectorInfo, styles, matchedElementsCache);
@@ -161,10 +161,10 @@ this.matchedRules = this.matchedRules || (() => {
 				if (filteredSelectorText == selectorInfo.selectorText) {
 					matchedElements.forEach(element => addRule(element, selectorInfo, styles));
 				} else {
-					let pseudoSelectors = selectorInfo.ruleInfo.mediaInfo.pseudoRules.get(selectorInfo.ruleInfo.cssRule);
+					let pseudoSelectors = selectorInfo.ruleInfo.mediaInfo.pseudoRules.get(selectorInfo.ruleInfo.ruleData);
 					if (!pseudoSelectors) {
 						pseudoSelectors = new Set();
-						selectorInfo.ruleInfo.mediaInfo.pseudoRules.set(selectorInfo.ruleInfo.cssRule, pseudoSelectors);
+						selectorInfo.ruleInfo.mediaInfo.pseudoRules.set(selectorInfo.ruleInfo.ruleData, pseudoSelectors);
 					}
 					pseudoSelectors.add(selectorInfo.selectorText);
 					matchedElements.forEach(element => addPseudoRule(element, selectorInfo));
@@ -214,7 +214,7 @@ this.matchedRules = this.matchedRules || (() => {
 		if (!elementInfo) {
 			elementInfo = [];
 			if (elementStyle) {
-				elementInfo.push({ styleInfo: { cssStyle: elementStyle, declarations: new Set() } });
+				elementInfo.push({ styleInfo: { styleData: elementStyle, declarations: new Set() } });
 			}
 			mediaInfo.elements.set(element, elementInfo);
 		}
@@ -241,18 +241,18 @@ this.matchedRules = this.matchedRules || (() => {
 					let info;
 					if (declarationsInfo.selectorInfo.ruleInfo) {
 						info = declarationsInfo.selectorInfo.ruleInfo;
-						const cssRule = info.cssRule;
-						const ascendantMedia = [mediaInfo, ...parentMediaInfo].find(media => media.rules.get(cssRule)) || mediaInfo;
-						ascendantMedia.rules.set(cssRule, info);
-						if (cssRule) {
+						const ruleData = info.ruleData;
+						const ascendantMedia = [mediaInfo, ...parentMediaInfo].find(media => media.rules.get(ruleData)) || mediaInfo;
+						ascendantMedia.rules.set(ruleData, info);
+						if (ruleData) {
 							info.matchedSelectors.add(declarationsInfo.selectorInfo.selectorText);
 						}
 					} else {
 						info = declarationsInfo.selectorInfo.styleInfo;
-						const cssStyle = info.cssStyle;
-						const matchedStyleInfo = mediaAllInfo.matchedStyles.get(cssStyle);
+						const styleData = info.styleData;
+						const matchedStyleInfo = mediaAllInfo.matchedStyles.get(styleData);
 						if (!matchedStyleInfo) {
-							mediaAllInfo.matchedStyles.set(cssStyle, info);
+							mediaAllInfo.matchedStyles.set(styleData, info);
 						}
 					}
 					if (!info.declarations.has(property)) {
@@ -269,9 +269,9 @@ this.matchedRules = this.matchedRules || (() => {
 		elementInfo.forEach(selectorInfo => {
 			let declarations;
 			if (selectorInfo.styleInfo) {
-				declarations = selectorInfo.styleInfo.cssStyle.children;
+				declarations = selectorInfo.styleInfo.styleData.children;
 			} else {
-				declarations = selectorInfo.ruleInfo.cssRule.block.children;
+				declarations = selectorInfo.ruleInfo.ruleData.block.children;
 			}
 			processDeclarations(declarationsInfo, declarations, selectorInfo, workStylesheet);
 		});

+ 9 - 9
lib/single-file/modules/css-rules-minifier.js

@@ -98,35 +98,35 @@ this.cssRulesMinifier = this.cssRulesMinifier || (() => {
 		}
 	}
 
-	function processRuleInfo(cssRule, ruleInfo, pseudoSelectors) {
+	function processRuleInfo(ruleData, ruleInfo, pseudoSelectors) {
 		const removedDeclarations = [];
 		const removedSelectors = [];
-		for (let declaration = cssRule.block.children.tail; declaration; declaration = declaration.prev) {
+		for (let declaration = ruleData.block.children.tail; declaration; declaration = declaration.prev) {
 			if (!ruleInfo.declarations.has(declaration.data)) {
 				removedDeclarations.push(declaration);
 			}
 		}
-		for (let selector = cssRule.prelude.children.head; selector; selector = selector.next) {
+		for (let selector = ruleData.prelude.children.head; selector; selector = selector.next) {
 			const selectorText = cssTree.generate(selector.data);
 			if (!ruleInfo.matchedSelectors.has(selectorText) && (!pseudoSelectors || !pseudoSelectors.has(selectorText))) {
 				removedSelectors.push(selector);
 			}
 		}
-		removedDeclarations.forEach(declaration => cssRule.block.children.remove(declaration));
-		removedSelectors.forEach(selector => cssRule.prelude.children.remove(selector));
+		removedDeclarations.forEach(declaration => ruleData.block.children.remove(declaration));
+		removedSelectors.forEach(selector => ruleData.prelude.children.remove(selector));
 	}
 
-	function processStyleAttribute(cssStyle, mediaAllInfo) {
+	function processStyleAttribute(styleData, mediaAllInfo) {
 		const removedDeclarations = [];
-		const styleInfo = mediaAllInfo.matchedStyles.get(cssStyle);
+		const styleInfo = mediaAllInfo.matchedStyles.get(styleData);
 		if (styleInfo) {
 			let propertyFound;
-			for (let declaration = cssStyle.children.head; declaration && !propertyFound; declaration = declaration.next) {
+			for (let declaration = styleData.children.head; declaration && !propertyFound; declaration = declaration.next) {
 				if (!styleInfo.declarations.has(declaration.data)) {
 					removedDeclarations.push(declaration);
 				}
 			}
-			removedDeclarations.forEach(declaration => cssStyle.children.remove(declaration));
+			removedDeclarations.forEach(declaration => styleData.children.remove(declaration));
 		}
 	}
 

+ 4 - 3
lib/single-file/single-file-core.js

@@ -812,7 +812,8 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 							if (this.options.compressCSS) {
 								const removedRules = [];
 								for (let cssRule = stylesheet.children.head; cssRule; cssRule = cssRule.next) {
-									if (cssRule.data.type == "Raw" && cssRule.data.value && cssRule.data.value.trim().startsWith("//")) {
+									const ruleData = cssRule.data;
+									if (ruleData.type == "Raw" && ruleData.value && ruleData.value.trim().startsWith("//")) {
 										removedRules.push(cssRule);
 									}
 								}
@@ -1300,8 +1301,8 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			removedRules.forEach(cssRule => cssRules.remove(cssRule));
 			await Promise.all(promises);
 
-			async function processFontFaceRule(cssRule) {
-				await Promise.all(cssRule.block.children.toArray().map(async declaration => {
+			async function processFontFaceRule(ruleData) {
+				await Promise.all(ruleData.block.children.toArray().map(async declaration => {
 					if (declaration.type == "Declaration" && declaration.value.children) {
 						const urlFunctions = Util.getUrlFunctions(Util.getCSSValue(declaration.value));
 						await Promise.all(urlFunctions.map(async urlFunction => {