Преглед на файлове

remove @charset rules from CSS contents

Gildas преди 7 години
родител
ревизия
e3408b5d29
променени са 1 файла, в които са добавени 17 реда и са изтрити 10 реда
  1. 17 10
      lib/single-file/single-file-core.js

+ 17 - 10
lib/single-file/single-file-core.js

@@ -781,7 +781,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 
 		async processStylesheets() {
 			await Promise.all(Array.from(this.stylesheets).map(async ([, stylesheetInfo]) => {
-				await DomProcessorHelper.processStylesheet(stylesheetInfo.stylesheet.children.toArray(), this.baseURI, this.options, this.cssVariables, this.batchRequest);
+				await DomProcessorHelper.processStylesheet(stylesheetInfo.stylesheet.children, this.baseURI, this.options, this.cssVariables, this.batchRequest);
 			}));
 		}
 
@@ -1180,17 +1180,24 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 		}
 
 		static async processStylesheet(cssRules, baseURI, options, cssVariables, batchRequest) {
-			await Promise.all(cssRules.map(async cssRule => {
-				if (cssRule.block && cssRule.block.children) {
-					if (cssRule.type == "Rule") {
-						await this.processStyle(cssRule.block.children.toArray(), baseURI, options, cssVariables, batchRequest);
-					} else if (cssRule.type == "Atrule" && (cssRule.name == "media" || cssRule.name == "supports")) {
-						await this.processStylesheet(cssRule.block.children.toArray(), baseURI, options, cssVariables, batchRequest);
-					} else if (cssRule.type == "Atrule" && cssRule.name == "font-face") {
-						await processDeclaration(cssRule);
+			const promises = [];
+			const removedRules = [];
+			for (let cssRule = cssRules.head; cssRule; cssRule = cssRule.next) {
+				const ruleData = cssRule.data;
+				if (ruleData.block && ruleData.block.children) {
+					if (ruleData.type == "Rule") {
+						promises.push(this.processStyle(ruleData.block.children.toArray(), baseURI, options, cssVariables, batchRequest));
+					} else if (ruleData.type == "Atrule" && ruleData.name == "charset") {
+						removedRules.push(cssRule);
+					} else if (ruleData.type == "Atrule" && (ruleData.name == "media" || ruleData.name == "supports")) {
+						promises.push(this.processStylesheet(ruleData.block.children.toArray(), baseURI, options, cssVariables, batchRequest));
+					} else if (ruleData.type == "Atrule" && ruleData.name == "font-face") {
+						promises.push(processDeclaration(ruleData));
 					}
 				}
-			}));
+			}
+			removedRules.forEach(cssRule => cssRules.remove(cssRule));
+			await Promise.all(promises);
 
 			async function processDeclaration(cssRule) {
 				await Promise.all(cssRule.block.children.toArray().map(async declaration => {