Переглянути джерело

added support of @supports rules

Gildas 7 роки тому
батько
коміт
309e9a7b8e
1 змінених файлів з 48 додано та 42 видалено
  1. 48 42
      lib/single-file/single-file-core.js

+ 48 - 42
lib/single-file/single-file-core.js

@@ -1177,51 +1177,57 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 
 		static async processStylesheet(cssRules, baseURI, options, cssVariables, batchRequest) {
 			await Promise.all(cssRules.map(async cssRule => {
-				if (cssRule.type == "Rule") {
-					await this.processStyle(cssRule.block.children.toArray(), baseURI, options, cssVariables, batchRequest);
-				} else if (cssRule.type == "Atrule" && cssRule.name == "media" && cssRule.block) {
-					await this.processStylesheet(cssRule.block.children.toArray(), baseURI, options, cssVariables, batchRequest);
-				} else if (cssRule.type == "Atrule" && cssRule.name == "font-face") {
-					await Promise.all(cssRule.block.children.toArray().map(async declaration => {
-						if (declaration.type == "Declaration") {
-							let declarationValue = "";
-							try {
-								declarationValue = cssTree.generate(declaration.value);
-							} catch (error) {
-								// ignored
-							}
-							const urlFunctions = DomUtil.getUrlFunctions(declarationValue); // TODO: use OM
-							await Promise.all(urlFunctions.map(async urlFunction => {
-								const originalResourceURL = DomUtil.matchURL(urlFunction);
-								const resourceURL = DomUtil.normalizeURL(originalResourceURL);
-								if (!DomUtil.testIgnoredPath(resourceURL)) {
-									if (DomUtil.testValidURL(resourceURL, baseURI, options.url)) {
-										let { content } = await batchRequest.addURL(resourceURL);
-										let validResource = content == EMPTY_DATA_URI || content.startsWith(PREFIX_DATA_URI_VND) || content.startsWith(PREFIX_DATA_URI_IMAGE_SVG);
-										if (!validResource) {
-											validResource = await DOM.validFont(urlFunction);
+				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);
+					}
+				}
+			}));
+
+			async function processDeclaration(cssRule) {
+				await Promise.all(cssRule.block.children.toArray().map(async declaration => {
+					if (declaration.type == "Declaration") {
+						let declarationValue = "";
+						try {
+							declarationValue = cssTree.generate(declaration.value);
+						} catch (error) {
+							// ignored
+						}
+						const urlFunctions = DomUtil.getUrlFunctions(declarationValue); // TODO: use OM
+						await Promise.all(urlFunctions.map(async urlFunction => {
+							const originalResourceURL = DomUtil.matchURL(urlFunction);
+							const resourceURL = DomUtil.normalizeURL(originalResourceURL);
+							if (!DomUtil.testIgnoredPath(resourceURL)) {
+								if (DomUtil.testValidURL(resourceURL, baseURI, options.url)) {
+									let { content } = await batchRequest.addURL(resourceURL);
+									let validResource = content == EMPTY_DATA_URI || content.startsWith(PREFIX_DATA_URI_VND) || content.startsWith(PREFIX_DATA_URI_IMAGE_SVG);
+									if (!validResource) {
+										validResource = await DOM.validFont(urlFunction);
+									}
+									if (!validResource) {
+										content = EMPTY_DATA_URI;
+									}
+									declaration.value.children.forEach(token => {
+										let tokenValue = "";
+										try {
+											tokenValue = cssTree.generate(token.value);
+										} catch (error) {
+											// ignored
 										}
-										if (!validResource) {
-											content = EMPTY_DATA_URI;
+										if (token.type == "Url" && DOM.removeQuotes(tokenValue) == originalResourceURL) {
+											token.value.value = content;
 										}
-										declaration.value.children.forEach(token => {
-											let tokenValue = "";
-											try {
-												tokenValue = cssTree.generate(token.value);
-											} catch (error) {
-												// ignored
-											}
-											if (token.type == "Url" && DOM.removeQuotes(tokenValue) == originalResourceURL) {
-												token.value.value = content;
-											}
-										});
-									}
+									});
 								}
-							}));
-						}
-					}));
-				}
-			}));
+							}
+						}));
+					}
+				}));
+			}
 		}
 
 		static async processStyle(declarations, baseURI, options, cssVariables, batchRequest) {