summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/sessionstore/SessionStoreHelper.sys.mjs
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-12 05:35:37 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-12 05:35:37 +0000
commita90a5cba08fdf6c0ceb95101c275108a152a3aed (patch)
tree532507288f3defd7f4dcf1af49698bcb76034855 /toolkit/modules/sessionstore/SessionStoreHelper.sys.mjs
parentAdding debian version 126.0.1-1. (diff)
downloadfirefox-a90a5cba08fdf6c0ceb95101c275108a152a3aed.tar.xz
firefox-a90a5cba08fdf6c0ceb95101c275108a152a3aed.zip
Merging upstream version 127.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/modules/sessionstore/SessionStoreHelper.sys.mjs')
-rw-r--r--toolkit/modules/sessionstore/SessionStoreHelper.sys.mjs110
1 files changed, 110 insertions, 0 deletions
diff --git a/toolkit/modules/sessionstore/SessionStoreHelper.sys.mjs b/toolkit/modules/sessionstore/SessionStoreHelper.sys.mjs
new file mode 100644
index 0000000000..d443cdb647
--- /dev/null
+++ b/toolkit/modules/sessionstore/SessionStoreHelper.sys.mjs
@@ -0,0 +1,110 @@
+/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
+ * This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+/**
+ * The external API exported by this module.
+ */
+export var SessionStoreHelper = Object.freeze({
+ buildRestoreData(formdata, scroll) {
+ return SessionStoreHelperInternal.buildRestoreData(formdata, scroll);
+ },
+});
+
+/**
+ * The internal API for the SessionStoreHelper module.
+ */
+var SessionStoreHelperInternal = {
+ /**
+ * Builds a single nsISessionStoreRestoreData tree for the provided |formdata|
+ * and |scroll| trees.
+ */
+ buildRestoreData(formdata, scroll) {
+ function addFormEntries(root, fields, isXpath) {
+ for (let [key, value] of Object.entries(fields)) {
+ switch (typeof value) {
+ case "string":
+ root.addTextField(isXpath, key, value);
+ break;
+ case "boolean":
+ root.addCheckbox(isXpath, key, value);
+ break;
+ case "object": {
+ if (value === null) {
+ break;
+ }
+ if (
+ value.hasOwnProperty("type") &&
+ value.hasOwnProperty("fileList")
+ ) {
+ root.addFileList(isXpath, key, value.type, value.fileList);
+ break;
+ }
+ if (
+ value.hasOwnProperty("selectedIndex") &&
+ value.hasOwnProperty("value")
+ ) {
+ root.addSingleSelect(
+ isXpath,
+ key,
+ value.selectedIndex,
+ value.value
+ );
+ break;
+ }
+ if (
+ value.hasOwnProperty("value") &&
+ value.hasOwnProperty("state")
+ ) {
+ root.addCustomElement(isXpath, key, value.value, value.state);
+ break;
+ }
+ if (
+ key === "sessionData" &&
+ ["about:sessionrestore", "about:welcomeback"].includes(
+ formdata.url
+ )
+ ) {
+ root.addTextField(isXpath, key, JSON.stringify(value));
+ break;
+ }
+ if (Array.isArray(value)) {
+ root.addMultipleSelect(isXpath, key, value);
+ break;
+ }
+ }
+ }
+ }
+ }
+
+ let root = SessionStoreUtils.constructSessionStoreRestoreData();
+ if (scroll?.hasOwnProperty("scroll")) {
+ root.scroll = scroll.scroll;
+ }
+ if (formdata?.hasOwnProperty("url")) {
+ root.url = formdata.url;
+ if (formdata.hasOwnProperty("innerHTML")) {
+ // eslint-disable-next-line no-unsanitized/property
+ root.innerHTML = formdata.innerHTML;
+ }
+ if (formdata.hasOwnProperty("xpath")) {
+ addFormEntries(root, formdata.xpath, /* isXpath */ true);
+ }
+ if (formdata.hasOwnProperty("id")) {
+ addFormEntries(root, formdata.id, /* isXpath */ false);
+ }
+ }
+ let childrenLength = Math.max(
+ scroll?.children?.length || 0,
+ formdata?.children?.length || 0
+ );
+ for (let i = 0; i < childrenLength; i++) {
+ root.addChild(
+ this.buildRestoreData(formdata?.children?.[i], scroll?.children?.[i]),
+ i
+ );
+ }
+ return root;
+ },
+};