Explorar el Código

improved the cleaning of singlefile attributes

Former-commit-id: 421a25a544c27cb192927ceceeaa43a1371b65f7
Gildas hace 6 años
padre
commit
cd981ef126

+ 1 - 1
extension/core/content/content-bootstrap.js

@@ -88,7 +88,7 @@ this.singlefile.extension.core.content.bootstrap = this.singlefile.extension.cor
 					frames: frames,
 					url: location.href
 				});
-				helper.postProcessDoc(document, options);
+				helper.postProcessDoc(document, docData.markedElements);
 				pageAutoSaved = true;
 				autoSavingPage = false;
 			}

+ 1 - 1
lib/frame-tree/content/content-frame-tree.js

@@ -261,7 +261,7 @@ this.singlefile.lib.frameTree.content.frames = this.singlefile.lib.frameTree.con
 		const helper = singlefile.lib.helper;
 		const docData = helper.preProcessDoc(document, window, options);
 		const content = helper.serialize(document);
-		helper.postProcessDoc(document, options);
+		helper.postProcessDoc(document, docData.markedElements);
 		const baseURI = document.baseURI.split("#")[0];
 		return {
 			windowId,

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

@@ -180,7 +180,7 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 			await this.executeStage(RESOLVE_URLS_STAGE);
 			this.pendingPromises = this.executeStage(REPLACE_DATA_STAGE);
 			if (this.options.doc) {
-				util.postProcessDoc(this.options.doc, this.options);
+				util.postProcessDoc(this.options.doc, this.markedElements);
 			}
 			this.options.doc = null;
 			this.options.win = null;
@@ -416,7 +416,7 @@ this.singlefile.lib.core = this.singlefile.lib.core || (() => {
 		}
 
 		async getPageData() {
-			util.postProcessDoc(this.doc, this.options);
+			util.postProcessDoc(this.doc);
 			const url = util.parseURL(this.baseURI);
 			if (this.options.insertSingleFileComment) {
 				const infobarContent = (this.options.infobarContent || "").replace(/\\n/g, "\n").replace(/\\t/g, "\t");

+ 30 - 15
lib/single-file/single-file-helper.js

@@ -92,7 +92,8 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 				posters: [],
 				usedFonts: [],
 				shadowRoots: [],
-				imports: []
+				imports: [],
+				markedElements: []
 			};
 		}
 		return {
@@ -104,11 +105,12 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 			usedFonts: Array.from(elementsInfo.usedFonts.values()),
 			shadowRoots: elementsInfo.shadowRoots,
 			imports: elementsInfo.imports,
-			referrer: doc.referrer
+			referrer: doc.referrer,
+			markedElements: elementsInfo.markedElements
 		};
 	}
 
-	function getElementsInfo(win, doc, element, options, data = { usedFonts: new Map(), canvases: [], images: [], posters: [], shadowRoots: [], imports: [] }, ascendantHidden) {
+	function getElementsInfo(win, doc, element, options, data = { usedFonts: new Map(), canvases: [], images: [], posters: [], shadowRoots: [], imports: [], markedElements: [] }, ascendantHidden) {
 		const elements = Array.from(element.childNodes).filter(node => node instanceof win.HTMLElement);
 		elements.forEach(element => {
 			let elementHidden, computedStyle;
@@ -119,6 +121,7 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 						Array.from(element.childNodes).filter(node => node instanceof win.HTMLElement).forEach(element => {
 							if (!IGNORED_REMOVED_TAG_NAMES.includes(element.tagName)) {
 								element.setAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME, "");
+								data.markedElements.push(element);
 							}
 						});
 					}
@@ -129,6 +132,7 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 						const whiteSpace = computedStyle.getPropertyValue("white-space");
 						if (whiteSpace && whiteSpace.startsWith("pre")) {
 							element.setAttribute(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME, "");
+							data.markedElements.push(element);
 						}
 					}
 					if (options.removeUnusedFonts) {
@@ -143,6 +147,7 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 			if (element.shadowRoot) {
 				const shadowRootInfo = {};
 				element.setAttribute(SHADOW_ROOT_ATTRIBUTE_NAME, data.shadowRoots.length);
+				data.markedElements.push(element);
 				data.shadowRoots.push(shadowRootInfo);
 				getElementsInfo(win, doc, element.shadowRoot, options, data, elementHidden);
 				shadowRootInfo.content = element.shadowRoot.innerHTML;
@@ -160,6 +165,7 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 				const size = getSize(win, element, computedStyle);
 				data.canvases.push({ dataURI: element.toDataURL("image/png", ""), width: size.width, height: size.height });
 				element.setAttribute(CANVAS_ATTRIBUTE_NAME, data.canvases.length - 1);
+				data.markedElements.push(element);
 			} catch (error) {
 				// ignored
 			}
@@ -172,6 +178,7 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 			};
 			data.images.push(imageData);
 			element.setAttribute(IMAGE_ATTRIBUTE_NAME, data.images.length - 1);
+			data.markedElements.push(element);
 			element.removeAttribute(LAZY_SRC_ATTRIBUTE_NAME);
 			computedStyle = computedStyle || win.getComputedStyle(element);
 			if (computedStyle) {
@@ -199,6 +206,7 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 					context.drawImage(element, 0, 0, canvasElement.width, canvasElement.height);
 					data.posters.push(canvasElement.toDataURL("image/png", ""));
 					element.setAttribute(POSTER_ATTRIBUTE_NAME, data.posters.length - 1);
+					data.markedElements.push(element);
 				} catch (error) {
 					// ignored
 				}
@@ -213,23 +221,28 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 			if (element.import) {
 				data.imports.push({ content: serialize(element.import) });
 				element.setAttribute(HTML_IMPORT_ATTRIBUTE_NAME, data.imports.length - 1);
+				data.markedElements.push(element);
 			}
 		}
 		if (element.tagName == "INPUT") {
 			if (element.type != "password") {
 				element.setAttribute(INPUT_VALUE_ATTRIBUTE_NAME, element.value);
+				data.markedElements.push(element);
 			}
 			if (element.type == "radio" || element.type == "checkbox") {
 				element.setAttribute(INPUT_VALUE_ATTRIBUTE_NAME, element.checked);
+				data.markedElements.push(element);
 			}
 		}
 		if (element.tagName == "TEXTAREA") {
 			element.setAttribute(INPUT_VALUE_ATTRIBUTE_NAME, element.value);
+			data.markedElements.push(element);
 		}
 		if (element.tagName == "SELECT") {
 			element.querySelectorAll("option").forEach(option => {
 				if (option.selected) {
 					option.setAttribute(INPUT_VALUE_ATTRIBUTE_NAME, "");
+					data.markedElements.push(option);
 				}
 			});
 		}
@@ -269,7 +282,7 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 		return Boolean(hidden);
 	}
 
