Gildas 7 лет назад
Родитель
Сommit
3ec2420a4a

+ 21 - 1
lib/single-file/doc-helper.js

@@ -24,6 +24,7 @@ this.docHelper = this.docHelper || (() => {
 	const PRESERVED_SPACE_ELEMENT_ATTRIBUTE_NAME = "data-single-file-preserved-space-element";
 	const WIN_ID_ATTRIBUTE_NAME = "data-frame-tree-win-id";
 	const RESPONSIVE_IMAGE_ATTRIBUTE_NAME = "data-single-file-responsive-image";
+	const INPUT_VALUE_ATTRIBUTE_NAME = "data-single-file-value";
 
 	return {
 		preProcessDoc,
@@ -32,7 +33,8 @@ this.docHelper = this.docHelper || (() => {
 		windowIdAttributeName,
 		preservedSpaceAttributeName,
 		removedContentAttributeName,
-		responsiveImagesAttributeName
+		responsiveImagesAttributeName,
+		inputValueAttributeName
 	};
 
 	function preProcessDoc(doc, win, options) {
@@ -60,6 +62,7 @@ this.docHelper = this.docHelper || (() => {
 				}
 			});
 		}
+		retrieveInputValues(doc, options);
 		return {
 			canvasData: getCanvasData(doc),
 			stylesheetContents: getStylesheetContents(doc),
@@ -81,6 +84,7 @@ this.docHelper = this.docHelper || (() => {
 			doc.querySelectorAll("[" + preservedSpaceAttributeName(options.sessionId) + "]").forEach(element => element.removeAttribute(preservedSpaceAttributeName(options.sessionId)));
 		}
 		doc.querySelectorAll("[" + responsiveImagesAttributeName(options.sessionId) + "]").forEach(element => element.removeAttribute(responsiveImagesAttributeName(options.sessionId)));
+		doc.querySelectorAll("[" + inputValueAttributeName(options.sessionId) + "]").forEach(element => element.removeAttribute(inputValueAttributeName(options.sessionId)));
 	}
 
 	function responsiveImagesAttributeName(sessionId) {
@@ -99,6 +103,10 @@ this.docHelper = this.docHelper || (() => {
 		return WIN_ID_ATTRIBUTE_NAME + (sessionId ? "-" + sessionId : "");
 	}
 
+	function inputValueAttributeName(sessionId) {
+		return INPUT_VALUE_ATTRIBUTE_NAME + (sessionId ? "-" + sessionId : "");
+	}
+
 	function getCanvasData(doc) {
 		if (doc) {
 			const canvasData = [];
@@ -167,6 +175,18 @@ this.docHelper = this.docHelper || (() => {
 		}
 	}
 
+	function retrieveInputValues(doc, options) {
+		doc.querySelectorAll("input").forEach(input => input.setAttribute(inputValueAttributeName(options.sessionId), input.value));
+		doc.querySelectorAll("textarea").forEach(textarea => textarea.setAttribute(inputValueAttributeName(options.sessionId), textarea.value));
+		doc.querySelectorAll("select").forEach(select => {
+			select.querySelectorAll("option").forEach(option => {
+				if (option.selected) {
+					option.setAttribute(inputValueAttributeName(options.sessionId), "");
+				}
+			});
+		});
+	}
+
 	function serialize(doc) {
 		const docType = doc.doctype;
 		let docTypeString = "";

+ 4 - 0
lib/single-file/single-file-browser.js

@@ -201,6 +201,10 @@ this.SingleFile = this.SingleFile || (() => {
 		static responsiveImagesAttributeName(sessionId) {
 			return docHelper.responsiveImagesAttributeName(sessionId);
 		}
+
+		static inputValueAttributeName(sessionId) {
+			return docHelper.inputValueAttributeName(sessionId);
+		}
 	}
 
 	return { getClass: () => SingleFileCore.getClass(Download, DOM, URL) };

+ 24 - 0
lib/single-file/single-file-core.js

@@ -78,6 +78,7 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			{ option: "removeScripts", action: "removeScripts" },
 			{ action: "removeDiscardedResources" },
 			{ action: "resetCharsetMeta" },
+			{ action: "setInputValues" },
 			{ option: "compressHTML", action: "compressHTML" },
 			{ option: "insertFaviconLink", action: "insertFaviconLink" },
 			{ action: "resolveHrefs" },
@@ -306,6 +307,29 @@ this.SingleFileCore = this.SingleFileCore || (() => {
 			};
 		}
 
+		setInputValues() {
+			this.doc.querySelectorAll("input").forEach(input => {
+				const value = input.getAttribute(DOM.inputValueAttributeName(this.options.sessionId));
+				if (value) {
+					input.setAttribute("value", value);
+				}
+			});
+			this.doc.querySelectorAll("textarea").forEach(textarea => {
+				const value = textarea.getAttribute(DOM.inputValueAttributeName(this.options.sessionId));
+				if (value) {
+					textarea.textContent = value;
+				}
+			});
+			this.doc.querySelectorAll("select").forEach(select => {
+				select.querySelectorAll("option").forEach(option => {
+					const selected = option.getAttribute(DOM.inputValueAttributeName(this.options.sessionId)) !== undefined;
+					if (selected) {
+						option.setAttribute("selected", "");
+					}
+				});
+			});
+		}
+
 		lazyLoadImages() {
 			DOM.lazyLoad(this.doc);
 		}