common-HTMLOptionsCollection.html (4231B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title id='title'>HTMLOptionsCollection</title> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 <div id="log"></div> 9 <select id="selly"> 10 <option>1</option> 11 <option>2</option> 12 <option>3</option> 13 <option>4</option> 14 </select> 15 16 <script> 17 var selly; 18 setup(function() { 19 selly = document.getElementById('selly'); 20 }); 21 22 test(function () { 23 assert_equals(selly.length, 4); 24 }, "On getting, the length attribute must return the number of nodes represented by the collection."); 25 26 test(function () { 27 selly.length = 7; 28 assert_equals(selly.length, 7, 29 "Number of nodes in collection should have changed"); 30 assert_equals(selly.children.length, 7, 31 "Number of children should have changed"); 32 for (var i = 4; i < 7; ++i) { 33 var child = selly.children[i]; 34 assert_equals(child.localName, "option", 35 "new child should be an option"); 36 assert_equals(child.namespaceURI, "http://www.w3.org/1999/xhtml", 37 "new child should be an HTML element"); 38 assert_equals(child.attributes.length, 0, 39 "new child should not have attributes"); 40 assert_equals(child.childNodes.length, 0, 41 "new child should not have child nodes"); 42 } 43 }, "Changing the length adds new nodes; The number of new nodes = new length minus old length"); 44 45 test(function () { 46 var elarray = []; 47 for (var i = 0; i < selly.length; i++) { 48 elarray.push(selly[i].value); 49 } 50 assert_array_equals(elarray, ["1", "2", "3", "4", "", "", ""]); 51 }, "New nodes have no value"); 52 53 test(function () { 54 selly.length = 7; 55 assert_equals(selly.length, 7, 56 "Number of nodes in collection should not have changed"); 57 assert_equals(selly.children.length, 7, 58 "Number of children should not have changed"); 59 }, "Setting a length equal to existing length changes nothing"); 60 61 test(function () { 62 selly.length = 4; 63 assert_equals(selly[6], undefined, 64 "previously set node is now undefined"); 65 assert_equals(selly.length, 4, 66 "Number of nodes in collection is correctly changed"); 67 assert_equals(selly.children.length, 4, 68 "Number of children should have changed"); 69 }, "Setting a length lower than the old length trims nodes from the end"); 70 71 test(function () { 72 var opts = selly.options; 73 opts[3] = null; 74 assert_equals(selly[3], undefined, 75 "previously set node is now undefined"); 76 assert_equals(selly.length, 3, 77 "Number of nodes in collection is correctly changed"); 78 assert_equals(selly.children.length, 3, 79 "Number of children should have changed"); 80 }, "Setting element to null by index removed the element"); 81 82 test(function () { 83 var opts = selly.options; 84 var new_option = document.createElement("option"); 85 var replace_option = new_option.cloneNode(true); 86 new_option.value = "-1"; 87 replace_option.value = "a"; 88 opts[5] = new_option; 89 opts[0] = replace_option; 90 91 var elarray = []; 92 for (var i = 0; i < selly.length; i++) { 93 elarray.push(selly[i].value); 94 } 95 assert_array_equals(elarray, ["a", "2", "3", "", "", "-1"]); 96 97 }, "Setting element by index should correctly append and replace elements"); 98 99 test(function () { 100 var selection = document.createElementNS("http://www.w3.org/1999/xhtml", "foo:select"); 101 102 selection.length = 5; 103 assert_equals(selection.length, 5, 104 "Number of nodes in collection should have changed"); 105 for (var i = 0; i < 5; ++i) { 106 var child = selection.children[i]; 107 assert_equals(child.localName, "option", 108 "new child should be an option"); 109 assert_equals(child.namespaceURI, "http://www.w3.org/1999/xhtml", 110 "new child should be an HTML element"); 111 assert_equals(child.prefix, null, 112 "new child should not copy select's prefix"); 113 } 114 115 }, "Changing the length adds new nodes; The new nodes will not copy select's prefix"); 116 117 </script>