summaryrefslogtreecommitdiffstats
path: root/devtools/server/actors/highlighters/tabbing-order.js
blob: ccc70779bb9c539aa2e56dfa4a06f9a0fe99a059 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/* 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";

const lazy = {};
loader.lazyGetter(
  lazy,
  "ContentDOMReference",
  () =>
    ChromeUtils.importESModule(
      "resource://gre/modules/ContentDOMReference.sys.mjs",
      // ContentDOMReference needs to be retrieved from the shared global
      // since it is a shared singleton.
      { global: "shared" }
    ).ContentDOMReference
);
loader.lazyRequireGetter(
  this,
  ["isFrameWithChildTarget", "isWindowIncluded"],
  "resource://devtools/shared/layout/utils.js",
  true
);
loader.lazyRequireGetter(
  this,
  "NodeTabbingOrderHighlighter",
  "resource://devtools/server/actors/highlighters/node-tabbing-order.js",
  true
);

const DEFAULT_FOCUS_FLAGS = Services.focus.FLAG_NOSCROLL;

/**
 * The TabbingOrderHighlighter uses focus manager to traverse all focusable
 * nodes on the page and then uses the NodeTabbingOrderHighlighter to highlight
 * these nodes.
 */
class TabbingOrderHighlighter {
  constructor(highlighterEnv) {
    this.highlighterEnv = highlighterEnv;
    this._highlighters = new Map();

    this.onMutation = this.onMutation.bind(this);
    this.onPageHide = this.onPageHide.bind(this);
    this.onWillNavigate = this.onWillNavigate.bind(this);

    this.highlighterEnv.on("will-navigate", this.onWillNavigate);

    const { pageListenerTarget } = highlighterEnv;
    pageListenerTarget.addEventListener("pagehide", this.onPageHide);
  }

  /**
   * Static getter that indicates that TabbingOrderHighlighter supports
   * highlighting in XUL windows.
   */
  static get XULSupported() {
    return true;
  }

  get win() {
    return this.highlighterEnv.window;
  }

  get focusedElement() {
    return Services.focus.getFocusedElementForWindow(this.win, true, {});
  }

  set focusedElement(element) {
    Services.focus.setFocus(element, DEFAULT_FOCUS_FLAGS);
  }

  moveFocus(startElement) {
    return Services.focus.moveFocus(
      this.win,
      startElement.nodeType === Node.DOCUMENT_NODE
        ? startElement.documentElement
        : startElement,
      Services.focus.MOVEFOCUS_FORWARD,
      DEFAULT_FOCUS_FLAGS
    );
  }

  /**
   * Show NodeTabbingOrderHighlighter on each node that belongs to the keyboard
   * tabbing order.
   *
   * @param  {DOMNode} startElm
   *         Starting element to calculate tabbing order from.
   *
   * @param  {JSON} options
   *         - options.index
   *           Start index for the tabbing order. Starting index will be 0 at
   *           the start of the tabbing order highlighting; in remote frames
   *           starting index will, typically, be greater than 0 (unless there
   *           was nothing to focus in the top level content document prior to
   *           the remote frame).
   */
  async show(startElm, { index }) {
    const focusableElements = [];
    const originalFocusedElement = this.focusedElement;
    let currentFocusedElement = this.moveFocus(startElm);
    while (
      currentFocusedElement &&
      isWindowIncluded(this.win, currentFocusedElement.ownerGlobal)
    ) {
      focusableElements.push(currentFocusedElement);
      currentFocusedElement = this.moveFocus(currentFocusedElement);
    }

    // Allow to flush pending notifications to ensure the PresShell and frames
    // are updated.
    await new Promise(resolve => Services.tm.dispatchToMainThread(resolve));
    let endElm = this.focusedElement;
    if (
      currentFocusedElement &&
      !isWindowIncluded(this.win, currentFocusedElement.ownerGlobal)
    ) {
      endElm = null;
    }

    if (
      !endElm &&
      !!focusableElements.length &&
      isFrameWithChildTarget(
        this.highlighterEnv.targetActor,
        focusableElements[focusableElements.length - 1]
      )
    ) {
      endElm = focusableElements[focusableElements.length - 1];
    }

    if (originalFocusedElement && originalFocusedElement !== endElm) {
      this.focusedElement = originalFocusedElement;
    }

    const highlighters = [];
    for (let i = 0; i < focusableElements.length; i++) {
      highlighters.push(
        this._accumulateHighlighter(focusableElements[i], index++)
      );
    }
    await Promise.all(highlighters);

    this._trackMutations();

    return {
      contentDOMReference: endElm && lazy.ContentDOMReference.get(endElm),
      index,
    };
  }

  async _accumulateHighlighter(node, index) {
    const highlighter = new NodeTabbingOrderHighlighter(this.highlighterEnv);
    await highlighter.isReady;

    highlighter.show(node, { index: index + 1 });
    this._highlighters.set(node, highlighter);
  }

  hide() {
    this._untrackMutations();
    for (const highlighter of this._highlighters.values()) {
      highlighter.destroy();
    }

    this._highlighters.clear();
  }

  /**
   * Track mutations in the top level document subtree so that the appropriate
   * NodeTabbingOrderHighlighter infobar's could be updated to reflect the
   * attribute mutations on relevant nodes.
   */
  _trackMutations() {
    const { win } = this;
    this.currentMutationObserver = new win.MutationObserver(this.onMutation);
    this.currentMutationObserver.observe(win.document.documentElement, {
      subtree: true,
      attributes: true,
    });
  }

  _untrackMutations() {
    if (!this.currentMutationObserver) {
      return;
    }

    this.currentMutationObserver.disconnect();
    this.currentMutationObserver = null;
  }

  onMutation(mutationList) {
    for (const { target } of mutationList) {
      const highlighter = this._highlighters.get(target);
      if (highlighter) {
        highlighter.update();
      }
    }
  }

  /**
   * Update NodeTabbingOrderHighlighter focus styling for a node that,
   * potentially, belongs to the tabbing order.
   * @param {Object} options
   *        Options specifying the node and its focused state.
   */
  updateFocus({ node, focused }) {
    const highlighter = this._highlighters.get(node);
    if (!highlighter) {
      return;
    }

    highlighter.updateFocus(focused);
  }

  destroy() {
    this.highlighterEnv.off("will-navigate", this.onWillNavigate);

    const { pageListenerTarget } = this.highlighterEnv;
    if (pageListenerTarget) {
      pageListenerTarget.removeEventListener("pagehide", this.onPageHide);
    }

    this.hide();
    this.highlighterEnv = null;
  }

  onPageHide({ target }) {
    // If a pagehide event is triggered for current window's highlighter, hide
    // the highlighter.
    if (target.defaultView === this.win) {
      this.hide();
    }
  }

  onWillNavigate({ isTopLevel }) {
    if (isTopLevel) {
      this.hide();
    }
  }
}

exports.TabbingOrderHighlighter = TabbingOrderHighlighter;