Debian Codegraph @

testing/web-platform/mozilla/tests/dom/classList.html

raw
1 <!doctype html>
2 <meta charset=utf-8>
3 <title>Test for the classList element attribute</title>
4 <script src=/resources/testharness.js></script>
5 <script src=/resources/testharnessreport.js></script>
6 <div id="content"></div>
7 <script>
8 // This should be the same as dom/nodes/Element-classlist.html in the upstream
9 // tests! We have a couple of bits changed, which are marked with comments
10 // "LOCAL MODIFICATION". Do not change this without changing the upstream test
11 // as well! Merging upstream changes here occasionally might also be nice.
12
13 const SVG_NS = "http://www.w3.org/2000/svg";
14 const XHTML_NS = "http://www.w3.org/1999/xhtml"
15 const MATHML_NS = "http://www.w3.org/1998/Math/MathML";
16
17 // BEGIN LOCAL MODIFICATION: The spec does not have the onAttrModified event
18 // and does not want it, but we still support it.
19 var gMutationEvents = [];
20
21 function onAttrModified(event) {
22 assert_equals(event.attrName, "class", "mutation on unexpected attribute");
23
24 gMutationEvents.push({
25 attrChange: event.attrChange,
26 prevValue: event.prevValue,
27 newValue: event.newValue,
28 });
29 }
30 // END LOCAL MODIFICATION
31
32 function setClass(e, newVal) {
33 if (newVal === null) {
34 e.removeAttribute("class");
35 } else {
36 e.setAttribute("class", newVal);
37 }
38 }
39
40 function checkModification(e, funcName, args, expectedRes, before, after,
41 expectedException, desc) {
42 if (!Array.isArray(args)) {
43 args = [args];
44 }
45
46 test(function() {
47 var shouldThrow = typeof(expectedException) === "string";
48 if (shouldThrow) {
49 // If an exception is thrown, the class attribute shouldn't change.
50 after = before;
51 }
52 setClass(e, before);
53
54 // BEGIN LOCAL MODIFICATION
55 gMutationEvents = [];
56 e.addEventListener("DOMAttrModified", onAttrModified);
57 // END LOCAL MODIFICATION
58 var obs;
59 // If we have MutationObservers available, do some checks to make
60 // sure attribute sets are happening at sane times.
61 if (self.MutationObserver) {
62 obs = new MutationObserver(() => {});
63 obs.observe(e, { attributes: true });
64 }
65 if (shouldThrow) {
66 assert_throws_dom(expectedException, function() {
67 var list = e.classList;
68 var res = list[funcName].apply(list, args);
69 });
70 } else {
71 var list = e.classList;
72 var res = list[funcName].apply(list, args);
73 }
74 if (obs) {
75 var mutationRecords = obs.takeRecords();
76 obs.disconnect();
77 if (shouldThrow) {
78 assert_equals(mutationRecords.length, 0,
79 "There should have been no mutation");
80 } else if (funcName == "replace") {
81 assert_equals(mutationRecords.length == 1,
82 expectedRes,
83 "Should have a mutation exactly when replace() returns true");
84 } else {
85 // For other functions, would need to check when exactly
86 // mutations are supposed to happen.
87 }
88 }
89 // BEGIN LOCAL MODIFICATION
90 e.removeEventListener("DOMAttrModified", onAttrModified);
91 // END LOCAL MODIFICATION
92 if (!shouldThrow) {
93 assert_equals(res, expectedRes, "wrong return value");
94 }
95
96 var expectedAfter = after;
97
98 assert_equals(e.getAttribute("class"), expectedAfter,
99 "wrong class after modification");
100 // BEGIN LOCAL MODIFICATION
101 var expectedMutation = before != after;
102 assert_equals(gMutationEvents.length, expectedMutation ? 1 : 0,
103 "unexpected mutation event count");
104 if (expectedMutation && gMutationEvents.length) {
105 assert_equals(gMutationEvents[0].attrChange,
106 before == null ? MutationEvent.ADDITION
107 : MutationEvent.MODIFICATION,
108 "wrong type of attribute change");
109 // If there wasn't any previous attribute, prevValue will return an empty
110 // string.
111 var expectedPrevValue = before === null ? "" : before;
112 assert_equals(gMutationEvents[0].prevValue, expectedPrevValue,
113 "wrong previous value");
114 assert_equals(gMutationEvents[0].newValue, after, "wrong new value");
115 }
116 // END LOCAL MODIFICATION
117 }, "classList." + funcName + "(" + args.map(format_value).join(", ") +
118 ") with attribute value " + format_value(before) + desc);
119 }
120
121 function assignToClassListStrict(e) {
122 "use strict";
123 e.classList = "foo";
124 e.removeAttribute("class");
125 }
126
127 function assignToClassList(e) {
128 var expect = e.classList;
129 e.classList = "foo";
130 assert_equals(e.classList, expect,
131 "classList should be unchanged after assignment");
132 e.removeAttribute("class");
133 }
134
135 function testClassList(e, desc) {
136
137 // assignment
138
139 test(function() {
140 assignToClassListStrict(e);
141 assignToClassList(e);
142 }, "Assigning to classList" + desc);
143
144 // supports
145 test(function() {
146 assert_throws_js(TypeError, function() {
147 e.classList.supports("a");
148 })
149 }, ".supports() must throw TypeError" + desc);
150
151 // length attribute
152
153 function checkLength(value, length) {
154 test(function() {
155 setClass(e, value);
156 assert_equals(e.classList.length, length);
157 }, "classList.length when " +
158 (value === null ? "removed" : "set to " + format_value(value)) + desc);
159 }
160
161 checkLength(null, 0);
162 checkLength("", 0);
163 checkLength(" \t \f", 0);
164 checkLength("a", 1);
165 checkLength("a A", 2);
166 checkLength("\r\na\t\f", 1);
167 checkLength("a a", 1);
168 checkLength("a a a a a a", 1);
169 checkLength("a a b b", 2);
170 checkLength("a A B b", 4);
171 checkLength("a b c c b a a b c c", 3);
172 checkLength(" a a b", 2);
173 checkLength("a\tb\nc\fd\re f", 6);
174
175 // [Stringifies]
176
177 function checkStringifier(value, expected) {
178 test(function() {
179 setClass(e, value);
180 assert_equals(e.classList.toString(), expected);
181 }, "classList.toString() when " +
182 (value === null ? "removed" : "set to " + format_value(value)) + desc);
183 }
184
185 checkStringifier(null, "");
186 checkStringifier("foo", "foo");
187 checkStringifier(" a a b", " a a b");
188
189 // item() method
190
191 function checkItems(attributeValue, expectedValues) {
192 function checkItemFunction(index, expected) {
193 assert_equals(e.classList.item(index), expected,
194 "classList.item(" + index + ")");
195 }
196
197 function checkItemArray(index, expected) {
198 assert_equals(e.classList[index], expected, "classList[" + index + "]");
199 }
200
201 test(function() {
202 setClass(e, attributeValue);
203
204 checkItemFunction(-1, null);
205 checkItemArray(-1, undefined);
206
207 var i = 0;
208 while (i < expectedValues.length) {
209 checkItemFunction(i, expectedValues[i]);
210 checkItemArray(i, expectedValues[i]);
211 i++;
212 }
213
214 checkItemFunction(i, null);
215 checkItemArray(i, undefined);
216
217 checkItemFunction(0xffffffff, null);
218 checkItemArray(0xffffffff, undefined);
219
220 checkItemFunction(0xfffffffe, null);
221 checkItemArray(0xfffffffe, undefined);
222 }, "classList.item() when set to " + format_value(attributeValue) + desc);
223 }
224
225 checkItems(null, []);
226 checkItems("a", ["a"]);
227 checkItems("aa AA aa", ["aa", "AA"]);
228 checkItems("a b", ["a", "b"]);
229 checkItems(" a a b", ["a", "b"]);
230 checkItems("\t\n\f\r a\t\n\f\r b\t\n\f\r ", ["a", "b"]);
231
232 // contains() method
233
234 function checkContains(attributeValue, args, expectedRes) {
235 if (!Array.isArray(expectedRes)) {
236 expectedRes = Array(args.length).fill(expectedRes);
237 }
238 setClass(e, attributeValue);
239 for (var i = 0; i < args.length; i++) {
240 test(function() {
241 assert_equals(e.classList.contains(args[i]), expectedRes[i],
242 "classList.contains(\"" + args[i] + "\")");
243 }, "classList.contains(" + format_value(args[i]) + ") when set to " +
244 format_value(attributeValue) + desc);
245 }
246 }
247
248 checkContains(null, ["a", "", " "], false);
249 checkContains("", ["a"], false);
250
251 checkContains("a", ["a"], true);
252 checkContains("a", ["aa", "b", "A", "a.", "a)",, "a'", 'a"', "a$", "a~",
253 "a?", "a\\"], false);
254
255 // All "ASCII whitespace" per spec, before and after
256 checkContains("a", ["a\t", "\ta", "a\n", "\na", "a\f", "\fa", "a\r", "\ra",
257 "a ", " a"], false);
258
259 checkContains("aa AA", ["aa", "AA", "aA"], [true, true, false]);
260 checkContains("a a a", ["a", "aa", "b"], [true, false, false]);
261 checkContains("a b c", ["a", "b"], true);
262
263 checkContains("null undefined", [null, undefined], true);
264 checkContains("\t\n\f\r a\t\n\f\r b\t\n\f\r ", ["a", "b"], true);
265
266 // add() method
267
268 function checkAdd(before, argument, after, expectedException) {
269 checkModification(e, "add", argument, undefined, before, after,
270 expectedException, desc);
271 // Also check force toggle
272 // XXX https://github.com/whatwg/dom/issues/443
273 //if (!Array.isArray(argument)) {
274 // checkModification(e, "toggle", [argument, true], true, before, after,
275 // expectedException);
276 //}
277 }
278
279 checkAdd(null, "", null, "SyntaxError");
280 checkAdd(null, ["a", ""], null, "SyntaxError");
281 checkAdd(null, " ", null, "InvalidCharacterError");
282 checkAdd(null, "\ta", null, "InvalidCharacterError");
283 checkAdd(null, "a\t", null, "InvalidCharacterError");
284 checkAdd(null, "\na", null, "InvalidCharacterError");
285 checkAdd(null, "a\n", null, "InvalidCharacterError");
286 checkAdd(null, "\fa", null, "InvalidCharacterError");
287 checkAdd(null, "a\f", null, "InvalidCharacterError");
288 checkAdd(null, "\ra", null, "InvalidCharacterError");
289 checkAdd(null, "a\r", null, "InvalidCharacterError");
290 checkAdd(null, " a", null, "InvalidCharacterError");
291 checkAdd(null, "a ", null, "InvalidCharacterError");
292 checkAdd(null, ["a", " "], null, "InvalidCharacterError");
293 checkAdd(null, ["a", "aa "], null, "InvalidCharacterError");
294
295 checkAdd("a", "a", "a");
296 checkAdd("aa", "AA", "aa AA");
297 checkAdd("a b c", "a", "a b c");
298 checkAdd("a a a b", "a", "a b");
299 checkAdd(null, "a", "a");
300 checkAdd("", "a", "a");
301 checkAdd(" ", "a", "a");
302 checkAdd(" \f", "a", "a");
303 checkAdd("a", "b", "a b");
304 checkAdd("a b c", "d", "a b c d");
305 checkAdd("a b c ", "d", "a b c d");
306 checkAdd(" a a b", "c", "a b c");
307 checkAdd(" a a b", "a", "a b");
308 checkAdd("\t\n\f\r a\t\n\f\r b\t\n\f\r ", "c", "a b c");
309
310 // multiple add
311 checkAdd("a b c ", ["d", "e"], "a b c d e");
312 checkAdd("a b c ", ["a", "a"], "a b c");
313 checkAdd("a b c ", ["d", "d"], "a b c d");
314 checkAdd("a b c a ", [], "a b c");
315 checkAdd(null, ["a", "b"], "a b");
316 checkAdd("", ["a", "b"], "a b");
317
318 checkAdd(null, null, "null");
319 checkAdd(null, undefined, "undefined");
320
321 // remove() method
322
323 function checkRemove(before, argument, after, expectedException) {
324 checkModification(e, "remove", argument, undefined, before, after,
325 expectedException, desc);
326 // Also check force toggle
327 // XXX https://github.com/whatwg/dom/issues/443
328 //if (!Array.isArray(argument)) {
329 // checkModification(e, "toggle", [argument, false], false, before, after,
330 // expectedException);
331 //}
332 }
333
334 checkRemove(null, "", null, "SyntaxError");
335 checkRemove(null, " ", null, "InvalidCharacterError");
336 checkRemove("\ta", "\ta", "\ta", "InvalidCharacterError");
337 checkRemove("a\t", "a\t", "a\t", "InvalidCharacterError");
338 checkRemove("\na", "\na", "\na", "InvalidCharacterError");
339 checkRemove("a\n", "a\n", "a\n", "InvalidCharacterError");
340 checkRemove("\fa", "\fa", "\fa", "InvalidCharacterError");
341 checkRemove("a\f", "a\f", "a\f", "InvalidCharacterError");
342 checkRemove("\ra", "\ra", "\ra", "InvalidCharacterError");
343 checkRemove("a\r", "a\r", "a\r", "InvalidCharacterError");
344 checkRemove(" a", " a", " a", "InvalidCharacterError");
345 checkRemove("a ", "a ", "a ", "InvalidCharacterError");
346 checkRemove("aa ", "aa ", null, "InvalidCharacterError");
347
348 checkRemove(null, "a", null);
349 checkRemove("", "a", "");
350 checkRemove("a b c", "d", "a b c");
351 checkRemove("a b c", "A", "a b c");
352 checkRemove(" a a a ", "a", "");
353 checkRemove("a b", "a", "b");
354 checkRemove("a b ", "a", "b");
355 checkRemove("a a b", "a", "b");
356 checkRemove("aa aa bb", "aa", "bb");
357 checkRemove("a a b a a c a a", "a", "b c");
358
359 checkRemove("a b c", "b", "a c");
360 checkRemove("aaa bbb ccc", "bbb", "aaa ccc");
361 checkRemove(" a b c ", "b", "a c");
362 checkRemove("a b b b c", "b", "a c");
363
364 checkRemove("a b c", "c", "a b");
365 checkRemove(" a b c ", "c", "a b");
366 checkRemove("a b c c c", "c", "a b");
367
368 checkRemove("a b a c a d a", "a", "b c d");
369 checkRemove("AA BB aa CC AA dd aa", "AA", "BB aa CC dd");
370
371 checkRemove("\ra\na\ta\f", "a", "");
372 checkRemove("\t\n\f\r a\t\n\f\r b\t\n\f\r ", "a", "b");
373
374 // multiple remove
375 checkRemove("a b c ", ["d", "e"], "a b c");
376 checkRemove("a b c ", ["a", "b"], "c");
377 checkRemove("a b c ", ["a", "c"], "b");
378 checkRemove("a b c ", ["a", "a"], "b c");
379 checkRemove("a b c ", ["d", "d"], "a b c");
380 checkRemove("a b c ", [], "a b c");
381 checkRemove(null, ["a", "b"], null);
382 checkRemove("", ["a", "b"], "");
383 checkRemove("a a", [], "a");
384
385 checkRemove("null", null, "");
386 checkRemove("undefined", undefined, "");
387
388 // toggle() method
389
390 function checkToggle(before, argument, expectedRes, after, expectedException) {
391 checkModification(e, "toggle", argument, expectedRes, before, after,
392 expectedException, desc);
393 }
394
395 checkToggle(null, "", null, null, "SyntaxError");
396 checkToggle(null, "aa ", null, null, "InvalidCharacterError");
397
398 checkToggle(null, "a", true, "a");
399 checkToggle("", "a", true, "a");
400 checkToggle(" ", "a", true, "a");
401 checkToggle(" \f", "a", true, "a");
402 checkToggle("a", "b", true, "a b");
403 checkToggle("a", "A", true, "a A");
404 checkToggle("a b c", "d", true, "a b c d");
405 checkToggle(" a a b", "d", true, "a b d");
406
407 checkToggle("a", "a", false, "");
408 checkToggle(" a a a ", "a", false, "");
409 checkToggle(" A A A ", "a", true, "A a");
410 checkToggle(" a b c ", "b", false, "a c");
411 checkToggle(" a b c b b", "b", false, "a c");
412 checkToggle(" a b c ", "c", false, "a b");
413 checkToggle(" a b c ", "a", false, "b c");
414 checkToggle(" a a b", "b", false, "a");
415 checkToggle("\t\n\f\r a\t\n\f\r b\t\n\f\r ", "a", false, "b");
416 checkToggle("\t\n\f\r a\t\n\f\r b\t\n\f\r ", "c", true, "a b c");
417
418 checkToggle("null", null, false, "");
419 checkToggle("", null, true, "null");
420 checkToggle("undefined", undefined, false, "");
421 checkToggle("", undefined, true, "undefined");
422
423
424 // tests for the force argument handling
425 // XXX Remove these if https://github.com/whatwg/dom/issues/443 is fixed
426
427 function checkForceToggle(before, argument, force, expectedRes, after, expectedException) {
428 checkModification(e, "toggle", [argument, force], expectedRes, before,
429 after, expectedException, desc);
430 }
431
432 checkForceToggle("", "a", true, true, "a");
433 checkForceToggle("a", "a", true, true, "a");
434 checkForceToggle("a", "b", true, true, "a b");
435 checkForceToggle("a b", "b", true, true, "a b");
436 checkForceToggle("", "a", false, false, "");
437 checkForceToggle("a", "a", false, false, "");
438 checkForceToggle("a", "b", false, false, "a");
439 checkForceToggle("a b", "b", false, false, "a");
440
441
442 // replace() method
443 function checkReplace(before, token, newToken, expectedRes, after, expectedException) {
444 checkModification(e, "replace", [token, newToken], expectedRes, before,
445 after, expectedException, desc);
446 }
447
448 checkReplace(null, "", "a", null, null, "SyntaxError");
449 checkReplace(null, "", " ", null, null, "SyntaxError");
450 checkReplace(null, " ", "a", null, null, "InvalidCharacterError");
451 checkReplace(null, "\ta", "b", null, null, "InvalidCharacterError");
452 checkReplace(null, "a\t", "b", null, null, "InvalidCharacterError");
453 checkReplace(null, "\na", "b", null, null, "InvalidCharacterError");
454 checkReplace(null, "a\n", "b", null, null, "InvalidCharacterError");
455 checkReplace(null, "\fa", "b", null, null, "InvalidCharacterError");
456 checkReplace(null, "a\f", "b", null, null, "InvalidCharacterError");
457 checkReplace(null, "\ra", "b", null, null, "InvalidCharacterError");
458 checkReplace(null, "a\r", "b", null, null, "InvalidCharacterError");
459 checkReplace(null, " a", "b", null, null, "InvalidCharacterError");
460 checkReplace(null, "a ", "b", null, null, "InvalidCharacterError");
461
462 checkReplace(null, "a", "", null, null, "SyntaxError");
463 checkReplace(null, " ", "", null, null, "SyntaxError");
464 checkReplace(null, "a", " ", null, null, "InvalidCharacterError");
465 checkReplace(null, "b", "\ta", null, null, "InvalidCharacterError");
466 checkReplace(null, "b", "a\t", null, null, "InvalidCharacterError");
467 checkReplace(null, "b", "\na", null, null, "InvalidCharacterError");
468 checkReplace(null, "b", "a\n", null, null, "InvalidCharacterError");
469 checkReplace(null, "b", "\fa", null, null, "InvalidCharacterError");
470 checkReplace(null, "b", "a\f", null, null, "InvalidCharacterError");
471 checkReplace(null, "b", "\ra", null, null, "InvalidCharacterError");
472 checkReplace(null, "b", "a\r", null, null, "InvalidCharacterError");
473 checkReplace(null, "b", " a", null, null, "InvalidCharacterError");
474 checkReplace(null, "b", "a ", null, null, "InvalidCharacterError");
475
476 checkReplace("a", "a", "a", true, "a");
477 checkReplace("a", "a", "b", true, "b");
478 checkReplace("a", "A", "b", false, "a");
479 checkReplace("a b", "b", "A", true, "a A");
480 checkReplace("a b", "c", "a", false, "a b");
481 checkReplace("a b c", "d", "e", false, "a b c");
482 // https://github.com/whatwg/dom/issues/443
483 checkReplace("a a a b", "a", "a", true, "a b");
484 checkReplace("a a a b", "c", "d", false, "a a a b");
485 checkReplace(null, "a", "b", false, null);
486 checkReplace("", "a", "b", false, "");
487 checkReplace(" ", "a", "b", false, " ");
488 checkReplace(" a \f", "a", "b", true, "b");
489 checkReplace("a b c", "b", "d", true, "a d c");
490 checkReplace("a b c", "c", "a", true, "a b");
491 checkReplace("c b a", "c", "a", true, "a b");
492 checkReplace("a b a", "a", "c", true, "c b");
493 checkReplace("a b a", "b", "c", true, "a c");
494 checkReplace(" a a b", "a", "c", true, "c b");
495 checkReplace(" a a b", "b", "c", true, "a c");
496 checkReplace("\t\n\f\r a\t\n\f\r b\t\n\f\r ", "a", "c", true, "c b");
497 checkReplace("\t\n\f\r a\t\n\f\r b\t\n\f\r ", "b", "c", true, "a c");
498
499 checkReplace("a null", null, "b", true, "a b");
500 checkReplace("a b", "a", null, true, "null b");
501 checkReplace("a undefined", undefined, "b", true, "a b");
502 checkReplace("a b", "a", undefined, true, "undefined b");
503 }
504
505 var content = document.getElementById("content");
506
507 var htmlNode = document.createElement("div");
508 content.appendChild(htmlNode);
509 testClassList(htmlNode, " (HTML node)");
510
511 var xhtmlNode = document.createElementNS(XHTML_NS, "div");
512 content.appendChild(xhtmlNode);
513 testClassList(xhtmlNode, " (XHTML node)");
514
515 var mathMLNode = document.createElementNS(MATHML_NS, "math");
516 content.appendChild(mathMLNode);
517 testClassList(mathMLNode, " (MathML node)");
518
519 var xmlNode = document.createElementNS(null, "foo");
520 content.appendChild(xmlNode);
521 testClassList(xmlNode, " (XML node with null namespace)");
522
523 var fooNode = document.createElementNS("http://example.org/foo", "foo");
524 content.appendChild(fooNode);
525 testClassList(fooNode, " (foo node)");
526 </script>