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

"use strict";

// Test that pseudoelements are displayed correctly in the rule view

const TEST_URI = URL_ROOT + "doc_pseudoelement.html";
const PSEUDO_PREF = "devtools.inspector.show_pseudo_elements";

add_task(function* () {
  yield pushPref(PSEUDO_PREF, true);

  yield addTab(TEST_URI);
  let {inspector, view} = yield openRuleView();

  yield testTopLeft(inspector, view);
  yield testTopRight(inspector, view);
  yield testBottomRight(inspector, view);
  yield testBottomLeft(inspector, view);
  yield testParagraph(inspector, view);
  yield testBody(inspector, view);
});

function* testTopLeft(inspector, view) {
  let id = "#topleft";
  let rules = yield assertPseudoElementRulesNumbers(id,
    inspector, view, {
      elementRulesNb: 4,
      firstLineRulesNb: 2,
      firstLetterRulesNb: 1,
      selectionRulesNb: 0,
      afterRulesNb: 1,
      beforeRulesNb: 2
    }
  );

  let gutters = assertGutters(view);

  info("Make sure that clicking on the twisty hides pseudo elements");
  let expander = gutters[0].querySelector(".ruleview-expander");
  ok(!view.element.children[1].hidden, "Pseudo Elements are expanded");

  expander.click();
  ok(view.element.children[1].hidden,
    "Pseudo Elements are collapsed by twisty");

  expander.click();
  ok(!view.element.children[1].hidden, "Pseudo Elements are expanded again");

  info("Make sure that dblclicking on the header container also toggles " +
       "the pseudo elements");
  EventUtils.synthesizeMouseAtCenter(gutters[0], {clickCount: 2},
                                     view.styleWindow);
  ok(view.element.children[1].hidden,
    "Pseudo Elements are collapsed by dblclicking");

  let elementRuleView = getRuleViewRuleEditor(view, 3);

  let elementFirstLineRule = rules.firstLineRules[0];
  let elementFirstLineRuleView =
    [...view.element.children[1].children].filter(e => {
      return e._ruleEditor && e._ruleEditor.rule === elementFirstLineRule;
    })[0]._ruleEditor;

  is(convertTextPropsToString(elementFirstLineRule.textProps),
     "color: orange",
     "TopLeft firstLine properties are correct");

  let onAdded = view.once("ruleview-changed");
  let firstProp = elementFirstLineRuleView.addProperty("background-color",
    "rgb(0, 255, 0)", "", true);
  yield onAdded;

  onAdded = view.once("ruleview-changed");
  let secondProp = elementFirstLineRuleView.addProperty("font-style",
    "italic", "", true);
  yield onAdded;

  is(firstProp,
     elementFirstLineRule.textProps[elementFirstLineRule.textProps.length - 2],
     "First added property is on back of array");
  is(secondProp,
     elementFirstLineRule.textProps[elementFirstLineRule.textProps.length - 1],
     "Second added property is on back of array");

  is((yield getComputedStyleProperty(id, ":first-line", "background-color")),
     "rgb(0, 255, 0)", "Added property should have been used.");
  is((yield getComputedStyleProperty(id, ":first-line", "font-style")),
     "italic", "Added property should have been used.");
  is((yield getComputedStyleProperty(id, null, "text-decoration")),
     "none", "Added property should not apply to element");

  yield togglePropStatus(view, firstProp);

  is((yield getComputedStyleProperty(id, ":first-line", "background-color")),
     "rgb(255, 0, 0)", "Disabled property should now have been used.");
  is((yield getComputedStyleProperty(id, null, "background-color")),
     "rgb(221, 221, 221)", "Added property should not apply to element");

  yield togglePropStatus(view, firstProp);

  is((yield getComputedStyleProperty(id, ":first-line", "background-color")),
     "rgb(0, 255, 0)", "Added property should have been used.");
  is((yield getComputedStyleProperty(id, null, "text-decoration")),
     "none", "Added property should not apply to element");

  onAdded = view.once("ruleview-changed");
  firstProp = elementRuleView.addProperty("background-color",
                                          "rgb(0, 0, 255)", "", true);
  yield onAdded;

  is((yield getComputedStyleProperty(id, null, "background-color")),
     "rgb(0, 0, 255)", "Added property should have been used.");
  is((yield getComputedStyleProperty(id, ":first-line", "background-color")),
     "rgb(0, 255, 0)", "Added prop does not apply to pseudo");
}

