summaryrefslogtreecommitdiffstats
path: root/toolkit/modules/sessionstore/SessionStoreHelper.sys.mjs
blob: d443cdb6479e4f9eab60808f75a76bfc502e5abc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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;
  },
};