-	function postProcessDoc(doc, options) {
+	function postProcessDoc(doc, markedElements) {
 		doc.querySelectorAll("disabled-noscript").forEach(element => {
 			const noscriptElement = doc.createElement("noscript");
 			Array.from(element.childNodes).forEach(node => noscriptElement.appendChild(node));
@@ -282,18 +295,20 @@ this.singlefile.lib.helper = this.singlefile.lib.helper || (() => {
 		if (doc.head) {
 			doc.head.querySelectorAll("*:not(base):not(link):not(meta):not(noscript):not(script):not(style):not(template):not(title)").forEach(element => element.removeAttribute("hidden"));
 		}
-		if (options.removeHiddenElements) {
-			doc.querySelectorAll("[" + REMOVED_CONTENT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME));
+		if (!markedElements) {
+			const singleFileAttributes = [REMOVED_CONTENT_ATTRIBUTE_NAME, PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME, IMAGE_ATTRIBUTE_NAME, POSTER_ATTRIBUTE_NAME, CANVAS_ATTRIBUTE_NAME, INPUT_VALUE_ATTRIBUTE_NAME, SHADOW_ROOT_ATTRIBUTE_NAME, HTML_IMPORT_ATTRIBUTE_NAME];
+			markedElements = doc.querySelectorAll(singleFileAttributes.map(name => "[" + name + "]").join(","));
 		}
-		if (options.compressHTML) {
-			doc.querySelectorAll("[" + PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME));
-		}
-		doc.querySelectorAll("[" + IMAGE_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(IMAGE_ATTRIBUTE_NAME));
-		doc.querySelectorAll("[" + POSTER_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(POSTER_ATTRIBUTE_NAME));
-		doc.querySelectorAll("[" + CANVAS_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(CANVAS_ATTRIBUTE_NAME));
-		doc.querySelectorAll("[" + INPUT_VALUE_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(INPUT_VALUE_ATTRIBUTE_NAME));
-		doc.querySelectorAll("[" + SHADOW_ROOT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(SHADOW_ROOT_ATTRIBUTE_NAME));
-		doc.querySelectorAll("[" + HTML_IMPORT_ATTRIBUTE_NAME + "]").forEach(element => element.removeAttribute(HTML_IMPORT_ATTRIBUTE_NAME));
+		markedElements.forEach(element => {
+			element.removeAttribute(REMOVED_CONTENT_ATTRIBUTE_NAME);
+			element.removeAttribute(PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME);
+			element.removeAttribute(IMAGE_ATTRIBUTE_NAME);
+			element.removeAttribute(POSTER_ATTRIBUTE_NAME);
+			element.removeAttribute(CANVAS_ATTRIBUTE_NAME);
+			element.removeAttribute(INPUT_VALUE_ATTRIBUTE_NAME);
+			element.removeAttribute(SHADOW_ROOT_ATTRIBUTE_NAME);
+			element.removeAttribute(HTML_IMPORT_ATTRIBUTE_NAME);
+		});
 	}
 
 	function getStylesheetsData(doc) {

+ 2 - 2
lib/single-file/single-file-util.js

@@ -140,8 +140,8 @@ this.singlefile.lib.util = this.singlefile.lib.util || (() => {
 				preProcessDoc(doc, win, options) {
 					return modules.helper.preProcessDoc(doc, win, options);
 				},
-				postProcessDoc(doc, options) {
-					modules.helper.postProcessDoc(doc, options);
+				postProcessDoc(doc, markedElements) {
+					modules.helper.postProcessDoc(doc, markedElements);
 				},
 				serialize(doc, compressHTML) {
 					return modules.serializer.process(doc, compressHTML);