summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/rules/test/browser_rules_selector_warnings.js
blob: 1e5c7da3020878cc8e4d460885a6746d5a47a0d0 (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
/* Any copyright is dedicated to the Public Domain.
 http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Test that selector warnings are displayed in the rule-view.
const TEST_URI = `
  <!DOCTYPE html>
  <style>
    main, :has(form) {
      /* /!\ space between & and : is important */
      & :has(input),
      & :has(select),
      &:has(button) {
        background: gold;
      }
    }
  </style>
  <body>
    <main>
      <form>
        <input>
      </form>
    </main>
  </body>`;

const UNCONSTRAINED_HAS_WARNING_MESSAGE =
  "This selector uses unconstrained :has(), which can be slow";

add_task(async function () {
  await addTab(
    "https://example.com/document-builder.sjs?html=" +
      encodeURIComponent(TEST_URI)
  );
  const { inspector, view } = await openRuleView();

  await selectNode("main", inspector);
  const { ancestorDataEl, selectorText } = getRuleViewRuleEditor(view, 1);

  info(
    "Check that unconstrained :has() warnings are displayed for the rules selectors"
  );
  const ruleSelectors = Array.from(
    selectorText.querySelectorAll(".ruleview-selector")
  );

  await assertSelectorWarnings({
    view,
    selectorEl: ruleSelectors[0],
    selectorText: "& :has(input)",
    expectedWarnings: [UNCONSTRAINED_HAS_WARNING_MESSAGE],
  });
  await assertSelectorWarnings({
    view,
    selectorEl: ruleSelectors[1],
    selectorText: "& :has(select)",
    expectedWarnings: [UNCONSTRAINED_HAS_WARNING_MESSAGE],
  });
  // Warning is not displayed when the selector does not have warnings
  await assertSelectorWarnings({
    view,
    selectorEl: ruleSelectors[2],
    selectorText: "&:has(button)",
    expectedWarnings: [],
  });

  info(
    "Check that unconstrained :has() warnings are displayed for the parent rules selectors"
  );
  const parentRuleSelectors = Array.from(
    ancestorDataEl.querySelectorAll(".ruleview-selector")
  );
  await assertSelectorWarnings({
    view,
    selectorEl: parentRuleSelectors[0],
    selectorText: "main",
    expectedWarnings: [],
  });
  await assertSelectorWarnings({
    view,
    selectorEl: parentRuleSelectors[1],
    selectorText: ":has(form)",
    expectedWarnings: [UNCONSTRAINED_HAS_WARNING_MESSAGE],
  });
});

async function assertSelectorWarnings({
  view,
  selectorEl,
  selectorText,
  expectedWarnings,
}) {
  is(
    selectorEl.textContent,
    selectorText,
    "Passed selector element is the expected one"
  );

  const selectorWarningsContainerEl = selectorEl.querySelector(
    ".ruleview-selector-warnings"
  );

  if (expectedWarnings.length === 0) {
    Assert.strictEqual(
      selectorWarningsContainerEl,
      null,
      `"${selectorText}" does not have warnings`
    );
    return;
  }

  Assert.notStrictEqual(
    selectorWarningsContainerEl,
    null,
    `"${selectorText}" does have warnings`
  );

  is(
    selectorWarningsContainerEl
      .getAttribute("data-selector-warning-kind")
      ?.split(",")?.length || 0,
    expectedWarnings.length,
    `"${selectorText}" has expected number of warnings`
  );

  // Ensure that the element can be targetted from EventUtils.
  selectorWarningsContainerEl.scrollIntoView();

  const tooltip = view.tooltips.getTooltip("interactiveTooltip");
  const onTooltipReady = tooltip.once("shown");
  EventUtils.synthesizeMouseAtCenter(
    selectorWarningsContainerEl,
    { type: "mousemove" },
    selectorWarningsContainerEl.ownerDocument.defaultView
  );
  await onTooltipReady;

  const lis = Array.from(tooltip.panel.querySelectorAll("li")).map(
    li => li.textContent
  );
  Assert.deepEqual(lis, expectedWarnings, "Tooltip has expected items");

  info("Hide the tooltip");
  const onHidden = tooltip.once("hidden");
  // Move the mouse elsewhere to hide the tooltip
  EventUtils.synthesizeMouse(
    selectorWarningsContainerEl.ownerDocument.body,
    1,
    1,
    { type: "mousemove" },
    selectorWarningsContainerEl.ownerDocument.defaultView
  );
  await onHidden;
}