فهرست منبع

insert empty images when invalid urls are found

Gildas 7 سال پیش
والد
کامیت
e8546b7ca3
1فایلهای تغییر یافته به همراه58 افزوده شده و 41 حذف شده
  1. 58 41
      lib/single-file/single-file-core.js

+ 58 - 41
lib/single-file/single-file-core.js

@@ -796,13 +796,11 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 				options.doc = null;
 				options.win = null;
 				options.url = resourceURL;
-				if (resourceURL) {
-					if (resourceURL && resourceURL != this.baseURI && DomUtil.testValidPath(resourceURL)) {
-						const processor = new PageProcessor(options);
-						this.relImportProcessors.set(linkElement, processor);
-						await processor.loadPage();
-						return processor.initialize();
-					}
+				if (!DomUtil.testIgnoredPath(resourceURL) && DomUtil.testValidPath(resourceURL, this.baseURI)) {
+					const processor = new PageProcessor(options);
+					this.relImportProcessors.set(linkElement, processor);
+					await processor.loadPage();
+					return processor.initialize();
 				}
 			}));
 		}
@@ -983,10 +981,12 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			urlFunctions.map(urlFunction => {
 				const originalResourceURL = DomUtil.matchURL(urlFunction);
 				const resourceURL = DomUtil.normalizeURL(originalResourceURL);
-				if (resourceURL && resourceURL != baseURI && DomUtil.testValidPath(resourceURL)) {
-					const resolvedURL = new URL(resourceURL, baseURI).href;
-					if (resourceURL != resolvedURL && stylesheetContent.includes(urlFunction)) {
-						stylesheetContent = stylesheetContent.replace(DomUtil.getRegExp(urlFunction), urlFunction.replace(originalResourceURL, resolvedURL));
+				if (!DomUtil.testIgnoredPath(resourceURL)) {
+					if (DomUtil.testValidPath(resourceURL, baseURI)) {
+						const resolvedURL = new URL(resourceURL, baseURI).href;
+						if (resourceURL != resolvedURL && stylesheetContent.includes(urlFunction)) {
+							stylesheetContent = stylesheetContent.replace(DomUtil.getRegExp(urlFunction), urlFunction.replace(originalResourceURL, resolvedURL));
+						}
 					}
 				} else {
 					if (resourceURL.startsWith(DATA_URI_PREFIX)) {
@@ -1017,14 +1017,16 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			await Promise.all(urlFunctions.map(async urlFunction => {
 				const originalResourceURL = DomUtil.matchURL(urlFunction);
 				const resourceURL = DomUtil.normalizeURL(originalResourceURL);
-				if (resourceURL && resourceURL != baseURI && DomUtil.testValidPath(resourceURL) && stylesheetContent.includes(urlFunction)) {
-					const { content, indexResource, duplicate } = await batchRequest.addURL(resourceURL);
-					const regExpUrlFunction = DomUtil.getRegExp(urlFunction);
-					if (duplicate && options.groupDuplicateImages) {
-						resourceInfos.set(resourceURL, { regExpUrlFunction, dataURI: content, variableName: "var(" + SINGLE_FILE_VARIABLE_NAME_PREFIX + indexResource + ")" });
-						DomUtil.insertVariable(doc, indexResource, content, options);
-					} else {
-						resourceInfos.set(resourceURL, { regExpUrlFunction, dataURI: content });
+				if (!DomUtil.testIgnoredPath(resourceURL)) {
+					if (DomUtil.testValidURL(resourceURL, baseURI) && stylesheetContent.includes(urlFunction)) {
+						const { content, indexResource, duplicate } = await batchRequest.addURL(resourceURL);
+						const regExpUrlFunction = DomUtil.getRegExp(urlFunction);
+						if (duplicate && options.groupDuplicateImages) {
+							resourceInfos.set(resourceURL, { regExpUrlFunction, dataURI: content, variableName: "var(" + SINGLE_FILE_VARIABLE_NAME_PREFIX + indexResource + ")" });
+							DomUtil.insertVariable(doc, indexResource, content, options);
+						} else {
+							resourceInfos.set(resourceURL, { regExpUrlFunction, dataURI: content });
+						}
 					}
 				}
 			}));
@@ -1054,12 +1056,14 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 				urlFunctions.forEach(urlFunction => {
 					const originalResourceURL = DomUtil.matchURL(urlFunction);
 					const resourceURL = DomUtil.normalizeURL(originalResourceURL);
-					if (resourceURL && resourceURL != baseURI && DomUtil.testValidPath(resourceURL) && cssText.includes(urlFunction)) {
-						const resourceInfo = resourceInfos.get(resourceURL);
-						if (options.groupDuplicateImages && resourceInfo.variableName) {
-							cssText = cssText.replace(resourceInfo.regExpUrlFunction, resourceInfo.variableName);
-						} else {
-							cssText = cssText.replace(resourceInfo.regExpUrlFunction, urlFunction.replace(originalResourceURL, resourceInfo.dataURI));
+					if (!DomUtil.testIgnoredPath(resourceURL)) {
+						if (DomUtil.testValidURL(resourceURL, baseURI) && cssText.includes(urlFunction)) {
+							const resourceInfo = resourceInfos.get(resourceURL);
+							if (options.groupDuplicateImages && resourceInfo.variableName) {
+								cssText = cssText.replace(resourceInfo.regExpUrlFunction, resourceInfo.variableName);
+							} else {
+								cssText = cssText.replace(resourceInfo.regExpUrlFunction, urlFunction.replace(originalResourceURL, resourceInfo.dataURI));
+							}
 						}
 					}
 				});
@@ -1070,10 +1074,10 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 		static async processAttribute(doc, resourceElements, attributeName, prefixDataURI, baseURI, batchRequest, options, processDuplicates, removeElementIfMissing) {
 			await Promise.all(Array.from(resourceElements).map(async resourceElement => {
 				let resourceURL = resourceElement.getAttribute(attributeName);
-				if (resourceURL) {
-					resourceURL = DomUtil.normalizeURL(resourceURL);
+				resourceURL = DomUtil.normalizeURL(resourceURL);
+				if (!DomUtil.testIgnoredPath(resourceURL)) {
 					resourceElement.setAttribute(attributeName, EMPTY_IMAGE);
-					if (resourceURL && resourceURL != baseURI && DomUtil.testValidPath(resourceURL)) {
+					if (DomUtil.testValidURL(resourceURL, baseURI)) {
 						try {
 							const { content, indexResource, duplicate } = await batchRequest.addURL(new URL(resourceURL, baseURI).href);
 							if (removeElementIfMissing && content == EMPTY_DATA_URI) {
@@ -1099,9 +1103,10 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			const attributeName = "xlink:href";
 			await Promise.all(Array.from(resourceElements).map(async resourceElement => {
 				const originalResourceURL = resourceElement.getAttribute(attributeName);
-				if (originalResourceURL) {
-					const resourceURL = DomUtil.normalizeURL(originalResourceURL);
-					if (resourceURL && resourceURL != baseURI && DomUtil.testValidPath(resourceURL)) {
+				const resourceURL = DomUtil.normalizeURL(originalResourceURL);
+				if (!DomUtil.testIgnoredPath(resourceURL)) {
+					resourceElement.setAttribute(attributeName, EMPTY_IMAGE);
+					if (DomUtil.testValidURL(resourceURL, baseURI)) {
 						try {
 							const { content } = await batchRequest.addURL(new URL(resourceURL, baseURI).href, false);
 							const DOMParser = DOM.getParser();
@@ -1121,7 +1126,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 								resourceElement.setAttribute(attributeName, "data:image/svg+xml," + content);
 							}
 						} catch (error) {
-							resourceElement.setAttribute(attributeName, EMPTY_IMAGE);
+							/* ignored */
 						}
 					}
 				}
@@ -1133,14 +1138,18 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 				const srcset = DOM.parseSrcset(resourceElement.getAttribute(attributeName));
 				const srcsetValues = await Promise.all(srcset.map(async srcsetValue => {
 					const resourceURL = DomUtil.normalizeURL(srcsetValue.url);
-					if (resourceURL && resourceURL != baseURI && DomUtil.testValidPath(resourceURL)) {
-						try {
-							const { content } = await batchRequest.addURL(new URL(resourceURL, baseURI).href);
-							if (!content.startsWith(prefixDataURI) && !content.startsWith(PREFIX_DATA_URI_NO_MIMETYPE) && !content.startsWith(PREFIX_DATA_URI_OCTET_STREAM)) {
+					if (!DomUtil.testIgnoredPath(resourceURL)) {
+						if (DomUtil.testValidURL(resourceURL, baseURI)) {
+							try {
+								const { content } = await batchRequest.addURL(new URL(resourceURL, baseURI).href);
+								if (!content.startsWith(prefixDataURI) && !content.startsWith(PREFIX_DATA_URI_NO_MIMETYPE) && !content.startsWith(PREFIX_DATA_URI_OCTET_STREAM)) {
+									resourceElement.setAttribute(attributeName, EMPTY_IMAGE);
+								}
+								return content + (srcsetValue.w ? " " + srcsetValue.w + "w" : srcsetValue.d ? " " + srcsetValue.d + "x" : "");
+							} catch (error) {
 								resourceElement.setAttribute(attributeName, EMPTY_IMAGE);
 							}
-							return content + (srcsetValue.w ? " " + srcsetValue.w + "w" : srcsetValue.d ? " " + srcsetValue.d + "x" : "");
-						} catch (error) {
+						} else {
 							resourceElement.setAttribute(attributeName, EMPTY_IMAGE);
 						}
 					}
@@ -1174,7 +1183,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 
 	class DomUtil {
 		static normalizeURL(url) {
-			if (url.startsWith(DATA_URI_PREFIX)) {
+			if (!url || url.startsWith(DATA_URI_PREFIX)) {
 				return url;
 			} else {
 				return url.split("#")[0];
@@ -1230,8 +1239,16 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			return match && match[1];
 		}
 
-		static testValidPath(resourceURL) {
-			return !resourceURL.startsWith(DATA_URI_PREFIX) && !resourceURL.startsWith(BLOB_URI_PREFIX) && resourceURL != ABOUT_BLANK_URI && (!resourceURL.match(HTTP_URI_PREFIX) || resourceURL.match(NOT_EMPTY_URL));
+		static testIgnoredPath(resourceURL) {
+			return resourceURL && (resourceURL.startsWith(DATA_URI_PREFIX) || resourceURL.startsWith(BLOB_URI_PREFIX) || resourceURL == ABOUT_BLANK_URI);
+		}
+
+		static testValidPath(resourceURL, baseURI) {
+			return resourceURL && resourceURL != baseURI;
+		}
+
+		static testValidURL(resourceURL, baseURI) {
+			return DomUtil.testValidPath(resourceURL, baseURI) && resourceURL.match(HTTP_URI_PREFIX) && resourceURL.match(NOT_EMPTY_URL);
 		}
 
 		static matchImport(stylesheetContent) {