summaryrefslogtreecommitdiffstats
path: root/devtools/server/actors/inspector/document-walker.js
blob: 7ced18ecd85fcc479721d40a9b66810859990a1b (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* 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/. */

"use strict";

loader.lazyRequireGetter(
  this,
  "nodeFilterConstants",
  "resource://devtools/shared/dom-node-filter-constants.js"
);
loader.lazyRequireGetter(
  this,
  "standardTreeWalkerFilter",
  "resource://devtools/server/actors/inspector/utils.js",
  true
);

// SKIP_TO_* arguments are used with the DocumentWalker, driving the strategy to use if
// the starting node is incompatible with the filter function of the walker.
const SKIP_TO_PARENT = "SKIP_TO_PARENT";
const SKIP_TO_SIBLING = "SKIP_TO_SIBLING";

class DocumentWalker {
  /**
   * Wrapper for inDeepTreeWalker.  Adds filtering to the traversal methods.
   * See inDeepTreeWalker for more information about the methods.
   *
   * @param {DOMNode} node
   * @param {Window} rootWin
   * @param {Object}
   *        - {Function} filter
   *          A custom filter function Taking in a DOMNode and returning an Int. See
   *          WalkerActor.nodeFilter for an example.
   *        - {String} skipTo
   *          Either SKIP_TO_PARENT or SKIP_TO_SIBLING. If the provided node is not
   *          compatible with the filter function for this walker, try to find a compatible
   *          one either in the parents or in the siblings of the node.
   *        - {Boolean} showAnonymousContent
   *          Pass true to let the walker return and traverse anonymous content.
   *          When navigating host elements to which shadow DOM is attached, the light tree
   *          will be visible only to a walker with showAnonymousContent=false. The shadow
   *          tree will only be visible to a walker with showAnonymousContent=true.
   */
  constructor(
    node,
    rootWin,
    {
      filter = standardTreeWalkerFilter,
      skipTo = SKIP_TO_PARENT,
      showAnonymousContent = true,
    } = {}
  ) {
    if (Cu.isDeadWrapper(rootWin) || !rootWin.location) {
      throw new Error("Got an invalid root window in DocumentWalker");
    }

    this.walker = Cc[
      "@mozilla.org/inspector/deep-tree-walker;1"
    ].createInstance(Ci.inIDeepTreeWalker);
    this.walker.showAnonymousContent = showAnonymousContent;
    this.walker.showSubDocuments = true;
    this.walker.showDocumentsAsNodes = true;
    this.walker.init(rootWin.document);
    this.filter = filter;

    // Make sure that the walker knows about the initial node (which could
    // be skipped due to a filter).
    this.walker.currentNode = this.getStartingNode(node, skipTo);
  }

  get currentNode() {
    return this.walker.currentNode;
  }
  set currentNode(val) {
    this.walker.currentNode = val;
  }

  parentNode() {
    return this.walker.parentNode();
  }

  nextNode() {
    const node = this.walker.currentNode;
    if (!node) {
      return null;
    }

    let nextNode = this.walker.nextNode();
    while (nextNode && this.isSkippedNode(nextNode)) {
      nextNode = this.walker.nextNode();
    }

    return nextNode;
  }

  firstChild() {
    if (!this.walker.currentNode) {
      return null;
    }

    let firstChild = this.walker.firstChild();
    while (firstChild && this.isSkippedNode(firstChild)) {
      firstChild = this.walker.nextSibling();
    }

    return firstChild;
  }

  lastChild() {
    if (!this.walker.currentNode) {
      return null;
    }

    let lastChild = this.walker.lastChild();
    while (lastChild && this.isSkippedNode(lastChild)) {
      lastChild = this.walker.previousSibling();
    }

    return lastChild;
  }

  previousSibling() {
    let node = this.walker.previousSibling();
    while (node && this.isSkippedNode(node)) {
      node = this.walker.previousSibling();
    }
    return node;
  }

  nextSibling() {
    let node = this.walker.nextSibling();
    while (node && this.isSkippedNode(node)) {
      node = this.walker.nextSibling();
    }
    return node;
  }

  getStartingNode(node, skipTo) {
    // Keep a reference on the starting node in case we can't find a node compatible with
    // the filter.
    const startingNode = node;

    if (skipTo === SKIP_TO_PARENT) {
      while (node && this.isSkippedNode(node)) {
        node = node.parentNode;
      }
    } else if (skipTo === SKIP_TO_SIBLING) {
      node = this.getClosestAcceptedSibling(node);
    }

    return node || startingNode;
  }

  /**
   * Loop on all of the provided node siblings until finding one that is compliant with
   * the filter function.
   */
  getClosestAcceptedSibling(node) {
    if (this.filter(node) === nodeFilterConstants.FILTER_ACCEPT) {
      // node is already valid, return immediately.
      return node;
    }

    // Loop on starting node siblings.
    let previous = node;
    let next = node;
    while (previous || next) {
      previous = previous?.previousSibling;
      next = next?.nextSibling;

      if (
        previous &&
        this.filter(previous) === nodeFilterConstants.FILTER_ACCEPT
      ) {
        // A valid node was found in the previous siblings of the node.
        return previous;
      }

      if (next && this.filter(next) === nodeFilterConstants.FILTER_ACCEPT) {
        // A valid node was found in the next siblings of the node.
        return next;
      }
    }

    return null;
  }

  isSkippedNode(node) {
    return this.filter(node) === nodeFilterConstants.FILTER_SKIP;
  }
}

exports.DocumentWalker = DocumentWalker;
exports.SKIP_TO_PARENT = SKIP_TO_PARENT;
exports.SKIP_TO_SIBLING = SKIP_TO_SIBLING;