Prechádzať zdrojové kódy

don't test if resource URL is the same than page URL or base URI (cf #159)

Gildas 7 rokov pred
rodič
commit
eb10d01854
1 zmenil súbory, kde vykonal 21 pridanie a 21 odobranie
  1. 21 21
      lib/single-file/single-file-core.js

+ 21 - 21
lib/single-file/single-file-core.js

@@ -749,7 +749,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 				if (this.options.compressCSS) {
 					styleContent = docUtil.compressCSS(styleContent);
 				}
-				styleContent = ProcessorHelper.resolveStylesheetURLs(styleContent, this.baseURI, this.options);
+				styleContent = ProcessorHelper.resolveStylesheetURLs(styleContent, this.baseURI);
 				const declarationList = cssTree.parse(styleContent, { context: "declarationList" });
 				this.styles.set(element, declarationList);
 			});
@@ -842,7 +842,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 				options.doc = null;
 				options.win = null;
 				options.url = resourceURL;
-				if (!Util.testIgnoredPath(resourceURL) && Util.testValidPath(resourceURL, this.baseURI, this.options.url)) {
+				if (!Util.testIgnoredPath(resourceURL) && Util.testValidPath(resourceURL)) {
 					const processor = new Runner(options);
 					this.relImportProcessors.set(linkElement, processor);
 					await processor.loadPage();
@@ -920,7 +920,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 				} catch (error) {
 					// ignored
 				}
-				if (Util.testValidURL(resourceURL, this.baseURI, this.options.url)) {
+				if (Util.testValidURL(resourceURL)) {
 					this.stats.add("processed", "scripts", 1);
 					const content = await docUtil.getContent(resourceURL, { asDataURI: true, maxResourceSize: this.options.maxResourceSize, maxResourceSizeEnabled: this.options.maxResourceSizeEnabled });
 					scriptElement.setAttribute("src", content.data);
@@ -1220,19 +1220,19 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 		}
 
 		static async resolveImportURLs(stylesheetContent, baseURI, options, workStylesheet) {
-			stylesheetContent = ProcessorHelper.resolveStylesheetURLs(stylesheetContent, baseURI, options);
+			stylesheetContent = ProcessorHelper.resolveStylesheetURLs(stylesheetContent, baseURI);
 			const imports = Util.getImportFunctions(stylesheetContent);
 			await Promise.all(imports.map(async cssImport => {
 				const match = Util.matchImport(cssImport);
 				if (match) {
 					let resourceURL = Util.normalizeURL(match.resourceURL);
-					if (!Util.testIgnoredPath(resourceURL) && Util.testValidPath(resourceURL, baseURI, options.url)) {
+					if (!Util.testIgnoredPath(resourceURL) && Util.testValidPath(resourceURL)) {
 						try {
 							resourceURL = docUtil.resolveURL(match.resourceURL, baseURI);
 						} catch (error) {
 							// ignored
 						}
-						if (Util.testValidURL(resourceURL, baseURI, options.url)) {
+						if (Util.testValidURL(resourceURL)) {
 							const downloadOptions = { asDataURI: false, maxResourceSize: options.maxResourceSize, maxResourceSizeEnabled: options.maxResourceSizeEnabled, validateTextContentType: true };
 							const content = await docUtil.getContent(resourceURL, downloadOptions);
 							resourceURL = content.resourceURL;
@@ -1257,20 +1257,20 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			return stylesheetContent;
 		}
 
-		static resolveStylesheetURLs(stylesheetContent, baseURI, options) {
+		static resolveStylesheetURLs(stylesheetContent, baseURI) {
 			const urlFunctions = Util.getUrlFunctions(stylesheetContent);
 			urlFunctions.map(urlFunction => {
 				const originalResourceURL = Util.matchURL(urlFunction);
 				const resourceURL = Util.normalizeURL(originalResourceURL);
 				if (!Util.testIgnoredPath(resourceURL)) {
-					if (!resourceURL || Util.testValidPath(resourceURL, baseURI, options.url)) {
+					if (!resourceURL || Util.testValidPath(resourceURL)) {
 						let resolvedURL;
 						try {
 							resolvedURL = docUtil.resolveURL(resourceURL, baseURI);
 						} catch (error) {
 							// ignored
 						}
-						if (Util.testValidURL(resolvedURL, baseURI, options.url) && resourceURL != resolvedURL && stylesheetContent.includes(urlFunction)) {
+						if (Util.testValidURL(resolvedURL) && resourceURL != resolvedURL && stylesheetContent.includes(urlFunction)) {
 							stylesheetContent = stylesheetContent.replace(Util.getRegExp(urlFunction), urlFunction.replace(originalResourceURL, resolvedURL));
 						}
 					} else {
@@ -1337,7 +1337,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 							const originalResourceURL = Util.matchURL(urlFunction);
 							const resourceURL = Util.normalizeURL(originalResourceURL);
 							if (!Util.testIgnoredPath(resourceURL)) {
-								if (Util.testValidURL(resourceURL, baseURI, options.url)) {
+								if (Util.testValidURL(resourceURL)) {
 									let { content } = await batchRequest.addURL(resourceURL, true);
 									if (content != EMPTY_DATA_URI && !content.startsWith(PREFIX_DATA_URI_VND) && !content.startsWith(PREFIX_DATA_URI_IMAGE_SVG)) {
 										const validResource = await docUtil.validFont(urlFunction);
@@ -1375,7 +1375,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 						const originalResourceURL = Util.matchURL(urlFunction);
 						const resourceURL = Util.normalizeURL(originalResourceURL);
 						if (!Util.testIgnoredPath(resourceURL)) {
-							if (Util.testValidURL(resourceURL, baseURI, options.url)) {
+							if (Util.testValidURL(resourceURL)) {
 								let { content, indexResource, duplicate } = await batchRequest.addURL(resourceURL, true, true);
 								let variableDefined;
 								const tokens = [];
@@ -1416,13 +1416,13 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 				resourceURL = Util.normalizeURL(resourceURL);
 				if (!Util.testIgnoredPath(resourceURL)) {
 					resourceElement.setAttribute(attributeName, EMPTY_IMAGE);
-					if (Util.testValidPath(resourceURL, baseURI, options.url)) {
+					if (Util.testValidPath(resourceURL)) {
 						try {
 							resourceURL = docUtil.resolveURL(resourceURL, baseURI);
 						} catch (error) {
 							// ignored
 						}
-						if (Util.testValidURL(resourceURL, baseURI, options.url)) {
+						if (Util.testValidURL(resourceURL)) {
 							const { content, indexResource, duplicate } = await batchRequest.addURL(resourceURL, true, resourceElement.tagName == "IMG" && attributeName == "src");
 							if (removeElementIfMissing && content == EMPTY_DATA_URI) {
 								resourceElement.remove();
@@ -1453,14 +1453,14 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			await Promise.all(Array.from(resourceElements).map(async resourceElement => {
 				const originalResourceURL = resourceElement.getAttribute(attributeName);
 				let resourceURL = Util.normalizeURL(originalResourceURL);
-				if (Util.testValidPath(resourceURL, baseURI, options.url) && !Util.testIgnoredPath(resourceURL)) {
+				if (Util.testValidPath(resourceURL) && !Util.testIgnoredPath(resourceURL)) {
 					resourceElement.setAttribute(attributeName, EMPTY_IMAGE);
 					try {
 						resourceURL = docUtil.resolveURL(resourceURL, baseURI);
 					} catch (error) {
 						// ignored
 					}
-					if (Util.testValidURL(resourceURL, baseURI, options.url)) {
+					if (Util.testValidURL(resourceURL)) {
 						try {
 							const { content } = await batchRequest.addURL(resourceURL);
 							const hashMatch = originalResourceURL.match(REGEXP_URL_HASH);
@@ -1490,13 +1490,13 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 				const srcsetValues = await Promise.all(srcset.map(async srcsetValue => {
 					let resourceURL = Util.normalizeURL(srcsetValue.url);
 					if (!Util.testIgnoredPath(resourceURL)) {
-						if (Util.testValidPath(resourceURL, baseURI, options.url)) {
+						if (Util.testValidPath(resourceURL)) {
 							try {
 								resourceURL = docUtil.resolveURL(resourceURL, baseURI);
 							} catch (error) {
 								// ignored
 							}
-							if (Util.testValidURL(resourceURL, baseURI, options.url)) {
+							if (Util.testValidURL(resourceURL)) {
 								const { content } = await batchRequest.addURL(resourceURL, true);
 								if (!content.startsWith(prefixDataURI) && !content.startsWith(PREFIX_DATA_URI_NO_MIMETYPE) && !content.match(PREFIX_DATA_URI_OCTET_STREAM) && !content.match(PREFIX_DATA_URI_NULL_STREAM)) {
 									resourceElement.setAttribute(attributeName, EMPTY_IMAGE);
@@ -1633,12 +1633,12 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			return resourceURL && (resourceURL.startsWith(DATA_URI_PREFIX) || resourceURL == ABOUT_BLANK_URI);
 		}
 
-		static testValidPath(resourceURL, baseURI, docURL) {
-			return resourceURL && resourceURL != baseURI && resourceURL != docURL && !resourceURL.match(EMPTY_URL);
+		static testValidPath(resourceURL) {
+			return resourceURL && !resourceURL.match(EMPTY_URL);
 		}
 
-		static testValidURL(resourceURL, baseURI, docURL) {
-			return Util.testValidPath(resourceURL, baseURI, docURL) && (resourceURL.match(HTTP_URI_PREFIX) || resourceURL.match(FILE_URI_PREFIX) || resourceURL.startsWith(BLOB_URI_PREFIX)) && resourceURL.match(NOT_EMPTY_URL);
+		static testValidURL(resourceURL) {
+			return Util.testValidPath(resourceURL) && (resourceURL.match(HTTP_URI_PREFIX) || resourceURL.match(FILE_URI_PREFIX) || resourceURL.startsWith(BLOB_URI_PREFIX)) && resourceURL.match(NOT_EMPTY_URL);
 		}
 
 		static matchImport(stylesheetContent) {