Browse Source

added Processor#compressCSS method

Gildas 7 years ago
parent
commit
0da6913436
1 changed files with 23 additions and 6 deletions
  1. 23 6
      lib/single-file/single-file-core.js

+ 23 - 6
lib/single-file/single-file-core.js

@@ -121,19 +121,22 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			if (!this.options.removeFrames && this.options.framesData) {
 				initializationPromises.push(this.processor.frames(true));
 			}
+			await Promise.all(initializationPromises);
+			if (this.options.removeUnusedCSSRules) {
+				this.processor.removeUnusedCSSRules();
+			}
 			if (!this.options.removeImports) {
 				initializationPromises.push(this.processor.htmlImports(true));
 			}
-			await Promise.all(initializationPromises);
 			if (this.options.compressHTML) {
 				this.processor.compressHTML();
 			}
-			if (this.options.removeUnusedCSSRules) {
-				this.processor.removeUnusedCSSRules();
-			}
 			if (this.options.removeAlternativeFonts) {
 				this.processor.removeAlternativeFonts();
 			}
+			if (this.options.compressCSS) {
+				this.processor.compressCSS();
+			}
 			this.pendingPromises = [this.processor.inlineStylesheets(), this.processor.attributeStyles(), this.processor.pageResources()];
 			if (!this.options.removeScripts) {
 				this.pendingPromises.push(this.processor.scripts());
@@ -158,6 +161,9 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			}
 			if (this.options.removeAlternativeFonts) {
 				this.processor.removeAlternativeFonts(true);
+				if (this.options.compressCSS) {
+					this.processor.compressCSS();
+				}
 			}
 			if (!this.options.removeFrames && this.options.framesData) {
 				await this.processor.frames();
@@ -430,6 +436,17 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			}
 		}
 
+		compressCSS() {
+			this.doc.querySelectorAll("style").forEach(styleElement => {
+				if (styleElement) {
+					styleElement.textContent = DOM.uglifycss(styleElement.textContent);
+				}
+			});
+			this.doc.querySelectorAll("[style]").forEach(element => {
+				element.setAttribute("style", DOM.uglifycss(element.getAttribute("style")));
+			});
+		}
+
 		insertSingleFileCommentNode() {
 			const commentNode = this.doc.createComment("\n Archive processed by SingleFile \n url: " + this.baseURI + " \n saved date: " + new Date() + " \n");
 			this.doc.documentElement.insertBefore(commentNode, this.doc.documentElement.firstChild);
@@ -501,7 +518,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 					this.stats.add("processed", "styleSheets", 1);
 				}
 				const stylesheetContent = initialization ? await DomProcessorHelper.resolveImportURLs(styleElement.textContent, this.baseURI, { maxResourceSize: this.options.maxResourceSize, maxResourceSizeEnabled: this.options.maxResourceSizeEnabled }) : await DomProcessorHelper.processStylesheet(styleElement.textContent, this.baseURI);
-				styleElement.textContent = !initialization && this.options.compressCSS ? DOM.uglifycss(stylesheetContent) : stylesheetContent;
+				styleElement.textContent = stylesheetContent;
 			}));
 		}
 
@@ -600,7 +617,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 		async attributeStyles(initialization) {
 			await Promise.all(Array.from(this.doc.querySelectorAll("[style]")).map(async element => {
 				const stylesheetContent = initialization ? await DomProcessorHelper.resolveImportURLs(element.getAttribute("style"), this.baseURI, { maxResourceSize: this.options.maxResourceSize, maxResourceSizeEnabled: this.options.maxResourceSizeEnabled }) : await DomProcessorHelper.processStylesheet(element.getAttribute("style"), this.baseURI);
-				element.setAttribute("style", this.options.compressCSS ? DOM.uglifycss(stylesheetContent) : stylesheetContent);
+				element.setAttribute("style", stylesheetContent);
 			}));
 		}