function* testTopRight(inspector, view) {
  yield assertPseudoElementRulesNumbers("#topright", inspector, view, {
    elementRulesNb: 4,
    firstLineRulesNb: 1,
    firstLetterRulesNb: 1,
    selectionRulesNb: 0,
    beforeRulesNb: 2,
    afterRulesNb: 1
  });

  let gutters = assertGutters(view);

  let expander = gutters[0].querySelector(".ruleview-expander");
  ok(!view.element.firstChild.classList.contains("show-expandable-container"),
     "Pseudo Elements remain collapsed after switching element");

  expander.scrollIntoView();
  expander.click();
  ok(!view.element.children[1].hidden,
    "Pseudo Elements are shown again after clicking twisty");
}

function* testBottomRight(inspector, view) {
  yield assertPseudoElementRulesNumbers("#bottomright", inspector, view, {
    elementRulesNb: 4,
    firstLineRulesNb: 1,
    firstLetterRulesNb: 1,
    selectionRulesNb: 0,
    beforeRulesNb: 3,
    afterRulesNb: 1
  });
}

function* testBottomLeft(inspector, view) {
  yield assertPseudoElementRulesNumbers("#bottomleft", inspector, view, {
    elementRulesNb: 4,
    firstLineRulesNb: 1,
    firstLetterRulesNb: 1,
    selectionRulesNb: 0,
    beforeRulesNb: 2,
    afterRulesNb: 1
  });
}

function* testParagraph(inspector, view) {
  let rules =
    yield assertPseudoElementRulesNumbers("#bottomleft p", inspector, view, {
      elementRulesNb: 3,
      firstLineRulesNb: 1,
      firstLetterRulesNb: 1,
      selectionRulesNb: 1,
      beforeRulesNb: 0,
      afterRulesNb: 0
    });

  assertGutters(view);

  let elementFirstLineRule = rules.firstLineRules[0];
  is(convertTextPropsToString(elementFirstLineRule.textProps),
     "background: blue",
     "Paragraph first-line properties are correct");

  let elementFirstLetterRule = rules.firstLetterRules[0];
  is(convertTextPropsToString(elementFirstLetterRule.textProps),
     "color: red; font-size: 130%",
     "Paragraph first-letter properties are correct");

  let elementSelectionRule = rules.selectionRules[0];
  is(convertTextPropsToString(elementSelectionRule.textProps),
     "color: white; background: black",
     "Paragraph first-letter properties are correct");
}

function* testBody(inspector, view) {
  yield testNode("body", inspector, view);

  let gutters = getGutters(view);
  is(gutters.length, 0, "There are no gutter headings");
}

function convertTextPropsToString(textProps) {
  return textProps.map(t => t.name + ": " + t.value).join("; ");
}

function* testNode(selector, inspector, view) {
  yield selectNode(selector, inspector);
  let elementStyle = view._elementStyle;
  return elementStyle;
}

function* assertPseudoElementRulesNumbers(selector, inspector, view, ruleNbs) {
  let elementStyle = yield testNode(selector, inspector, view);

  let rules = {
    elementRules: elementStyle.rules.filter(rule => !rule.pseudoElement),
    firstLineRules: elementStyle.rules.filter(rule =>
      rule.pseudoElement === ":first-line"),
    firstLetterRules: elementStyle.rules.filter(rule =>
      rule.pseudoElement === ":first-letter"),
    selectionRules: elementStyle.rules.filter(rule =>
      rule.pseudoElement === ":-moz-selection"),
    beforeRules: elementStyle.rules.filter(rule =>
      rule.pseudoElement === ":before"),
    afterRules: elementStyle.rules.filter(rule =>
      rule.pseudoElement === ":after"),
  };

  is(rules.elementRules.length, ruleNbs.elementRulesNb,
     selector + " has the correct number of non pseudo element rules");
  is(rules.firstLineRules.length, ruleNbs.firstLineRulesNb,
     selector + " has the correct number of :first-line rules");
  is(rules.firstLetterRules.length, ruleNbs.firstLetterRulesNb,
     selector + " has the correct number of :first-letter rules");
  is(rules.selectionRules.length, ruleNbs.selectionRulesNb,
     selector + " has the correct number of :selection rules");
  is(rules.beforeRules.length, ruleNbs.beforeRulesNb,
     selector + " has the correct number of :before rules");
  is(rules.afterRules.length, ruleNbs.afterRulesNb,
     selector + " has the correct number of :after rules");

  return rules;
}

function getGutters(view) {
  return view.element.querySelectorAll(".theme-gutter");
}

function assertGutters(view) {
  let gutters = getGutters(view);

  is(gutters.length, 3,
     "There are 3 gutter headings");
  is(gutters[0].textContent, "Pseudo-elements",
     "Gutter heading is correct");
  is(gutters[1].textContent, "This Element",
     "Gutter heading is correct");
  is(gutters[2].textContent, "Inherited from body",
     "Gutter heading is correct");

  return gutters;
}