summaryrefslogtreecommitdiffstats
path: root/layout/inspector/tests/test_replaceBlockRuleBodyTextInStylesheet.html
blob: e6385978ba69298c14df417eb697b088b331c99d (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
<!DOCTYPE HTML>
<html>
<head>
  <meta charset="utf-8">
  <title>Test InspectorUtils::replaceBlockRuleBodyTextInStylesheet</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" href="/tests/SimpleTest/test.css"/>
  <style>
#test-simple {
  color: #f0c;
}
#test-unicode,[data-unicode="๐Ÿฆ„๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ"]::after {
  content: /* test comment */ "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿฆ„";
  outline: 2px solid salmon;
}
#test-empty {} /* ๐Ÿ› ๏ธโš’๏ธ๐Ÿ› ๏ธ */ #test-same-line { font-size: 3em; }
#test-nested-parent {
  color: tomato;
  #test-nested-child {
    background: gold;
  }
}#test-after-closing-bracket{--modified:false}
  </style>
  <script>SimpleTest.waitForExplicitFinish();</script>
  <script defer>
    const InspectorUtils = SpecialPowers.InspectorUtils;
    let stylesheet = document.styleSheets[1];
    let authoredStyleSheetText = document.querySelector("style").textContent;

    const existingRulesAuthoredText = [
`#test-simple {
  color: #f0c;
}`,
`#test-unicode,[data-unicode="๐Ÿฆ„๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ"]::after {
  content: /* test comment */ "๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ๐Ÿฆ„";
  outline: 2px solid salmon;
}`,
`#test-empty {}`,
`#test-same-line { font-size: 3em; }`,
`#test-nested-parent {
  color: tomato;
  #test-nested-child {
    background: gold;
  }
}`,
`#test-nested-child {
    background: gold;
  }`,
`#test-after-closing-bracket{--modified:false}`,
];

    const replaceBlockRuleBodyTextInStylesheet = (rule, newBodyText) => {
      return InspectorUtils.replaceBlockRuleBodyTextInStylesheet(
        authoredStyleSheetText,
        InspectorUtils.getRelativeRuleLine(rule),
        InspectorUtils.getRuleColumn(rule),
        newBodyText
      )};

    info("Check a simple case");
    let newBodyText = `border-color: cyan;`;
    is(
      replaceBlockRuleBodyTextInStylesheet(stylesheet.cssRules[0], newBodyText),
      authoredStyleSheetText.replace(
        existingRulesAuthoredText[0],
        `#test-simple {${newBodyText}}`,
      ),
      "Got the expected result for #test-simple"
    );

    info("Check that the rule body can be emptied");
    is(
      replaceBlockRuleBodyTextInStylesheet(stylesheet.cssRules[0], ""),
      authoredStyleSheetText.replace(
        existingRulesAuthoredText[0],
        `#test-simple {}`,
      ),
      "Successfuly removed rule content for #test-simple"
    );

    info("Check that it can handle unicode characters");
    newBodyText = `content: "o ๐ŸฆŠ o";`;
    is(
      replaceBlockRuleBodyTextInStylesheet(stylesheet.cssRules[1], newBodyText),
      authoredStyleSheetText.replace(existingRulesAuthoredText[1],
        `#test-unicode,[data-unicode="๐Ÿฆ„๐Ÿ‘จโ€๐Ÿ‘ฉโ€๐Ÿ‘งโ€๐Ÿ‘ฆ"]::after {${newBodyText}}`,
      ),
      "Got the expected result for #test-unicode"
    );

    info("Check that it can replace content of an empty rule");
    newBodyText = `font-family: "Zilla;"`;
    is(
      replaceBlockRuleBodyTextInStylesheet(stylesheet.cssRules[2], newBodyText),
      authoredStyleSheetText.replace(
        existingRulesAuthoredText[2],
        `#test-empty {${newBodyText}}`,
      ),
      "Got the expected result for #test-empty"
    );

    info("Check that it can handle a rule on a same line as another rule");
    newBodyText = `color: pink;`;
    is(
      replaceBlockRuleBodyTextInStylesheet(stylesheet.cssRules[3], newBodyText),
      authoredStyleSheetText.replace(
        existingRulesAuthoredText[3],
        `#test-same-line {${newBodyText}}`,
      ),
      "Got the expected result for #test-same-line"
    );

    info("Check that it can handle a rule with a child rule");
    newBodyText = `background: silver;
    & > span {
      color: white;
    }`;
    is(
      replaceBlockRuleBodyTextInStylesheet(stylesheet.cssRules[4], newBodyText),
      authoredStyleSheetText.replace(
        existingRulesAuthoredText[4],
        `#test-nested-parent {${newBodyText}}`,
      ),
      "Got the expected result for #test-nested-parent"
    );

    info("Check that it can handle a nested rule");
    newBodyText = `color: white;height: 100%;`;
    is(
      replaceBlockRuleBodyTextInStylesheet(stylesheet.cssRules[4].cssRules[0], newBodyText),
      authoredStyleSheetText.replace(
        existingRulesAuthoredText[5],
        `#test-nested-child {${newBodyText}}`,
      ),
      "Got the expected result for #test-nested-child"
    );

    // Covering fix for Bug 1890775
    info("Check that it can handle rules  whose declaration is directly after the } of the previous rule, without spaces");
    isnot(
      InspectorUtils.getRelativeRuleLine(stylesheet.cssRules[5]),
      1,
      "The rule should not be on the first line of the stylesheet to check the issue it covers"
    );
    newBodyText = `--modified:true`;
    is(
      replaceBlockRuleBodyTextInStylesheet(stylesheet.cssRules[5], newBodyText),
      authoredStyleSheetText.replace(
        existingRulesAuthoredText[6],
        `#test-after-closing-bracket{${newBodyText}}`,
      ),
      "Got the expected result for #test-after-closing-bracket"
    );

    info("Checking fix for files with crlf EOL sequence");
    let styleEl = document.createElement("style");
    let ruleText = `#test-after-closing-bracket-crlf{--modified-crlf:false}`
    authoredStyleSheetText = `\r\nhtml{}${ruleText}`;
    styleEl.append(document.createTextNode(authoredStyleSheetText));
    document.head.append(styleEl);
    stylesheet = document.styleSheets[2];
    isnot(
      InspectorUtils.getRelativeRuleLine(stylesheet.cssRules[1]),
      1,
      "The rule should not be on the first line of the stylesheet to check the issue it covers"
    );
    newBodyText = `--modified-crlf:true`;
    is(
      replaceBlockRuleBodyTextInStylesheet(stylesheet.cssRules[1], newBodyText),
      authoredStyleSheetText.replace(
        ruleText,
        `#test-after-closing-bracket-crlf{${newBodyText}}`,
      ),
      "Got the expected result for #test-after-closing-bracket-crlf"
    );

    info("Checking fix for files with cr EOL sequence");
    ruleText = `#test-after-closing-bracket-cr{--modified-cr:false}`
    authoredStyleSheetText = `\rhtml{}${ruleText}`;
    styleEl.innerText = "";
    styleEl.append(document.createTextNode(authoredStyleSheetText));
    isnot(
      InspectorUtils.getRelativeRuleLine(stylesheet.cssRules[1]),
      1,
      "The rule should not be on the first line of the stylesheet to check the issue it covers"
    );
    newBodyText = `--modified-cr:true`;
    is(
      replaceBlockRuleBodyTextInStylesheet(stylesheet.cssRules[1], newBodyText),
      authoredStyleSheetText.replace(
        ruleText,
        `#test-after-closing-bracket-cr{${newBodyText}}`,
      ),
      "Got the expected result for #test-after-closing-bracket-cr"
    );

    SimpleTest.finish();
  </script>
</head>
<body>
<h1>Test InspectorUtils::replaceBlockRuleBodyTextInStylesheet</h1>
<p id="display"></p>
<div id="content" style="display: none">

</div>
<pre id="test">
</pre>
</body>
</html>