Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(117)

Side by Side Diff: third_party/WebKit/LayoutTests/imported/web-platform-tests/dom/nodes/attributes.html

Issue 1529523002: Import dom/ from web-platform-tests (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: tweak W3CImportExpectations Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 <!DOCTYPE html>
2 <meta charset=utf-8>
3 <title>Attributes tests</title>
4 <link rel=help href="https://dom.spec.whatwg.org/#attr">
5 <link rel=help href="https://dom.spec.whatwg.org/#dom-element-setattribute">
6 <link rel=help href="https://dom.spec.whatwg.org/#dom-element-setattributens">
7 <script src="../../../../resources/testharness.js"></script>
8 <script src="../../../../resources/testharnessreport.js"></script>
9 <script src="attributes.js"></script>
10 <script src="productions.js"></script>
11 <div id="log"></div>
12 <span id="test1"></span>
13 <span class="&amp;&lt;&gt;foo"></span>
14 <script>
15 var XML = "http://www.w3.org/XML/1998/namespace"
16 var XMLNS = "http://www.w3.org/2000/xmlns/"
17
18 // AttrExodus
19 test(function() {
20 document.body.setAttribute("abc", "pass")
21 var attr = document.body.attributes[0]
22 assert_true(attr instanceof Attr)
23 assert_false(attr instanceof Node)
24 assert_throws(new TypeError(), function() { attr.appendChild(document.createTe xtNode("fail")) })
25 assert_throws(new TypeError(), function() { attr.appendChild(null) })
26 assert_equals(attr.value, "pass")
27 assert_false("childNodes" in attr, "Should not have childNodes")
28 }, "AttrExodus")
29
30 // setAttribute exhaustive tests
31 // Step 1
32 test(function() {
33 var el = document.createElement("foo")
34 for (var i = 0; i < invalid_names.length; i++) {
35 assert_throws("INVALID_CHARACTER_ERR", function() { el.setAttribute(invalid_ names[i], "test") })
36 }
37 }, "When qualifiedName does not match the Name production, an " +
38 "INVALID_CHARACTER_ERR exception is to be thrown. (setAttribute)")
39
40 // Step 2
41 test(function() {
42 var el = document.createElement("div")
43 el.setAttribute("ALIGN", "left")
44 assert_equals(el.getAttributeNS("", "ALIGN"), null)
45 assert_equals(el.getAttributeNS("", "align"), "left")
46 assert_equals(el.getAttribute("align"), "left")
47 }, "setAttribute should lowercase its name argument (upper case attribute)")
48 test(function() {
49 var el = document.createElement("div")
50 el.setAttribute("CHEEseCaKe", "tasty")
51 assert_equals(el.getAttributeNS("", "CHEEseCaKe"), null)
52 assert_equals(el.getAttributeNS("", "cheesecake"), "tasty")
53 assert_equals(el.getAttribute("cheesecake"), "tasty")
54 }, "setAttribute should lowercase its name argument (mixed case attribute)")
55
56 // Step 3
57 test(function() {
58 var el = document.createElement("foo")
59 var tests = ["xmlns", "xmlns:a", "xmlnsx", "xmlns0"]
60 for (var i = 0; i < tests.length; i++) {
61 el.setAttribute(tests[i], "success");
62 }
63 }, "setAttribute should not throw even when qualifiedName starts with 'xmlns'")
64
65 // Step 4
66 test(function() {
67 var el = document.createElement("foo")
68 for (var i = 0; i < valid_names.length; i++) {
69 el.setAttribute(valid_names[i], "test")
70 assert_equals(el.getAttribute(valid_names[i]), "test")
71 }
72 }, "Basic functionality should be intact.")
73
74 // Step 5
75 test(function() {
76 var el = document.createElement("foo")
77 el.setAttribute("a", "1")
78 el.setAttribute("b", "2")
79 el.setAttribute("a", "3")
80 el.setAttribute("c", "4")
81 attributes_are(el, [["a", "3"],
82 ["b", "2"],
83 ["c", "4"]])
84 }, "setAttribute should not change the order of previously set attributes.")
85 test(function() {
86 var el = document.createElement("baz")
87 el.setAttributeNS("ab", "attr", "fail")
88 el.setAttributeNS("kl", "attr", "pass")
89 el.setAttribute("attr", "pass")
90 attributes_are(el, [["attr", "pass", "ab"],
91 ["attr", "pass", "kl"]])
92 }, "setAttribute should set the first attribute with the given name")
93 test(function() {
94 // Based on a test by David Flanagan.
95 var el = document.createElement("baz")
96 el.setAttributeNS("foo", "foo:bar", "1");
97 assert_equals(el.getAttribute("foo:bar"), "1")
98 attr_is(el.attributes[0], "1", "bar", "foo", "foo", "foo:bar")
99 el.setAttribute("foo:bar", "2");
100 assert_equals(el.getAttribute("foo:bar"), "2")
101 attr_is(el.attributes[0], "2", "bar", "foo", "foo", "foo:bar")
102 }, "setAttribute should set the attribute with the given qualified name")
103
104 // setAttributeNS exhaustive tests
105 // Step 1
106 test(function() {
107 var el = document.createElement("foo")
108 for (var i = 0, il = invalid_names.length; i < il; ++i) {
109 assert_throws("INVALID_CHARACTER_ERR",
110 function() { el.setAttributeNS("a", invalid_names[i], "fail") })
111 }
112 }, "When qualifiedName does not match the Name production, an " +
113 "INVALID_CHARACTER_ERR exception is to be thrown. (setAttributeNS)")
114
115 // Step 2
116 test(function() {
117 var el = document.createElement("foo")
118 for (var i = 0, il = invalid_qnames.length; i < il; ++i) {
119 assert_throws("NAMESPACE_ERR",
120 function() { el.setAttributeNS("a", invalid_qnames[i], "fail") },
121 "Expected exception for " + invalid_qnames[i] + ".")
122 }
123 }, "When qualifiedName does not match the QName production, an " +
124 "NAMESPACE_ERR exception is to be thrown.")
125
126 // Step 3
127 test(function() {
128 var el = document.createElement("foo")
129 el.setAttributeNS(null, "aa", "bb")
130 el.setAttributeNS("", "xx", "bb")
131 attributes_are(el, [["aa", "bb"],
132 ["xx", "bb"]])
133 }, "null and the empty string should result in a null namespace.")
134
135 // Step 4
136 test(function() {
137 var el = document.createElement("foo")
138 assert_throws("NAMESPACE_ERR",
139 function() { el.setAttributeNS("", "aa:bb", "fail") })
140 assert_throws("NAMESPACE_ERR",
141 function() { el.setAttributeNS(null, "aa:bb", "fail") })
142 }, "A namespace is required to use a prefix.")
143
144 // Step 5
145 test(function() {
146 var el = document.createElement("foo")
147 assert_throws("NAMESPACE_ERR",
148 function() { el.setAttributeNS("a", "xml:bb", "fail") })
149 }, "The xml prefix should not be allowed for arbitrary namespaces")
150 test(function() {
151 var el = document.createElement("foo")
152 el.setAttributeNS(XML, "a:bb", "pass")
153 assert_equals(el.attributes.length, 1)
154 attr_is(el.attributes[0], "pass", "bb", XML, "a", "a:bb")
155 }, "XML-namespaced attributes don't need an xml prefix")
156
157 // Step 6
158 test(function() {
159 var el = document.createElement("foo")
160 assert_throws("NAMESPACE_ERR",
161 function() { el.setAttributeNS("a", "xmlns:bb", "fail") })
162 }, "The xmlns prefix should not be allowed for arbitrary namespaces")
163 test(function() {
164 var el = document.createElement("foo")
165 assert_throws("NAMESPACE_ERR",
166 function() { el.setAttributeNS("a", "xmlns", "fail") })
167 }, "The xmlns qualified name should not be allowed for arbitrary namespaces")
168 test(function() {
169 var el = document.createElement("foo")
170 el.setAttributeNS("ns", "a:xmlns", "pass")
171 assert_equals(el.attributes.length, 1)
172 attr_is(el.attributes[0], "pass", "xmlns", "ns", "a", "a:xmlns")
173 }, "xmlns should be allowed as local name")
174
175 // Step 7
176 test(function() {
177 var el = document.createElement("foo")
178 assert_throws("NAMESPACE_ERR",
179 function() { el.setAttributeNS(XMLNS, "a:xmlns", "fail") })
180 assert_throws("NAMESPACE_ERR",
181 function() { el.setAttributeNS(XMLNS, "b:foo", "fail") })
182 }, "The XMLNS namespace should require xmlns as prefix or qualified name")
183 test(function() {
184 var el = document.createElement("foo")
185 el.setAttributeNS(XMLNS, "xmlns:a", "pass")
186 assert_equals(el.attributes.length, 1)
187 attr_is(el.attributes[0], "pass", "a", XMLNS, "xmlns", "xmlns:a")
188 }, "xmlns should be allowed as prefix in the XMLNS namespace")
189 test(function() {
190 var el = document.createElement("foo")
191 el.setAttributeNS(XMLNS, "xmlns", "pass")
192 assert_equals(el.attributes.length, 1)
193 attr_is(el.attributes[0], "pass", "xmlns", XMLNS, null, "xmlns")
194 }, "xmlns should be allowed as qualified name in the XMLNS namespace")
195
196 // Step 8-9
197 test(function() {
198 var el = document.createElement("foo")
199 el.setAttributeNS("a", "foo:bar", "X")
200 assert_equals(el.attributes.length, 1)
201 attr_is(el.attributes[0], "X", "bar", "a", "foo", "foo:bar")
202
203 el.setAttributeNS("a", "quux:bar", "Y")
204 assert_equals(el.attributes.length, 1)
205 attr_is(el.attributes[0], "Y", "bar", "a", "foo", "foo:bar")
206 el.removeAttributeNS("a", "bar")
207 }, "Setting the same attribute with another prefix should not change the prefix" )
208
209 // Miscellaneous tests
210 test(function() {
211 var el = document.createElement("iframe")
212 el.setAttribute("src", "file:///home")
213 assert_equals(el.getAttribute("src"), "file:///home")
214 }, "setAttribute should not throw even if a load is not allowed")
215 test(function() {
216 var docFragment = document.createDocumentFragment()
217 var newOne = document.createElement("newElement")
218 newOne.setAttribute("newdomestic", "Yes")
219 docFragment.appendChild(newOne)
220 var domesticNode = docFragment.firstChild
221 var attr = domesticNode.attributes.item(0)
222 attr_is(attr, "Yes", "newdomestic", null, null, "newdomestic")
223 }, "Attributes should work in document fragments.")
224 test(function() {
225 var el = document.createElement("foo")
226 el.setAttribute("x", "y")
227 var attr = el.attributes[0]
228 attr.value = "Y&lt;"
229 attr_is(attr, "Y&lt;", "x", null, null, "x")
230 assert_equals(el.getAttribute("x"), "Y&lt;")
231 }, "Attribute values should not be parsed.")
232 test(function() {
233 var el = document.getElementsByTagName("span")[0]
234 attr_is(el.attributes[0], "test1", "id", null, null, "id")
235 }, "Specified attributes should be accessible.")
236 test(function() {
237 var el = document.getElementsByTagName("span")[1]
238 attr_is(el.attributes[0], "&<>foo", "class", null, null, "class")
239 }, "Entities in attributes should have been expanded while parsing.")
240
241 test(function() {
242 var el = document.createElement("div")
243 assert_equals(el.hasAttribute("bar"), false)
244 assert_equals(el.hasAttributeNS(null, "bar"), false)
245 assert_equals(el.hasAttributeNS("", "bar"), false)
246 assert_equals(el.getAttribute("bar"), null)
247 assert_equals(el.getAttributeNS(null, "bar"), null)
248 assert_equals(el.getAttributeNS("", "bar"), null)
249 }, "Unset attributes return null")
250 test(function() {
251 var el = document.createElement("div")
252 el.setAttributeNS("ab", "attr", "t1")
253 el.setAttributeNS("kl", "attr", "t2")
254 assert_equals(el.hasAttribute("attr"), true)
255 assert_equals(el.hasAttributeNS("ab", "attr"), true)
256 assert_equals(el.hasAttributeNS("kl", "attr"), true)
257 assert_equals(el.getAttribute("attr"), "t1")
258 assert_equals(el.getAttributeNS("ab", "attr"), "t1")
259 assert_equals(el.getAttributeNS("kl", "attr"), "t2")
260 assert_equals(el.getAttributeNS(null, "attr"), null)
261 assert_equals(el.getAttributeNS("", "attr"), null)
262 }, "First set attribute is returned by getAttribute")
263 test(function() {
264 var el = document.createElement("div")
265 el.setAttribute("style", "color:#fff;")
266 assert_equals(el.hasAttribute("style"), true)
267 assert_equals(el.hasAttributeNS(null, "style"), true)
268 assert_equals(el.hasAttributeNS("", "style"), true)
269 assert_equals(el.getAttribute("style"), "color:#fff;")
270 assert_equals(el.getAttributeNS(null, "style"), "color:#fff;")
271 assert_equals(el.getAttributeNS("", "style"), "color:#fff;")
272 }, "Style attributes are not normalized")
273 test(function() {
274 var el = document.createElement("div")
275 el.setAttributeNS("", "ALIGN", "left")
276 assert_equals(el.hasAttribute("ALIGN"), false)
277 assert_equals(el.hasAttribute("align"), false)
278 assert_equals(el.hasAttributeNS(null, "ALIGN"), true)
279 assert_equals(el.hasAttributeNS(null, "align"), false)
280 assert_equals(el.hasAttributeNS("", "ALIGN"), true)
281 assert_equals(el.hasAttributeNS("", "align"), false)
282 assert_equals(el.getAttribute("ALIGN"), null)
283 assert_equals(el.getAttribute("align"), null)
284 assert_equals(el.getAttributeNS(null, "ALIGN"), "left")
285 assert_equals(el.getAttributeNS("", "ALIGN"), "left")
286 assert_equals(el.getAttributeNS(null, "align"), null)
287 assert_equals(el.getAttributeNS("", "align"), null)
288 el.removeAttributeNS("", "ALIGN")
289 }, "Only lowercase attributes are returned on HTML elements (upper case attribut e)")
290 test(function() {
291 var el = document.createElement("div")
292 el.setAttributeNS("", "CHEEseCaKe", "tasty")
293 assert_equals(el.hasAttribute("CHEESECAKE"), false)
294 assert_equals(el.hasAttribute("CHEEseCaKe"), false)
295 assert_equals(el.hasAttribute("cheesecake"), false)
296 assert_equals(el.hasAttributeNS("", "CHEESECAKE"), false)
297 assert_equals(el.hasAttributeNS("", "CHEEseCaKe"), true)
298 assert_equals(el.hasAttributeNS("", "cheesecake"), false)
299 assert_equals(el.hasAttributeNS(null, "CHEESECAKE"), false)
300 assert_equals(el.hasAttributeNS(null, "CHEEseCaKe"), true)
301 assert_equals(el.hasAttributeNS(null, "cheesecake"), false)
302 assert_equals(el.getAttribute("CHEESECAKE"), null)
303 assert_equals(el.getAttribute("CHEEseCaKe"), null)
304 assert_equals(el.getAttribute("cheesecake"), null)
305 assert_equals(el.getAttributeNS(null, "CHEESECAKE"), null)
306 assert_equals(el.getAttributeNS("", "CHEESECAKE"), null)
307 assert_equals(el.getAttributeNS(null, "CHEEseCaKe"), "tasty")
308 assert_equals(el.getAttributeNS("", "CHEEseCaKe"), "tasty")
309 assert_equals(el.getAttributeNS(null, "cheesecake"), null)
310 assert_equals(el.getAttributeNS("", "cheesecake"), null)
311 el.removeAttributeNS("", "CHEEseCaKe")
312 }, "Only lowercase attributes are returned on HTML elements (mixed case attribut e)")
313 test(function() {
314 var el = document.createElement("div")
315 document.body.appendChild(el)
316 el.setAttributeNS("", "align", "left")
317 el.setAttributeNS("xx", "align", "right")
318 el.setAttributeNS("", "foo", "left")
319 el.setAttributeNS("xx", "foo", "right")
320 assert_equals(el.hasAttribute("align"), true)
321 assert_equals(el.hasAttribute("foo"), true)
322 assert_equals(el.hasAttributeNS("xx", "align"), true)
323 assert_equals(el.hasAttributeNS(null, "foo"), true)
324 assert_equals(el.getAttribute("align"), "left")
325 assert_equals(el.getAttribute("foo"), "left")
326 assert_equals(el.getAttributeNS("xx", "align"), "right")
327 assert_equals(el.getAttributeNS(null, "foo"), "left")
328 assert_equals(el.getAttributeNS("", "foo"), "left")
329 el.removeAttributeNS("", "align")
330 el.removeAttributeNS("xx", "align")
331 el.removeAttributeNS("", "foo")
332 el.removeAttributeNS("xx", "foo")
333 document.body.removeChild(el)
334 }, "First set attribute is returned with mapped attribute set first")
335 test(function() {
336 var el = document.createElement("div")
337 el.setAttributeNS("xx", "align", "right")
338 el.setAttributeNS("", "align", "left")
339 el.setAttributeNS("xx", "foo", "right")
340 el.setAttributeNS("", "foo", "left")
341 assert_equals(el.hasAttribute("align"), true)
342 assert_equals(el.hasAttribute("foo"), true)
343 assert_equals(el.hasAttributeNS("xx", "align"), true)
344 assert_equals(el.hasAttributeNS(null, "foo"), true)
345 assert_equals(el.getAttribute("align"), "right")
346 assert_equals(el.getAttribute("foo"), "right")
347 assert_equals(el.getAttributeNS("xx", "align"), "right")
348 assert_equals(el.getAttributeNS(null, "foo"), "left")
349 assert_equals(el.getAttributeNS("", "foo"), "left")
350 el.removeAttributeNS("", "align")
351 el.removeAttributeNS("xx", "align")
352 el.removeAttributeNS("", "foo")
353 el.removeAttributeNS("xx", "foo")
354 }, "First set attribute is returned with mapped attribute set later")
355
356 test(function() {
357 var el = document.createElementNS("http://www.example.com", "foo")
358 el.setAttribute("A", "test")
359 assert_equals(el.hasAttribute("A"), true, "hasAttribute()")
360 assert_equals(el.hasAttributeNS("", "A"), true, "el.hasAttributeNS(\"\")")
361 assert_equals(el.hasAttributeNS(null, "A"), true, "el.hasAttributeNS(null)")
362 assert_equals(el.hasAttributeNS(undefined, "A"), true, "el.hasAttributeNS(unde fined)")
363 assert_equals(el.hasAttributeNS("foo", "A"), false, "el.hasAttributeNS(\"foo\" )")
364
365 assert_equals(el.getAttribute("A"), "test", "getAttribute()")
366 assert_equals(el.getAttributeNS("", "A"), "test", "el.getAttributeNS(\"\")")
367 assert_equals(el.getAttributeNS(null, "A"), "test", "el.getAttributeNS(null)")
368 assert_equals(el.getAttributeNS(undefined, "A"), "test", "el.getAttributeNS(un defined)")
369 assert_equals(el.getAttributeNS("foo", "A"), null, "el.getAttributeNS(\"foo\") ")
370 }, "Non-HTML element with upper-case attribute")
371
372 test(function() {
373 var el = document.createElement("div")
374 el.setAttribute("pre:fix", "value 1")
375 el.setAttribute("fix", "value 2")
376
377 var prefixed = el.attributes[0]
378 assert_equals(prefixed.localName, "pre:fix", "prefixed local name")
379 assert_equals(prefixed.namespaceURI, null, "prefixed namespace")
380
381 var unprefixed = el.attributes[1]
382 assert_equals(unprefixed.localName, "fix", "unprefixed local name")
383 assert_equals(unprefixed.namespaceURI, null, "unprefixed namespace")
384
385 el.removeAttributeNS(null, "pre:fix")
386 assert_equals(el.attributes[0], unprefixed)
387 }, "Attribute with prefix in local name")
388
389 test(function() {
390 var el = document.createElement("div")
391 el.setAttribute("foo", "bar")
392 var attr = el.attributes[0]
393 assert_equals(attr.ownerElement, el)
394 el.removeAttribute("foo")
395 assert_equals(attr.ownerElement, null)
396 }, "Attribute loses its owner when removed")
397
398 test(function() {
399 var el = document.createElement("div")
400 el.setAttribute("foo", "bar")
401 var attr = el.attributes[0]
402 var attrNode = el.getAttributeNode("foo");
403 var attrNodeNS = el.getAttributeNodeNS("", "foo");
404 assert_equals(attr, attrNode);
405 assert_equals(attr, attrNodeNS);
406 el.setAttributeNS("x", "foo2", "bar2");
407 var attr2 = el.attributes[1];
408 var attrNodeNS2 = el.getAttributeNodeNS("x", "foo2");
409 assert_equals(attr2, attrNodeNS2);
410 }, "Basic functionality of getAttributeNode/getAttributeNodeNS")
411
412 test(function() {
413 var el = document.createElement("div")
414 el.setAttribute("foo", "bar")
415 var attrNode = el.getAttributeNode("foo");
416 var attrNodeNS = el.getAttributeNodeNS("", "foo");
417 assert_equals(attrNode, attrNodeNS);
418 el.removeAttribute("foo");
419 var el2 = document.createElement("div");
420 el2.setAttributeNode(attrNode);
421 assert_equals(attrNode, el2.getAttributeNode("foo"));
422 assert_equals(attrNode, el2.attributes[0]);
423 assert_equals(attrNode.ownerElement, el2);
424 assert_equals(attrNode.value, "bar");
425
426 var el3 = document.createElement("div");
427 el2.removeAttribute("foo");
428 el3.setAttribute("foo", "baz");
429 el3.setAttributeNode(attrNode);
430 assert_equals(el3.getAttribute("foo"), "bar");
431 }, "Basic functionality of setAttributeNode")
432
433 test(function() {
434 var el = document.createElement("div")
435 el.setAttributeNS("x", "foo", "bar")
436 var attrNode = el.getAttributeNodeNS("x", "foo");
437 el.removeAttribute("foo");
438 var el2 = document.createElement("div");
439 el2.setAttributeNS("x", "foo", "baz");
440 el2.setAttributeNodeNS(attrNode);
441 assert_equals(el2.getAttributeNS("x", "foo"), "bar");
442 }, "Basic functionality of setAttributeNodeNS")
443
444 test(function() {
445 var el = document.createElement("div")
446 el.setAttribute("foo", "bar")
447 var attrNode = el.getAttributeNode("foo");
448 el.removeAttributeNode(attrNode);
449 var el2 = document.createElement("div");
450 el2.setAttributeNode(attrNode);
451 assert_equals(el2.attributes[0], attrNode);
452 assert_equals(el.attributes.length, 0);
453 }, "Basic functionality of removeAttributeNode")
454
455 test(function() {
456 var el = document.createElement("div")
457 el.setAttribute("foo", "bar")
458 var attrNode = el.getAttributeNode("foo");
459 var el2 = document.createElement("div");
460 assert_throws("INUSE_ATTRIBUTE_ERR", function(){el2.setAttributeNode(attrNode) });
461 }, "setAttributeNode on bound attribute should throw InUseAttributeError")
462 </script>
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698