attributes-namednodemap.html (3908B)
1 <!DOCTYPE HTML> 2 <title>Tests of some tricky semantics around NamedNodeMap and the element.attributes collection</title> 3 <link rel="author" title="Domenic Denicola" href="mailto:d@domenic.me"> 4 <link rel="help" href="https://dom.spec.whatwg.org/#interface-namednodemap"> 5 <link rel="help" href="https://dom.spec.whatwg.org/#dom-element-attributes"> 6 7 <script src="/resources/testharness.js"></script> 8 <script src="/resources/testharnessreport.js"></script> 9 10 <script> 11 "use strict"; 12 13 test(() => { 14 15 const element = document.createElement("div"); 16 element.setAttribute("x", "first"); 17 18 assert_equals(element.attributes.length, 1, "one attribute"); 19 assert_equals(element.attributes.x.value, "first"); 20 21 }, "an attribute set by setAttribute should be accessible as a field on the `attributes` field of an Element"); 22 23 test(() => { 24 25 const element = document.createElement("div"); 26 const map = element.attributes; 27 28 assert_equals(map.length, 0); 29 30 const attr1 = document.createAttribute("attr1"); 31 map.setNamedItem(attr1); 32 assert_equals(map.attr1, attr1); 33 assert_equals(map.length, 1); 34 35 const attr2 = document.createAttribute("attr2"); 36 map.setNamedItem(attr2); 37 assert_equals(map.attr2, attr2); 38 assert_equals(map.length, 2); 39 40 const rm1 = map.removeNamedItem("attr1"); 41 assert_equals(rm1, attr1); 42 assert_equals(map.length, 1); 43 44 const rm2 = map.removeNamedItem("attr2"); 45 assert_equals(rm2, attr2); 46 assert_equals(map.length, 0); 47 48 }, "setNamedItem and removeNamedItem on `attributes` should add and remove fields from `attributes`"); 49 50 test(() => { 51 52 const element = document.createElement("div"); 53 const map = element.attributes; 54 55 const fooAttribute = document.createAttribute("foo"); 56 map.setNamedItem(fooAttribute); 57 58 const itemAttribute = document.createAttribute("item"); 59 map.setNamedItem(itemAttribute); 60 61 assert_equals(map.foo, fooAttribute); 62 assert_equals(map.item, NamedNodeMap.prototype.item); 63 assert_equals(typeof map.item, "function"); 64 65 map.removeNamedItem("item"); 66 assert_equals(map.item, NamedNodeMap.prototype.item); 67 assert_equals(typeof map.item, "function"); 68 69 }, "setNamedItem and removeNamedItem on `attributes` should not interfere with existing method names"); 70 71 test(() => { 72 73 const element = document.createElement("div"); 74 element.setAttributeNS(null, "x", "first"); 75 76 assert_equals(element.attributes.length, 1, "one attribute"); 77 assert_equals(element.attributes.x.value, "first"); 78 79 }, "an attribute with a null namespace should be accessible as a field on the `attributes` field of an Element"); 80 81 test(() => { 82 83 const element = document.createElement("div"); 84 element.setAttributeNS("foo", "x", "first"); 85 86 assert_equals(element.attributes.length, 1, "one attribute"); 87 assert_equals(element.attributes.x.value, "first"); 88 89 }, "an attribute with a set namespace should be accessible as a field on the `attributes` field of an Element"); 90 91 test(() => { 92 93 const element = document.createElement("div"); 94 element.setAttributeNS("foo", "setNamedItem", "first"); 95 96 assert_equals(element.attributes.length, 1, "one attribute"); 97 assert_equals(typeof element.attributes.setNamedItem, "function"); 98 99 }, "setting an attribute should not overwrite the methods of an `NamedNodeMap` object"); 100 101 test(() => { 102 103 const element = document.createElement("div"); 104 element.setAttributeNS("foo", "toString", "first"); 105 106 assert_equals(element.attributes.length, 1, "one attribute"); 107 assert_equals(typeof element.attributes.toString, "function"); 108 109 }, "setting an attribute should not overwrite the methods defined by prototype ancestors of an `NamedNodeMap` object"); 110 111 test(() => { 112 113 const element = document.createElement("div"); 114 element.setAttributeNS("foo", "length", "first"); 115 116 assert_equals(element.attributes.length, 1, "one attribute"); 117 118 }, "setting an attribute should not overwrite the length property of an `NamedNodeMap` object"); 119 120 </script>