summaryrefslogtreecommitdiffstats
path: root/devtools/client/shared/widgets/tooltip/css-query-container-tooltip-helper.js
blob: 633e57b9cfb5b0b83c515992861c7be77460fea5 (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
/* 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 XHTML_NS = "http://www.w3.org/1999/xhtml";

class CssQueryContainerTooltipHelper {
  /**
   * Fill the tooltip with container information.
   */
  async setContent(data, tooltip) {
    const res = await data.rule.domRule.getQueryContainerForNode(
      data.ancestorIndex,
      data.rule.inherited ||
        data.rule.elementStyle.ruleView.inspector.selection.nodeFront
    );

    const fragment = this.#getTemplate(res, tooltip);
    tooltip.panel.innerHTML = "";
    tooltip.panel.appendChild(fragment);

    // Size the content.
    tooltip.setContentSize({ width: 267, height: Infinity });
  }

  /**
   * Get the template of the tooltip.
   *
   * @param {Object} data
   * @param {NodeFront} data.node
   * @param {string} data.containerType
   * @param {string} data.inlineSize
   * @param {string} data.blockSize
   * @param {HTMLTooltip} tooltip
   *        The tooltip we are targetting.
   */
  #getTemplate(data, tooltip) {
    const { doc } = tooltip;

    const templateNode = doc.createElementNS(XHTML_NS, "template");

    const tooltipContainer = doc.createElementNS(XHTML_NS, "div");
    tooltipContainer.classList.add("devtools-tooltip-query-container");
    templateNode.content.appendChild(tooltipContainer);

    const nodeContainer = doc.createElementNS(XHTML_NS, "header");
    tooltipContainer.append(nodeContainer);

    const containerQueryLabel = doc.createElementNS(XHTML_NS, "span");
    containerQueryLabel.classList.add("property-name");
    containerQueryLabel.appendChild(doc.createTextNode(`query container`));

    const nodeEl = doc.createElementNS(XHTML_NS, "span");
    nodeEl.classList.add("objectBox-node");
    nodeContainer.append(doc.createTextNode("<"), nodeEl);

    const nodeNameEl = doc.createElementNS(XHTML_NS, "span");
    nodeNameEl.classList.add("tag-name");
    nodeNameEl.appendChild(
      doc.createTextNode(data.node.nodeName.toLowerCase())
    );

    nodeEl.appendChild(nodeNameEl);

    if (data.node.id) {
      const idEl = doc.createElementNS(XHTML_NS, "span");
      idEl.classList.add("attribute-name");
      idEl.appendChild(doc.createTextNode(`#${data.node.id}`));
      nodeEl.appendChild(idEl);
    }

    for (const attr of data.node.attributes) {
      if (attr.name !== "class") {
        continue;
      }
      for (const cls of attr.value.split(/\s/)) {
        const el = doc.createElementNS(XHTML_NS, "span");
        el.classList.add("attribute-name");
        el.appendChild(doc.createTextNode(`.${cls}`));
        nodeEl.appendChild(el);
      }
    }
    nodeContainer.append(doc.createTextNode(">"));

    const ul = doc.createElementNS(XHTML_NS, "ul");
    tooltipContainer.appendChild(ul);

    const containerTypeEl = doc.createElementNS(XHTML_NS, "li");
    const containerTypeLabel = doc.createElementNS(XHTML_NS, "span");
    containerTypeLabel.classList.add("property-name");
    containerTypeLabel.appendChild(doc.createTextNode(`container-type`));

    const containerTypeValue = doc.createElementNS(XHTML_NS, "span");
    containerTypeValue.classList.add("property-value");
    containerTypeValue.appendChild(doc.createTextNode(data.containerType));

    containerTypeEl.append(
      containerTypeLabel,
      doc.createTextNode(": "),
      containerTypeValue
    );
    ul.appendChild(containerTypeEl);

    const inlineSizeEl = doc.createElementNS(XHTML_NS, "li");

    const inlineSizeLabel = doc.createElementNS(XHTML_NS, "span");
    inlineSizeLabel.classList.add("property-name");
    inlineSizeLabel.appendChild(doc.createTextNode(`inline-size`));

    const inlineSizeValue = doc.createElementNS(XHTML_NS, "span");
    inlineSizeValue.classList.add("property-value");
    inlineSizeValue.appendChild(doc.createTextNode(data.inlineSize));

    inlineSizeEl.append(
      inlineSizeLabel,
      doc.createTextNode(": "),
      inlineSizeValue
    );
    ul.appendChild(inlineSizeEl);

    if (data.containerType != "inline-size") {
      const blockSizeEl = doc.createElementNS(XHTML_NS, "li");
      const blockSizeLabel = doc.createElementNS(XHTML_NS, "span");
      blockSizeLabel.classList.add("property-name");
      blockSizeLabel.appendChild(doc.createTextNode(`block-size`));

      const blockSizeValue = doc.createElementNS(XHTML_NS, "span");
      blockSizeValue.classList.add("property-value");
      blockSizeValue.appendChild(doc.createTextNode(data.blockSize));

      blockSizeEl.append(
        blockSizeLabel,
        doc.createTextNode(": "),
        blockSizeValue
      );
      ul.appendChild(blockSizeEl);
    }

    return doc.importNode(templateNode.content, true);
  }
}

module.exports = CssQueryContainerTooltipHelper;