Przeglądaj źródła

don't fetch the current page (fix #764)

Gildas 4 lat temu
rodzic
commit
1fcd4dd670
1 zmienionych plików z 11 dodań i 4 usunięć
  1. 11 4
      lib/single-file/single-file-core.js

+ 11 - 4
lib/single-file/single-file-core.js

@@ -1179,7 +1179,7 @@ class Processor {
 			ProcessorHelper.processAttribute(this.doc.querySelectorAll(selector), attributeName, this.baseURI, this.options, this.cssVariables, this.styles, this.batchRequest, processDuplicates, removeElementIfMissing)
 		);
 		resourcePromises = resourcePromises.concat([
-			ProcessorHelper.processXLinks(this.doc.querySelectorAll("use"), this.baseURI, this.options, this.batchRequest),
+			ProcessorHelper.processXLinks(this.doc.querySelectorAll("use"), this.doc, this.baseURI, this.options, this.batchRequest),
 			ProcessorHelper.processSrcset(this.doc.querySelectorAll("img[srcset], source[srcset]"), "srcset", this.baseURI, this.batchRequest)
 		]);
 		if (!this.options.removeAudioSrc) {
@@ -1892,7 +1892,7 @@ class ProcessorHelper {
 		}));
 	}
 
-	static async processXLinks(resourceElements, baseURI, options, batchRequest) {
+	static async processXLinks(resourceElements, doc, baseURI, options, batchRequest) {
 		let attributeName = "xlink:href";
 		await Promise.all(Array.from(resourceElements).map(async resourceElement => {
 			let originalResourceURL = resourceElement.getAttribute(attributeName);
@@ -1909,12 +1909,18 @@ class ProcessorHelper {
 					// ignored
 				}
 				if (testValidURL(resourceURL)) {
-					const { content } = await batchRequest.addURL(resourceURL);
+					let svgDoc;
+					if (!originalResourceURL.startsWith(baseURI + "#")) {
+						const content = await batchRequest.addURL(resourceURL);
+						svgDoc = util.parseSVGContent(content);
+					} else {
+						svgDoc = doc;
+					}
 					const hashMatch = originalResourceURL.match(REGEXP_URL_HASH);
 					if (hashMatch && hashMatch[0]) {
 						let symbolElement;
 						try {
-							symbolElement = util.parseSVGContent(content).querySelector(hashMatch[0]);
+							symbolElement = svgDoc.querySelector(hashMatch[0]);
 						} catch (error) {
 							// ignored
 						}
@@ -1923,6 +1929,7 @@ class ProcessorHelper {
 							resourceElement.parentElement.insertBefore(symbolElement, resourceElement.parentElement.firstChild);
 						}
 					} else {
+						const content = await batchRequest.addURL(resourceURL);
 						resourceElement.setAttribute(attributeName, PREFIX_DATA_URI_IMAGE_SVG + "," + content);
 					}
 				}