Element-children.html (2055B)
1 <!DOCTYPE html> 2 <title>HTMLCollection edge cases</title> 3 <script src="/resources/testharness.js"></script> 4 <script src="/resources/testharnessreport.js"></script> 5 <div id="log"></div> 6 <div id="test"><img><img id=foo><img id=foo><img name="bar"></div> 7 <script> 8 setup(function() { 9 // Add some non-HTML elements in there to test what happens with those. 10 var container = document.getElementById("test"); 11 var child = document.createElementNS("", "img"); 12 child.setAttribute("id", "baz"); 13 container.appendChild(child); 14 15 child = document.createElementNS("", "img"); 16 child.setAttribute("name", "qux"); 17 container.appendChild(child); 18 }); 19 20 test(function() { 21 var container = document.getElementById("test"); 22 var result = container.children.item("foo"); 23 assert_true(result instanceof Element, "Expected an Element."); 24 assert_false(result.hasAttribute("id"), "Expected the IDless Element.") 25 }) 26 27 test(function() { 28 var container = document.getElementById("test"); 29 var list = container.children; 30 var result = []; 31 for (var p in list) { 32 if (list.hasOwnProperty(p)) { 33 result.push(p); 34 } 35 } 36 assert_array_equals(result, ['0', '1', '2', '3', '4', '5']); 37 result = Object.getOwnPropertyNames(list); 38 assert_array_equals(result, ['0', '1', '2', '3', '4', '5', 'foo', 'bar', 'baz']); 39 40 // Mapping of exposed names to their indices in the list. 41 var exposedNames = { 'foo': 1, 'bar': 3, 'baz': 4 }; 42 for (var exposedName in exposedNames) { 43 assert_true(exposedName in list); 44 assert_true(list.hasOwnProperty(exposedName)); 45 assert_equals(list[exposedName], list.namedItem(exposedName)); 46 assert_equals(list[exposedName], list.item(exposedNames[exposedName])); 47 assert_true(list[exposedName] instanceof Element); 48 } 49 50 var unexposedNames = ['qux']; 51 for (var unexposedName of unexposedNames) { 52 assert_false(unexposedName in list); 53 assert_false(list.hasOwnProperty(unexposedName)); 54 assert_equals(list[unexposedName], undefined); 55 assert_equals(list.namedItem(unexposedName), null); 56 } 57 }); 58 </script>