summaryrefslogtreecommitdiffstats
path: root/layout/style/test/test_additional_sheets.html
blob: 8cd8ffd93a77e05dafe2fc00ac741c82ae6c0741 (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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for additional sheets</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body onload="run()">
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=737003">Mozilla Bug 737003</a>
<iframe id="iframe" src="additional_sheets_helper.html"></iframe>
<pre id="test">
<script type="application/javascript">

var gIOService = SpecialPowers.Cc["@mozilla.org/network/io-service;1"]
  .getService(SpecialPowers.Ci.nsIIOService)

var gSSService = SpecialPowers.Cc["@mozilla.org/content/style-sheet-service;1"]
  .getService(SpecialPowers.Ci.nsIStyleSheetService);

function getUri(style)
{
  return "data:text/css," + style;
}

function getStyle(color, swapped)
{
  return "body {color: " + color +  (swapped ? " !important" : "") +
    "; background-color: " + color + (swapped ? "" : " !important;") + ";}";
}

function loadUserSheet(win, style)
{
  loadSheet(win, style, "USER_SHEET");
}

function loadAgentSheet(win, style)
{
  loadSheet(win, style, "AGENT_SHEET");
}

function loadAuthorSheet(win, style)
{
  loadSheet(win, style, "AUTHOR_SHEET");
}

function removeUserSheet(win, style)
{
  removeSheet(win, style, "USER_SHEET");
}

function removeAgentSheet(win, style)
{
  removeSheet(win, style, "AGENT_SHEET");
}

function removeAuthorSheet(win, style)
{
  removeSheet(win, style, "AUTHOR_SHEET");
}

function loadSheet(win, style, type)
{
  var uri = gIOService.newURI(getUri(style));
  var windowUtils = SpecialPowers.getDOMWindowUtils(win);
  windowUtils.loadSheet(uri, windowUtils[type]);
}

function removeSheet(win, style, type)
{
  var uri = gIOService.newURI(getUri(style));
  var windowUtils = SpecialPowers.getDOMWindowUtils(win);
  windowUtils.removeSheet(uri, windowUtils[type]);
}

function loadAndRegisterUserSheet(win, style)
{
  loadAndRegisterSheet(win, style, "USER_SHEET");
}

function loadAndRegisterAgentSheet(win, style)
{
  loadAndRegisterSheet(win, style, "AGENT_SHEET");
}

function loadAndRegisterAuthorSheet(win, style)
{
  loadAndRegisterSheet(win, style, "AUTHOR_SHEET");
}

function unregisterUserSheet(win, style)
{
  unregisterSheet(win, style, "USER_SHEET");
}

function unregisterAgentSheet(win, style)
{
  unregisterSheet(win, style, "AGENT_SHEET");
}

function unregisterAuthorSheet(win, style)
{
  unregisterSheet(win, style, "AUTHOR_SHEET");
}

function loadAndRegisterSheet(win, style, type)
{
  uri = gIOService.newURI(getUri(style));
  gSSService.loadAndRegisterSheet(uri, gSSService[type]);
  is(gSSService.sheetRegistered(uri, gSSService[type]), true);
}

function unregisterSheet(win, style, type)
{
  var uri = gIOService.newURI(getUri(style));
  gSSService.unregisterSheet(uri, gSSService[type]);
  is(gSSService.sheetRegistered(uri, gSSService[type]), false);
}

function setDocSheet(win, style)
{
  var subdoc = win.document;
  var headID = subdoc.getElementsByTagName("head")[0];
  var cssNode = subdoc.createElement('style');
  cssNode.type = 'text/css';
  cssNode.innerHTML = style;
  cssNode.id = 'docsheet';
  headID.appendChild(cssNode);
}

function removeDocSheet(win)
{
  var subdoc = win.document;
  var node = subdoc.getElementById('docsheet');
  node.remove();
}

var agent = {
  type: 'agent',
  color: 'rgb(255, 0, 0)',
  addRules: loadAndRegisterAgentSheet,
  removeRules: unregisterAgentSheet
};

var user = {
  type: 'user',
  color: 'rgb(0, 255, 0)',
  addRules: loadAndRegisterUserSheet,
  removeRules: unregisterUserSheet
};

var additionalAgent = {
  type: 'additionalAgent',
  color: 'rgb(0, 0, 255)',
  addRules: loadAgentSheet,
  removeRules: removeAgentSheet
};

var additionalUser = {
  type: 'additionalUser',
  color: 'rgb(255, 255, 0)',
  addRules: loadUserSheet,
  removeRules: removeUserSheet
};

var additionalAuthor = {
  type: 'additionalAuthor',
  color: 'rgb(255, 255, 0)',
  addRules: loadAuthorSheet,
  removeRules: removeAuthorSheet
};

var doc = {
  type: 'doc',
  color: 'rgb(0, 255, 255)',
  addRules: setDocSheet,
  removeRules: removeDocSheet
};

var author = {
  type: 'author',
  color: 'rgb(255, 0, 255)',
  addRules: loadAndRegisterAuthorSheet,
  removeRules: unregisterAuthorSheet
};

function loadAndCheck(win, firstType, secondType, swap, result1, result2)
{
  var firstStyle = getStyle(firstType.color, false);
  var secondStyle = getStyle(secondType.color, swap);

  firstType.addRules(win, firstStyle);
  secondType.addRules(win, secondStyle);

  var cs = win.getComputedStyle(win.document.body);
  is(cs.getPropertyValue('color'), result1,
    firstType.type + "(normal)" + " vs " + secondType.type + (swap ? "(important)" : "(normal)" ) + " 1");
  is(cs.getPropertyValue('background-color'), result2,
    firstType.type + "(important)" + " vs " + secondType.type + (swap ? "(normal)" : "(important)" ) + " 2");

  firstType.removeRules(win, firstStyle);
  secondType.removeRules(win, secondStyle);

  is(cs.getPropertyValue('color'), 'rgb(0, 0, 0)', firstType.type + " vs " + secondType.type + " 3");
  is(cs.getPropertyValue('background-color'), 'rgba(0, 0, 0, 0)', firstType.type + " vs " + secondType.type + " 4");
}

// There are 8 cases. Regular against regular, regular against important, important
// against regular, important against important. We can load style from typeA first
// then typeB or the other way around so that's 4*2=8 cases.

function testStyleVsStyle(win, typeA, typeB, results)
{
  function color(res)
  {
    return res ? typeB.color : typeA.color;
  }

  loadAndCheck(win, typeA, typeB, false, color(results.AB.rr), color(results.AB.ii));
  loadAndCheck(win, typeB, typeA, false, color(results.BA.rr), color(results.BA.ii));

  loadAndCheck(win, typeA, typeB, true, color(results.AB.ri), color(results.AB.ir));
  loadAndCheck(win, typeB, typeA, true, color(results.BA.ir), color(results.BA.ri));
}

// 5 user agent normal declarations
// 4 user normal declarations
// 3 author normal declarations
// 2 author important declarations
// 1 user important declarations
// 0 user agent important declarations

function run()
{
  var iframe = document.getElementById("iframe");
  var win = iframe.contentWindow;

// Some explanation how to interpret this result table...
// in case of loading the agent style first and the user style later (AB)
// if there is an important rule in both for let's say color (ii)
// the rule specified in the agent style will lead (AB.ii == 0)
// If both rules would be just regular rules the one specified in the user style
// would lead. (AB.rr == 1). If we would load/add the rules in reverse order that
// would not change that (BA.rr == 1)
  testStyleVsStyle(win, agent, user,
                   {AB:{rr:1, ii:0, ri:1, ir:0}, BA:{rr:1, ii:0, ri:1, ir:0}});

  testStyleVsStyle(win, agent, doc,
                   {AB:{rr:1, ii:0, ri:1, ir:0}, BA:{rr:1, ii:0, ri:1, ir:0}});


  testStyleVsStyle(win, additionalUser, agent,
                   {AB:{rr:0, ii:1, ri:1, ir:0}, BA:{rr:0, ii:1, ri:1, ir:0}});

  testStyleVsStyle(win, additionalUser, doc,
                   {AB:{rr:1, ii:0, ri:1, ir:0}, BA:{rr:1, ii:0, ri:1, ir:0}});

  testStyleVsStyle(win, additionalAgent, user,
                   {AB:{rr:1, ii:0, ri:1, ir:0}, BA:{rr:1, ii:0, ri:1, ir:0}});

  testStyleVsStyle(win, additionalAgent, doc,
                   {AB:{rr:1, ii:0, ri:1, ir:0}, BA:{rr:1, ii:0, ri:1, ir:0}});


  testStyleVsStyle(win, additionalAgent, additionalUser,
                   {AB:{rr:1, ii:0, ri:1, ir:0}, BA:{rr:1, ii:0, ri:1, ir:0}});

  testStyleVsStyle(win, author, doc,
                   {AB:{rr:0, ii:0, ri:1, ir:0}, BA:{rr:0, ii:0, ri:1, ir:0}});

  testStyleVsStyle(win, author, user,
                   {AB:{rr:0, ii:1, ri:1, ir:0}, BA:{rr:0, ii:1, ri:1, ir:0}});

  testStyleVsStyle(win, author, agent,
                   {AB:{rr:0, ii:1, ri:1, ir:0}, BA:{rr:0, ii:1, ri:1, ir:0}});

  testStyleVsStyle(win, author, additionalUser,
                   {AB:{rr:0, ii:1, ri:1, ir:0}, BA:{rr:0, ii:1, ri:1, ir:0}});

  testStyleVsStyle(win, additionalAuthor, doc,
                   {AB:{rr:0, ii:0, ri:1, ir:0}, BA:{rr:0, ii:0, ri:1, ir:0}});

  testStyleVsStyle(win, additionalAuthor, author,
                   {AB:{rr:0, ii:0, ri:1, ir:0}, BA:{rr:0, ii:0, ri:1, ir:0}});

  testStyleVsStyle(win, additionalAuthor, user,
                     {AB:{rr:0, ii:1, ri:1, ir:0}, BA:{rr:0, ii:1, ri:1, ir:0}});

  testStyleVsStyle(win, additionalAuthor, agent,
                     {AB:{rr:0, ii:1, ri:1, ir:0}, BA:{rr:0, ii:1, ri:1, ir:0}});

  testStyleVsStyle(win, additionalAuthor, additionalUser,
                   {AB:{rr:0, ii:1, ri:1, ir:0}, BA:{rr:0, ii:1, ri:1, ir:0}});

  // Bug 1228542
  var url = getStyle('rgb(255, 0, 0)');
  loadAndRegisterAuthorSheet(win, url);
  // Avoiding security exception...
  (new win.Function("document.open()"))();
  (new win.Function("document.close()"))();
  unregisterAuthorSheet(win, url);

  SimpleTest.finish();
}

SimpleTest.waitForExplicitFinish();

</script>
</pre>
</body>
</html>