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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
|
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=501257
-->
<head>
<title>Test for the classList element attribute</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="http://www.whatwg.org/specs/web-apps/current-work/#dom-classlist">classList DOM attribute</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script type="application/javascript">
/** Test for Bug 501257 **/
const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
const SVG_NS = "http://www.w3.org/2000/svg";
const XHTML_NS = "http://www.w3.org/1999/xhtml"
const MATHML_NS = "http://www.w3.org/1998/Math/MathML";
var gMutationEvents = [];
function onAttrModified(event) {
is(event.attrName, "class", "mutation on unexpected attribute");
gMutationEvents.push({
attrChange: event.attrChange,
prevValue: event.prevValue,
newValue: event.newValue,
});
}
function checkModification(e, funcName, args, expectedRes, before, after, expectedException) {
if (!Array.isArray(args)) {
args = [args];
}
var shouldThrow = typeof(expectedException) === "string";
if (shouldThrow) {
// If an exception is thrown, the class attribute shouldn't change.
after = before;
}
if (before === null)
e.removeAttribute("class");
else
e.setAttribute("class", before);
var contextMsg = "(checkModification: funcName=" + funcName + ",args=" +
JSON.stringify(args) + ",expectedRes=" + expectedRes +
",before=" + before + ",after=" + after + ")";
gMutationEvents = [];
e.addEventListener("DOMAttrModified", onAttrModified, false);
try {
var list = e.classList;
var res = list[funcName].apply(list, args);
if (shouldThrow)
ok(false, "classList modification didn't throw " + contextMsg);
} catch (e) {
if (!shouldThrow)
ok(false, "classList modification threw an exception " + contextMsg);
is(e.name, expectedException, "wrong exception thrown " + contextMsg);
}
e.removeEventListener("DOMAttrModified", onAttrModified, false);
if (expectedRes !== null)
is(res, expectedRes, "wrong return value from " + funcName +
" " + contextMsg);
var expectedAfter = after;
// XUL returns an empty string when getting a nonexistent class attribute.
if (e.namespaceURI == XUL_NS && expectedAfter === null)
expectedAfter = "";
is(e.getAttribute("class"), expectedAfter, "wrong class after modification " +
contextMsg);
var expectedMutation = before != after;
is(gMutationEvents.length, expectedMutation ? 1 : 0,
"unexpected mutation event count " + contextMsg);
if (expectedMutation && gMutationEvents.length) {
is(gMutationEvents[0].attrChange,
before == null ? MutationEvent.ADDITION : MutationEvent.MODIFICATION,
"wrong type of attribute change " + contextMsg);
// If there wasn't any previous attribute, prevValue will return an empty
// string.
var expectedPrevValue = before === null ? "" : before;
is(gMutationEvents[0].prevValue, expectedPrevValue,
"wrong previous value " + contextMsg);
is(gMutationEvents[0].newValue, after, "wrong new value " + contextMsg);
}
}
function assignToClassListStrict(e) {
"use strict";
try {
e.classList = "foo";
ok(true, "assigning to classList didn't throw");
e.removeAttribute("class");
} catch (e) {
ok(false, "assigning to classList threw");
}
}
function assignToClassList(e) {
try {
var expect = e.classList;
e.classList = "foo";
ok(true, "assigning to classList didn't throw");
is(e.classList, expect, "classList should be unchanged after assignment");
e.removeAttribute("class");
} catch (e) {
ok(false, "assigning to classList threw");
}
}
function testClassList(e) {
// basic tests
isnot(e.classList, undefined, "no classList attribute");
is(typeof(e.classList.contains), "function",
"no classList.contains function");
is(typeof(e.classList.add), "function", "no classList.add function");
is(typeof(e.classList.remove), "function", "no classList.remove function");
is(typeof(e.classList.toggle), "function", "no classList.toggle function");
assignToClassListStrict(e);
assignToClassList(e);
// length attribute
is(e.classList.length, 0, "wrong classList.length value");
e.setAttribute("class", "");
is(e.classList.length, 0, "wrong classList.length value");
e.setAttribute("class", " \t \f");
is(e.classList.length, 0, "wrong classList.length value");
e.setAttribute("class", "a");
is(e.classList.length, 1, "wrong classList.length value");
e.setAttribute("class", "a A");
is(e.classList.length, 2, "wrong classList.length value");
e.setAttribute("class", "\r\na\t\f");
is(e.classList.length, 1, "wrong classList.length value");
e.setAttribute("class", "a a");
is(e.classList.length, 2, "wrong classList.length value");
e.setAttribute("class", "a a a a a a");
is(e.classList.length, 6, "wrong classList.length value");
e.setAttribute("class", "a a b b");
is(e.classList.length, 4, "wrong classList.length value");
e.setAttribute("class", "a A B b");
is(e.classList.length, 4, "wrong classList.length value");
e.setAttribute("class", "a b c c b a a b c c");
is(e.classList.length, 10, "wrong classList.length value");
// [Stringifies]
ok(DOMTokenList.prototype.hasOwnProperty("toString"),
"Should have own toString on DOMTokenList")
e.removeAttribute("class");
is(e.classList.toString(), "", "wrong classList.toString() value");
is(e.classList + "", "", "wrong classList string conversion value");
e.setAttribute("class", "foo");
is(e.classList.toString(), "foo", "wrong classList.toString() value");
is(e.classList + "", "foo", "wrong classList string conversion value");
// item() method
e.setAttribute("class", "a");
is(e.classList.item(-1), null, "wrong classList.item() result");
is(e.classList[-1], undefined, "wrong classList[] result");
is(e.classList.item(0), "a", "wrong classList.item() result");
is(e.classList[0], "a", "wrong classList[] result");
is(e.classList.item(1), null, "wrong classList.item() result");
is(e.classList[1], undefined, "wrong classList[] result");
e.setAttribute("class", "aa AA aa");
is(e.classList.item(-1), null, "wrong classList.item() result");
is(e.classList[-1], undefined, "wrong classList[] result");
is(e.classList.item(0), "aa", "wrong classList.item() result");
is(e.classList[0], "aa", "wrong classList[] result");
is(e.classList.item(1), "AA", "wrong classList.item() result");
is(e.classList[1], "AA", "wrong classList[] result");
is(e.classList.item(2), "aa", "wrong classList.item() result");
is(e.classList[2], "aa", "wrong classList[] result");
is(e.classList.item(3), null, "wrong classList.item() result");
is(e.classList[3], undefined, "wrong classList[] result");
is(e.classList.item(0xffffffff), null, "wrong classList.item() result");
is(e.classList[0xffffffff], undefined, "wrong classList[] result");
is(e.classList.item(0xfffffffe), null, "wrong classList.item() result");
is(e.classList[0xffffffe], undefined, "wrong classList[] result");
e.setAttribute("class", "a b");
is(e.classList.item(-1), null, "wrong classList.item() result");
is(e.classList[-1], undefined, "wrong classList[] result");
is(e.classList.item(0), "a", "wrong classList.item() result");
is(e.classList[0], "a", "wrong classList[] result");
is(e.classList.item(1), "b", "wrong classList.item() result");
is(e.classList[1], "b", "wrong classList[] result");
is(e.classList.item(2), null, "wrong classList.item() result");
is(e.classList[2], undefined, "wrong classList[] result");
// contains() method
e.removeAttribute("class");
is(e.classList.contains("a"), false, "wrong classList.contains() result");
try {
e.classList.contains("");
ok(true, "classList.contains(empty_string) didn't throw");
} catch (e) {
ok(false, "classList.contains(empty_string) threw");
}
try {
e.classList.contains(" ");
ok(true, "classList.contains(string_with_spaces) didn't throw");
} catch (e) {
ok(false, "classList.contains(string_with_spaces) threw");
}
try {
e.classList.contains("aa ");
ok(true, "classList.contains(string_with_spaces) didn't throw");
} catch (e) {
ok(false, "classList.contains(string_with_spaces) threw");
}
e.setAttribute("class", "");
is(e.classList.contains("a"), false, "wrong classList.contains() result");
e.setAttribute("class", "a");
is(e.classList.contains("a"), true, "wrong classList.contains() result");
is(e.classList.contains("aa"), false, "wrong classList.contains() result");
is(e.classList.contains("b"), false, "wrong classList.contains() result");
e.setAttribute("class", "aa AA");
is(e.classList.contains("aa"), true, "wrong classList.contains() result");
is(e.classList.contains("AA"), true, "wrong classList.contains() result");
is(e.classList.contains("aA"), false, "wrong classList.contains() result");
e.setAttribute("class", "a a a");
is(e.classList.contains("a"), true, "wrong classList.contains() result");
is(e.classList.contains("aa"), false, "wrong classList.contains() result");
is(e.classList.contains("b"), false, "wrong classList.contains() result");
e.setAttribute("class", "a b c");
is(e.classList.contains("a"), true, "wrong classList.contains() result");
is(e.classList.contains("b"), true, "wrong classList.contains() result");
// Test for bug 530171
e.setAttribute("class", "null undefined");
is(e.classList.contains(null), true, "wrong classList.contains() result");
is(e.classList.contains(undefined), true, "wrong classList.contains() result");
// add() method
function checkAdd(before, argument, after, expectedException) {
checkModification(e, "add", argument, null, before, after, expectedException);
}
checkAdd(null, "", null, "SyntaxError");
checkAdd(null, ["a", ""], null, "SyntaxError");
checkAdd(null, " ", null, "InvalidCharacterError");
checkAdd(null, ["a", " "], null, "InvalidCharacterError");
checkAdd(null, ["a", "aa "], null, "InvalidCharacterError");
checkAdd("a", "a", "a");
checkAdd("aa", "AA", "aa AA");
checkAdd("a b c", "a", "a b c");
checkAdd("a a a b", "a", "a a a b");
checkAdd(null, "a", "a");
checkAdd("", "a", "a");
checkAdd(" ", "a", " a");
checkAdd(" \f", "a", " \fa");
checkAdd("a", "b", "a b");
checkAdd("a b c", "d", "a b c d");
checkAdd("a b c ", "d", "a b c d");
// multiple add
checkAdd("a b c ", ["d", "e"], "a b c d e");
checkAdd("a b c ", ["a", "a"], "a b c ");
checkAdd("a b c ", ["d", "d"], "a b c d");
checkAdd("a b c ", [], "a b c ");
checkAdd(null, ["a", "b"], "a b");
checkAdd("", ["a", "b"], "a b");
// Test for bug 530171
checkAdd(null, null, "null");
checkAdd(null, undefined, "undefined");
// remove() method
function checkRemove(before, argument, after, expectedException) {
checkModification(e, "remove", argument, null, before, after, expectedException);
}
checkRemove(null, "", null, "SyntaxError");
checkRemove(null, " ", null, "InvalidCharacterError");
checkRemove(null, "aa ", null, "InvalidCharacterError");
checkRemove(null, "a", null);
checkRemove("", "a", "");
checkRemove("a b c", "d", "a b c");
checkRemove("a b c", "A", "a b c");
checkRemove(" a a a ", "a", "");
checkRemove("a b", "a", "b");
checkRemove("a b ", "a", "b");
checkRemove("a a b", "a", "b");
checkRemove("aa aa bb", "aa", "bb");
checkRemove("a a b a a c a a", "a", "b c");
checkRemove("a b c", "b", "a c");
checkRemove("aaa bbb ccc", "bbb", "aaa ccc");
checkRemove(" a b c ", "b", "a c");
checkRemove("a b b b c", "b", "a c");
checkRemove("a b c", "c", "a b");
checkRemove(" a b c ", "c", "a b");
checkRemove("a b c c c", "c", "a b");
checkRemove("a b a c a d a", "a", "b c d");
checkRemove("AA BB aa CC AA dd aa", "AA", "BB aa CC dd aa");
checkRemove("\ra\na\ta\f", "a", "");
// multiple remove
checkRemove("a b c ", ["d", "e"], "a b c");
checkRemove("a b c ", ["a", "b"], "c");
checkRemove("a b c ", ["a", "c"], "b");
checkRemove("a b c ", ["a", "a"], "b c");
checkRemove("a b c ", ["d", "d"], "a b c");
checkRemove("a b c ", [], "a b c");
checkRemove(null, ["a", "b"], null);
checkRemove("", ["a", "b"], "");
// Test for bug 530171
checkRemove("null", null, "");
checkRemove("undefined", undefined, "");
// toggle() method
function checkToggle(before, argument, expectedRes, after, expectedException) {
checkModification(e, "toggle", argument, expectedRes, before, after, expectedException);
}
checkToggle(null, "", null, null, "SyntaxError");
checkToggle(null, "aa ", null, null, "InvalidCharacterError");
checkToggle(null, "a", true, "a");
checkToggle("", "a", true, "a");
checkToggle(" ", "a", true, " a");
checkToggle(" \f", "a", true, " \fa");
checkToggle("a", "b", true, "a b");
checkToggle("a", "A", true, "a A");
checkToggle("a b c", "d", true, "a b c d");
checkToggle("a b c", "d", true, "a b c d");
checkToggle("a", "a", false, "");
checkToggle(" a a a ", "a", false, "");
checkToggle(" A A A ", "a", true, " A A A a");
checkToggle(" a b c ", "b", false, "a c");
checkToggle(" a b c b b", "b", false, "a c");
checkToggle(" a b c ", "c", false, "a b");
checkToggle(" a b c ", "a", false, "b c");
// Test for bug 530171
checkToggle("null", null, false, "");
checkToggle("", null, true, "null");
checkToggle("undefined", undefined, false, "");
checkToggle("", undefined, true, "undefined");
// tests for the force argument handling
function checkForceToggle(before, argument, force, expectedRes, after, expectedException) {
checkModification(e, "toggle", [argument, force], expectedRes, before, after, expectedException);
}
checkForceToggle("", "a", true, true, "a");
checkForceToggle("a", "a", true, true, "a");
checkForceToggle("a", "b", true, true, "a b");
checkForceToggle("a b", "b", true, true, "a b");
checkForceToggle("", "a", false, false, "");
checkForceToggle("a", "a", false, false, "");
checkForceToggle("a", "b", false, false, "a");
checkForceToggle("a b", "b", false, false, "a");
}
var content = document.getElementById("content");
var htmlNode = document.createElement("div");
content.appendChild(htmlNode);
testClassList(htmlNode);
var xhtmlNode = document.createElementNS(XHTML_NS, "div");
content.appendChild(xhtmlNode);
testClassList(xhtmlNode);
var xulNode = document.createElementNS(XUL_NS, "box");
content.appendChild(xulNode);
testClassList(xulNode);
var mathMLNode = document.createElementNS(MATHML_NS, "math");
content.appendChild(mathMLNode);
testClassList(mathMLNode);
var xmlNode = document.createElementNS(null, "foo");
content.appendChild(xmlNode);
testClassList(xmlNode);
var fooNode = document.createElementNS("http://example.org/foo", "foo");
content.appendChild(fooNode);
testClassList(fooNode);
</script>
</pre>
</body>
</html>
|