summaryrefslogtreecommitdiffstats
path: root/devtools/server/actors/highlighters/node-tabbing-order.js
blob: 229342ee98f156596433c48e53f1d9ddaa9f4214 (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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
/* 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,
  ["setIgnoreLayoutChanges", "getCurrentZoom"],
  "resource://devtools/shared/layout/utils.js",
  true
);
loader.lazyRequireGetter(
  this,
  "AutoRefreshHighlighter",
  "resource://devtools/server/actors/highlighters/auto-refresh.js",
  true
);
loader.lazyRequireGetter(
  this,
  ["CanvasFrameAnonymousContentHelper"],
  "resource://devtools/server/actors/highlighters/utils/markup.js",
  true
);

/**
 * The NodeTabbingOrderHighlighter draws an outline around a node (based on its
 * border bounds).
 *
 * Usage example:
 *
 * const h = new NodeTabbingOrderHighlighter(env);
 * await h.isReady();
 * h.show(node, options);
 * h.hide();
 * h.destroy();
 *
 * @param {Number} options.index
 *        Tabbing index value to be displayed in the highlighter info bar.
 */
class NodeTabbingOrderHighlighter extends AutoRefreshHighlighter {
  constructor(highlighterEnv) {
    super(highlighterEnv);

    this._doNotStartRefreshLoop = true;
    this.ID_CLASS_PREFIX = "tabbing-order-";
    this.markup = new CanvasFrameAnonymousContentHelper(
      this.highlighterEnv,
      this._buildMarkup.bind(this)
    );
    this.isReady = this.markup.initialize();
  }

  _buildMarkup() {
    const root = this.markup.createNode({
      attributes: {
        id: "root",
        class: "root highlighter-container tabbing-order",
        "aria-hidden": "true",
      },
      prefix: this.ID_CLASS_PREFIX,
    });

    const container = this.markup.createNode({
      parent: root,
      attributes: {
        id: "container",
        width: "100%",
        height: "100%",
        hidden: "true",
      },
      prefix: this.ID_CLASS_PREFIX,
    });

    // Building the SVG element
    this.markup.createNode({
      parent: container,
      attributes: {
        class: "bounds",
        id: "bounds",
      },
      prefix: this.ID_CLASS_PREFIX,
    });

    // Building the nodeinfo bar markup

    const infobarContainer = this.markup.createNode({
      parent: root,
      attributes: {
        class: "infobar-container",
        id: "infobar-container",
        position: "top",
        hidden: "true",
      },
      prefix: this.ID_CLASS_PREFIX,
    });

    const infobar = this.markup.createNode({
      parent: infobarContainer,
      attributes: {
        class: "infobar",
      },
      prefix: this.ID_CLASS_PREFIX,
    });

    this.markup.createNode({
      parent: infobar,
      attributes: {
        class: "infobar-text",
        id: "infobar-text",
      },
      prefix: this.ID_CLASS_PREFIX,
    });

    return root;
  }

  /**
   * Destroy the nodes. Remove listeners.
   */
  destroy() {
    this.markup.destroy();

    AutoRefreshHighlighter.prototype.destroy.call(this);
  }

  getElement(id) {
    return this.markup.getElement(this.ID_CLASS_PREFIX + id);
  }

  /**
   * Update focused styling for a node tabbing index highlight.
   *
   * @param {Boolean} focused
   *        Indicates if the highlighted node needs to be focused.
   */
  updateFocus(focused) {
    const root = this.getElement("root");
    root.classList.toggle("focused", focused);
  }

  /**
   * Show the highlighter on a given node
   */
  _show() {
    return this._update();
  }

  /**
   * Update the highlighter on the current highlighted node (the one that was
   * passed as an argument to show(node)).
   * Should be called whenever node size or attributes change
   */
  _update() {
    let shown = false;
    setIgnoreLayoutChanges(true);

    if (this._updateTabbingOrder()) {
      this._showInfobar();
      this._showTabbingOrder();
      shown = true;
      setIgnoreLayoutChanges(
        false,
        this.highlighterEnv.window.document.documentElement
      );
    } else {
      // Nothing to highlight (0px rectangle like a <script> tag for instance)
      this._hide();
    }

    return shown;
  }

  /**
   * Hide the highlighter, the outline and the infobar.
   */
  _hide() {
    setIgnoreLayoutChanges(true);

    this._hideTabbingOrder();
    this._hideInfobar();

    setIgnoreLayoutChanges(
      false,
      this.highlighterEnv.window.document.documentElement
    );
  }

  /**
   * Hide the infobar
   */
  _hideInfobar() {
    this.getElement("infobar-container").setAttribute("hidden", "true");
  }

  /**
   * Show the infobar
   */
  _showInfobar() {
    if (!this.currentNode) {
      return;
    }

    this.getElement("infobar-container").removeAttribute("hidden");
    this.getElement("infobar-text").setTextContent(this.options.index);
    const bounds = this._getBounds();
    const container = this.getElement("infobar-container");

    moveInfobar(container, bounds, this.win);
  }

  /**
   * Hide the tabbing order highlighter
   */
  _hideTabbingOrder() {
    this.getElement("container").setAttribute("hidden", "true");
  }

  /**
   * Show the tabbing order highlighter
   */
  _showTabbingOrder() {
    this.getElement("container").removeAttribute("hidden");
  }

  /**
   * Calculate border bounds based on the quads returned by getAdjustedQuads.
   * @return {Object} A bounds object {bottom,height,left,right,top,width,x,y}
   */
  _getBorderBounds() {
    const quads = this.currentQuads.border;
    if (!quads || !quads.length) {
      return null;
    }

    const bounds = {
      bottom: -Infinity,
      height: 0,
      left: Infinity,
      right: -Infinity,
      top: Infinity,
      width: 0,
      x: 0,
      y: 0,
    };

    for (const q of quads) {
      bounds.bottom = Math.max(bounds.bottom, q.bounds.bottom);
      bounds.top = Math.min(bounds.top, q.bounds.top);
      bounds.left = Math.min(bounds.left, q.bounds.left);
      bounds.right = Math.max(bounds.right, q.bounds.right);
    }
    bounds.x = bounds.left;
    bounds.y = bounds.top;
    bounds.width = bounds.right - bounds.left;
    bounds.height = bounds.bottom - bounds.top;

    return bounds;
  }

  /**
   * Update the tabbing order index as per the current node.
   *
   * @return {boolean}
   *         True if the current node has a tabbing order index to be
   *         highlighted
   */
  _updateTabbingOrder() {
    if (!this._nodeNeedsHighlighting()) {
      this._hideTabbingOrder();
      return false;
    }

    const boundsEl = this.getElement("bounds");
    const { left, top, width, height } = this._getBounds();
    boundsEl.setAttribute(
      "style",
      `top: ${top}px; left: ${left}px; width: ${width}px; height: ${height}px;`
    );

    // Un-zoom the root wrapper if the page was zoomed.
    const rootId = this.ID_CLASS_PREFIX + "container";
    this.markup.scaleRootElement(this.currentNode, rootId);

    return true;
  }

  /**
   * Can the current node be highlighted? Does it have quads.
   * @return {Boolean}
   */
  _nodeNeedsHighlighting() {
    return (
      this.currentQuads.margin.length ||
      this.currentQuads.border.length ||
      this.currentQuads.padding.length ||
      this.currentQuads.content.length
    );
  }

  _getBounds() {
    const borderBounds = this._getBorderBounds();
    let bounds = {
      bottom: 0,
      height: 0,
      left: 0,
      right: 0,
      top: 0,
      width: 0,
      x: 0,
      y: 0,
    };

    if (!borderBounds) {
      // Invisible element such as a script tag.
      return bounds;
    }

    const { bottom, height, left, right, top, width, x, y } = borderBounds;
    if (width > 0 || height > 0) {
      bounds = { bottom, height, left, right, top, width, x, y };
    }

    return bounds;
  }
}

/**
 * Move the infobar to the right place in the highlighter. The infobar is used
 * to display element's tabbing order index.
 *
 * @param  {DOMNode} container
 *         The container element which will be used to position the infobar.
 * @param  {Object} bounds
 *         The content bounds of the container element.
 * @param  {Window} win
 *         The window object.
 */
function moveInfobar(container, bounds, win) {
  const zoom = getCurrentZoom(win);
  const { computedStyle } = container;
  const margin = 2;
  const arrowSize =
    parseFloat(
      computedStyle.getPropertyValue("--highlighter-bubble-arrow-size")
    ) - 2;
  const containerHeight = parseFloat(computedStyle.getPropertyValue("height"));
  const containerWidth = parseFloat(computedStyle.getPropertyValue("width"));

  const topBoundary = margin;
  const bottomBoundary =
    win.document.scrollingElement.scrollHeight - containerHeight - margin - 1;
  const leftBoundary = containerWidth / 2 + margin;

  let top = bounds.y - containerHeight - arrowSize;
  let left = bounds.x + bounds.width / 2;
  const bottom = bounds.bottom + arrowSize;
  let positionAttribute = "top";

  const canBePlacedOnTop = top >= topBoundary;
  const canBePlacedOnBottom = bottomBoundary - bottom > 0;

  if (!canBePlacedOnTop && canBePlacedOnBottom) {
    top = bottom;
    positionAttribute = "bottom";
  }

  let hideArrow = false;
  if (top < topBoundary) {
    hideArrow = true;
    top = topBoundary;
  } else if (top > bottomBoundary) {
    hideArrow = true;
    top = bottomBoundary;
  }

  if (left < leftBoundary) {
    hideArrow = true;
    left = leftBoundary;
  }

  if (hideArrow) {
    container.setAttribute("hide-arrow", "true");
  } else {
    container.removeAttribute("hide-arrow");
  }

  container.setAttribute(
    "style",
    `
     position: absolute;
     transform-origin: 0 0;
     transform: scale(${1 / zoom}) translate(calc(${left}px - 50%), ${top}px)`
  );

  container.setAttribute("position", positionAttribute);
}

exports.NodeTabbingOrderHighlighter = NodeTabbingOrderHighlighter;