summaryrefslogtreecommitdiffstats
path: root/devtools/client/inspector/rules/test/browser_rules_selector_highlight.js
blob: 91422d57a7e8fe234048254c5c6b447ece143bf0 (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
/* vim: set ft=javascript ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
 http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

// Tests that the rule view selector text is highlighted correctly according
// to the components of the selector.

const TEST_URI = [
  "<style type='text/css'>",
  "  h1 {}",
  "  h1#testid {}",
  "  h1 + p {}",
  "  div[hidden=\"true\"] {}",
  "  div[title=\"test\"][checked=true] {}",
  "  p:empty {}",
  "  p:lang(en) {}",
  "  .testclass:active {}",
  "  .testclass:focus {}",
  "  .testclass:hover {}",
  "</style>",
  "<h1>Styled Node</h1>",
  "<p>Paragraph</p>",
  "<h1 id=\"testid\">Styled Node</h1>",
  "<div hidden=\"true\"></div>",
  "<div title=\"test\" checked=\"true\"></div>",
  "<p></p>",
  "<p lang=\"en\">Paragraph<p>",
  "<div class=\"testclass\">Styled Node</div>"
].join("\n");

const SELECTOR_ATTRIBUTE = "ruleview-selector-attribute";
const SELECTOR_ELEMENT = "ruleview-selector";
const SELECTOR_PSEUDO_CLASS = "ruleview-selector-pseudo-class";
const SELECTOR_PSEUDO_CLASS_LOCK = "ruleview-selector-pseudo-class-lock";

const TEST_DATA = [
  {
    node: "h1",
    expected: [
      { value: "h1", class: SELECTOR_ELEMENT }
    ]
  },
  {
    node: "h1 + p",
    expected: [
      { value: "h1 + p", class: SELECTOR_ELEMENT }
    ]
  },
  {
    node: "h1#testid",
    expected: [
      { value: "h1#testid", class: SELECTOR_ELEMENT }
    ]
  },
  {
    node: "div[hidden='true']",
    expected: [
      { value: "div", class: SELECTOR_ELEMENT },
      { value: "[hidden=\"true\"]", class: SELECTOR_ATTRIBUTE }
    ]
  },
  {
    node: "div[title=\"test\"][checked=\"true\"]",
    expected: [
      { value: "div", class: SELECTOR_ELEMENT },
      { value: "[title=\"test\"]", class: SELECTOR_ATTRIBUTE },
      { value: "[checked=\"true\"]", class: SELECTOR_ATTRIBUTE }
    ]
  },
  {
    node: "p:empty",
    expected: [
      { value: "p", class: SELECTOR_ELEMENT },
      { value: ":empty", class: SELECTOR_PSEUDO_CLASS }
    ]
  },
  {
    node: "p:lang(en)",
    expected: [
      { value: "p", class: SELECTOR_ELEMENT },
      { value: ":lang(en)", class: SELECTOR_PSEUDO_CLASS }
    ]
  },
  {
    node: ".testclass",
    pseudoClass: ":active",
    expected: [
      { value: ".testclass", class: SELECTOR_ELEMENT },
      { value: ":active", class: SELECTOR_PSEUDO_CLASS_LOCK }
    ]
  },
  {
    node: ".testclass",
    pseudoClass: ":focus",
    expected: [
      { value: ".testclass", class: SELECTOR_ELEMENT },
      { value: ":focus", class: SELECTOR_PSEUDO_CLASS_LOCK }
    ]
  },
  {
    node: ".testclass",
    pseudoClass: ":hover",
    expected: [
      { value: ".testclass", class: SELECTOR_ELEMENT },
      { value: ":hover", class: SELECTOR_PSEUDO_CLASS_LOCK }
    ]
  },
];

add_task(function* () {
  yield addTab("data:text/html;charset=utf-8," + encodeURIComponent(TEST_URI));
  let {inspector, view} = yield openRuleView();

  for (let {node, pseudoClass, expected} of TEST_DATA) {
    yield selectNode(node, inspector);

    if (pseudoClass) {
      let onRefresh = inspector.once("rule-view-refreshed");
      inspector.togglePseudoClass(pseudoClass);
      yield onRefresh;
    }

    let selectorContainer =
      getRuleViewRuleEditor(view, 1).selectorText.firstChild;

    if (selectorContainer.children.length === expected.length) {
      for (let i = 0; i < expected.length; i++) {
        is(expected[i].value, selectorContainer.children[i].textContent,
          "Got expected selector value: " + expected[i].value + " == " +
          selectorContainer.children[i].textContent);
        is(expected[i].class, selectorContainer.children[i].className,
          "Got expected class name: " + expected[i].class + " == " +
          selectorContainer.children[i].className);
      }
    } else {
      for (let selector of selectorContainer.children) {
        info("Actual selector components: { value: " + selector.textContent +
          ", class: " + selector.className + " }\n");
      }
    }
  }
});