Explorar o código

avoid processing empty src

Gildas %!s(int64=7) %!d(string=hai) anos
pai
achega
1e35fcaf15
Modificáronse 1 ficheiros con 21 adicións e 18 borrados
  1. 21 18
      lib/single-file/html-images-minifier.js

+ 21 - 18
lib/single-file/html-images-minifier.js

@@ -31,7 +31,7 @@ this.imagesMinifier = this.imagesMinifier || (() => {
 			let duplicates = new Set();
 			const duplicateURLs = [];
 			imageGroups.forEach((elements, src) => {
-				if (elements.length > 1) {
+				if (elements.length > 1 && src && src != options.baseURI) {
 					elements.forEach(element => duplicates.add(element));
 					duplicateURLs.push(src);
 				}
@@ -100,25 +100,28 @@ this.imagesMinifier = this.imagesMinifier || (() => {
 				replaceImage = !Object.keys(ignoredAttributeNames).map(key => ignoredAttributeNames[key]).find(attributeName => imgElement.getAttribute(attributeName));
 			}
 			if (replaceImage && duplicates.has(imgElement)) {
-				const dataAttributeName = docHelper.imagesAttributeName(options.sessionId);
-				const imageData = options.imageData[Number(imgElement.getAttribute(dataAttributeName))];
-				const svgElement = doc.createElementNS(SVG_NS, "svg");
-				const useElement = doc.createElementNS(SVG_NS, "use");
-				svgElement.appendChild(useElement);
-				imgElement.getAttributeNames().forEach(attributeName => attributeName != "src" && svgElement.setAttribute(attributeName, imgElement.getAttribute(attributeName)));
-				svgElement.setAttributeNS(SVG_NS, "viewBox", "0 0 " + imageData.naturalWidth + " " + imageData.naturalHeight);
-				svgElement.setAttributeNS(SVG_NS, "width", imageData.clientWidth);
-				svgElement.setAttributeNS(SVG_NS, "height", imageData.clientHeight);
-				svgElement.setAttributeNS(SVG_NS, "preserveAspectRatio", "none");
 				const urlIndex = duplicateURLs.indexOf(imgElement.src);
-				useElement.setAttributeNS(SVG_NS, "xlink:href", "#single-file-" + urlIndex);
-				const imageElement = doc.getElementById("single-file-" + urlIndex);
-				if (!imageElement.getAttributeNS(SVG_NS, "width") && !imageElement.getAttributeNS(SVG_NS, "height") && imageData.naturalWidth && imageData.naturalHeight) {
-					imageElement.setAttributeNS(SVG_NS, "viewBox", "0 0 " + imageData.naturalWidth + " " + imageData.naturalHeight);
-					imageElement.setAttributeNS(SVG_NS, "width", imageData.naturalWidth);
-					imageElement.setAttributeNS(SVG_NS, "height", imageData.naturalHeight);
+				if (urlIndex != -1) {
+					const dataAttributeName = docHelper.imagesAttributeName(options.sessionId);
+					const imageData = options.imageData[Number(imgElement.getAttribute(dataAttributeName))];
+					const svgElement = doc.createElementNS(SVG_NS, "svg");
+					const useElement = doc.createElementNS(SVG_NS, "use");
+					svgElement.appendChild(useElement);
+					imgElement.getAttributeNames().forEach(attributeName => attributeName != "src" && svgElement.setAttribute(attributeName, imgElement.getAttribute(attributeName)));
+					svgElement.setAttributeNS(SVG_NS, "viewBox", "0 0 " + imageData.naturalWidth + " " + imageData.naturalHeight);
+					svgElement.setAttributeNS(SVG_NS, "width", imageData.clientWidth);
+					svgElement.setAttributeNS(SVG_NS, "height", imageData.clientHeight);
+					svgElement.setAttributeNS(SVG_NS, "preserveAspectRatio", "none");
+					useElement.setAttributeNS(SVG_NS, "xlink:href", "#single-file-" + urlIndex);
+					const imageElement = doc.getElementById("single-file-" + urlIndex);
+					if (!imageElement.getAttributeNS(SVG_NS, "width") && !imageElement.getAttributeNS(SVG_NS, "height") && imageData.naturalWidth && imageData.naturalHeight) {
+						imageElement.setAttributeNS(SVG_NS, "viewBox", "0 0 " + imageData.naturalWidth + " " + imageData.naturalHeight);
+						imageElement.setAttributeNS(SVG_NS, "width", imageData.naturalWidth);
+						imageElement.setAttributeNS(SVG_NS, "height", imageData.naturalHeight);
+					}
+					svgElement.style.border = "1px solid red";
+					imgElement.parentElement.replaceChild(svgElement, imgElement);
 				}
-				imgElement.parentElement.replaceChild(svgElement, imgElement);
 			}
 		});
 	}