Browse Source

removed usage of insertRule/deleteRule

Gildas 7 years ago
parent
commit
5c41bce7d8

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

@@ -46,7 +46,7 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 					stats.processed += cssRules.getSize();
 					stats.processed += cssRules.getSize();
 					stats.discarded += cssRules.getSize();
 					stats.discarded += cssRules.getSize();
 					getFontsInfo(cssRules, fontsInfo);
 					getFontsInfo(cssRules, fontsInfo);
-					docContent = getRulesTextContent(doc, cssRules, workStyleElement.sheet, docContent);
+					docContent = getRulesTextContent(doc, cssRules, workStyleElement, docContent);
 				}
 				}
 			});
 			});
 			styles.forEach(declarations => {
 			styles.forEach(declarations => {
@@ -54,7 +54,7 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 				if (fontFamilyNames.length) {
 				if (fontFamilyNames.length) {
 					fontsInfo.used.push(fontFamilyNames);
 					fontsInfo.used.push(fontFamilyNames);
 				}
 				}
-				docContent = getDeclarationsTextContent(declarations.children, workStyleElement.sheet, docContent);
+				docContent = getDeclarationsTextContent(declarations.children, workStyleElement, docContent);
 			});
 			});
 			workStyleElement.remove();
 			workStyleElement.remove();
 			docContent += doc.body.innerText;
 			docContent += doc.body.innerText;
@@ -262,10 +262,8 @@ this.fontsMinifier = this.fontsMinifier || (() => {
 	function getDeclarationUnescapedValue(declarations, property, workStylesheet) {
 	function getDeclarationUnescapedValue(declarations, property, workStylesheet) {
 		const rawValue = docHelper.removeQuotes(getDeclarationValue(declarations, property) || "");
 		const rawValue = docHelper.removeQuotes(getDeclarationValue(declarations, property) || "");
 		if (rawValue) {
 		if (rawValue) {
-			workStylesheet.insertRule("tmp { content:\"" + rawValue + "\"}");
-			const value = docHelper.removeQuotes(workStylesheet.cssRules[0].style.getPropertyValue("content"));
-			workStylesheet.deleteRule(0);
-			return value;
+			workStylesheet.textContent = "tmp { content:\"" + rawValue + "\"}";
+			return docHelper.removeQuotes(workStylesheet.sheet.cssRules[0].style.getPropertyValue("content"));
 		}
 		}
 		return "";
 		return "";
 	}
 	}

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

@@ -41,9 +41,9 @@ this.matchedRules = this.matchedRules || (() => {
 					if (stylesheetInfo.mediaText && stylesheetInfo.mediaText != MEDIA_ALL) {
 					if (stylesheetInfo.mediaText && stylesheetInfo.mediaText != MEDIA_ALL) {
 						const mediaInfo = createMediaInfo(stylesheetInfo.mediaText);
 						const mediaInfo = createMediaInfo(stylesheetInfo.mediaText);
 						this.mediaAllInfo.medias.set("style-" + sheetIndex + "-" + stylesheetInfo.mediaText, mediaInfo);
 						this.mediaAllInfo.medias.set("style-" + sheetIndex + "-" + stylesheetInfo.mediaText, mediaInfo);
-						getMatchedElementsRules(doc, cssRules, mediaInfo, sheetIndex, styles, matchedElementsCache, workStyleElement.sheet);
+						getMatchedElementsRules(doc, cssRules, mediaInfo, sheetIndex, styles, matchedElementsCache, workStyleElement);
 					} else {
 					} else {
-						getMatchedElementsRules(doc, cssRules, this.mediaAllInfo, sheetIndex, styles, matchedElementsCache, workStyleElement.sheet);
+						getMatchedElementsRules(doc, cssRules, this.mediaAllInfo, sheetIndex, styles, matchedElementsCache, workStyleElement);
 					}
 					}
 				}
 				}
 				sheetIndex++;
 				sheetIndex++;
@@ -59,7 +59,7 @@ this.matchedRules = this.matchedRules || (() => {
 				startTime = Date.now();
 				startTime = Date.now();
 				log("  -- STARTED computeCascade");
 				log("  -- STARTED computeCascade");
 			}
 			}
-			computeCascade(this.mediaAllInfo, [], this.mediaAllInfo, workStyleElement.sheet);
+			computeCascade(this.mediaAllInfo, [], this.mediaAllInfo, workStyleElement);
 			workStyleElement.remove();
 			workStyleElement.remove();
 			if (DEBUG) {
 			if (DEBUG) {
 				log("  -- ENDED computeCascade", Date.now() - startTime);
 				log("  -- ENDED computeCascade", Date.now() - startTime);
@@ -122,16 +122,8 @@ this.matchedRules = this.matchedRules || (() => {
 	}
 	}
 
 
 	function invalidSelector(selectorText, workStylesheet) {
 	function invalidSelector(selectorText, workStylesheet) {
-		let invalidSelector;
-		try {
-			workStylesheet.insertRule(selectorText + "{}");
-			workStylesheet.deleteRule(0);
-		} catch (error) {
-			if (!selectorText.match(REGEXP_VENDOR_IDENTIFIER)) {
-				invalidSelector = true;
-			}
-		}
-		return invalidSelector;
+		workStylesheet.textContent = selectorText + "{}";
+		return !workStylesheet.sheet.cssRules.length;
 	}
 	}
 
 
 	function getMatchedElementsSelector(doc, selectorInfo, styles, matchedElementsCache) {
 	function getMatchedElementsSelector(doc, selectorInfo, styles, matchedElementsCache) {
@@ -298,13 +290,12 @@ this.matchedRules = this.matchedRules || (() => {
 
 
 	function invalidDeclaration(declarationText, workStylesheet) {
 	function invalidDeclaration(declarationText, workStylesheet) {
 		let invalidDeclaration;
 		let invalidDeclaration;
-		workStylesheet.insertRule("single-file-declaration { " + declarationText + "}");
-		if (!workStylesheet.cssRules[0].style.length) {
+		workStylesheet.textContent = "single-file-declaration { " + declarationText + "}";
+		if (!workStylesheet.sheet.cssRules[0].style.length) {
 			if (!declarationText.match(REGEXP_VENDOR_IDENTIFIER)) {
 			if (!declarationText.match(REGEXP_VENDOR_IDENTIFIER)) {
 				invalidDeclaration = true;
 				invalidDeclaration = true;
 			}
 			}
 		}
 		}
-		workStylesheet.deleteRule(0);
 		return invalidDeclaration;
 		return invalidDeclaration;
 	}
 	}