document.images.html (3342B)
1 <!doctype html> 2 <meta charset=utf-8> 3 <title>Document.images</title> 4 <script src=/resources/testharness.js></script> 5 <script src=/resources/testharnessreport.js></script> 6 <div id=log></div> 7 <div id=test> 8 <img> 9 <img id=x><img name=y><img id=z1 name=z2> 10 <img id=a><img id=a> 11 <img name=b><img name=b> 12 <img id=><img name=> 13 <input type=image name=input> 14 </div> 15 <script> 16 function assert_all(aAssertFunc, aCollection) { 17 for (var i = 0; i < aCollection.length; ++i) { 18 aAssertFunc(aCollection[i]); 19 } 20 } 21 22 var XHTML = "http://www.w3.org/1999/xhtml"; 23 var div, images, c; 24 25 setup(function() { 26 div = document.getElementById("test"); 27 var foreign = 28 div.appendChild(document.createElementNS("http://example.org", "img")); 29 foreign.setAttribute("id", "f"); 30 31 images = [].slice.call(div.getElementsByTagNameNS(XHTML, "img")); 32 33 c = document.images; 34 }); 35 36 test(function() { 37 assert_equals(c.length, 10); 38 assert_array_equals(c, images); 39 40 assert_all(function (aElement) { 41 assert_equals(aElement.namespaceURI, XHTML); 42 }, c); 43 }, "document.images should contain all HTML img elements"); 44 45 test(function() { 46 assert_equals(c.x, images[1]); 47 assert_equals(c.namedItem("x"), images[1]); 48 assert_true("x" in c, '"x" in c'); 49 }, "img with id"); 50 51 test(function() { 52 assert_equals(c.y, images[2]); 53 assert_equals(c.namedItem("y"), images[2]); 54 assert_true("y" in c, '"y" in c'); 55 }, "img with name"); 56 57 test(function() { 58 assert_equals(c.z1, images[3]); 59 assert_equals(c.namedItem("z1"), images[3]); 60 assert_true("z1" in c, '"z1" in c'); 61 assert_equals(c.z2, images[3]); 62 assert_equals(c.namedItem("z2"), images[3]); 63 assert_true("z2" in c, '"z2" in c'); 64 }, "img with id and name"); 65 66 test(function() { 67 assert_equals(c.a, images[4]); 68 assert_equals(c.namedItem("a"), images[4]); 69 assert_true("a" in c, '"a" in c'); 70 }, "Two img elements with the same id"); 71 72 test(function() { 73 assert_equals(c.b, images[6]); 74 assert_equals(c.namedItem("b"), images[6]); 75 assert_true("b" in c, '"b" in c'); 76 }, "Two img elements with the same name"); 77 78 test(function() { 79 assert_equals(c.c, undefined); 80 assert_equals(c.namedItem("c"), null); 81 assert_false("c" in c, '"c" in c'); 82 }, "Unknown name should not be in the collection"); 83 84 test(function() { 85 assert_equals(c.f, undefined); 86 assert_equals(c.namedItem("f"), null); 87 assert_false("f" in c, '"f" in c'); 88 }, "Foreign element should not be in the collection"); 89 90 test(function() { 91 assert_equals(c.input, undefined); 92 assert_equals(c.namedItem("input"), null); 93 assert_false("input" in c, '"input" in c'); 94 var input = div.getElementsByTagName("input")[0]; 95 assert_all(function (aElement) { 96 assert_not_equals(aElement.namespaceURI, input); 97 }, c); 98 }, "Input elements should not be in the collection"); 99 100 test(function() { 101 assert_equals(c[""], undefined); 102 assert_equals(c.namedItem(""), null); 103 assert_false("" in c, '"" in c'); 104 }, "The empty string should not be in the collections"); 105 106 test(function() { 107 var div = document.getElementById("test"); 108 var imgs = document.images; 109 assert_true(imgs instanceof HTMLCollection); 110 assert_equals(imgs.length, 10); 111 112 var img = document.createElement("img"); 113 div.appendChild(img); 114 assert_equals(imgs.length, 11); 115 116 div.removeChild(img); 117 assert_equals(imgs.length, 10); 118 }, "Document.images should be a live collection"); 119 </script>