htmlformcontrolscollection.html (5013B)
1 <!DOCTYPE html> 2 <meta charset="utf-8"> 3 <title>HTML Test: the HTMLFormControlsCollection interface</title> 4 <link rel="author" title="Intel" href="http://www.intel.com/"> 5 <link rel="help" href="https://html.spec.whatwg.org/multipage/multipage/common-dom-interfaces.html#htmlformcontrolscollection"> 6 <script src="/resources/testharness.js"></script> 7 <script src="/resources/testharnessreport.js"></script> 8 <div id="log"></div> 9 <form id="f1"> 10 <input type="radio" id="r1" name="ra"> 11 <keygen id="kg" name="key"></keygen> <!-- we test that it does *not* appear in form.elements --> 12 </form> 13 <form id="f2"> 14 <table> 15 <tr> 16 <td> 17 <input type="checkbox" id="cb"> 18 <input type="checkbox" name="cb"> 19 </td> 20 </tr> 21 <tr> 22 <td> 23 <button id="btn"></button> 24 <button name="btn"></button> 25 </td> 26 </tr> 27 </table> 28 </form> 29 30 <script> 31 32 var coll1, coll2, rdo; 33 34 setup(function () { 35 rdo = document.getElementById("r1"); 36 coll1 = document.forms[0].elements; 37 coll2 = document.forms[1].elements; 38 }); 39 40 //length 41 test(function () { 42 assert_equals(coll1.length, 1, "The length attribute is incorrect."); 43 assert_equals(coll2.length, 4, "The length attribute is incorrect."); 44 }, "The length attribute must return the number of elements in the form"); 45 46 //getter - index 47 test(function () { 48 assert_equals(coll1.item(0), rdo, "HTMLFormControlsCollection.item(index) should return the 'input' element in radio status."); 49 }, "HTMLFormControlsCollection.item(index) must return the indexed item"); 50 51 test(function () { 52 assert_equals(coll1[0], rdo, "HTMLFormControlsCollection[index] should return the 'input' element in radio status."); 53 }, "HTMLFormControlsCollection[index] must return the indexed item"); 54 55 //getter - name 56 test(function () { 57 assert_throws_js(TypeError, function() { coll1("r1") }); 58 }, "HTMLFormControlsCollection is not callable"); 59 60 test(function () { 61 assert_equals(coll1["r1"], rdo, "HTMLFormControlsCollection[name] should return the 'input' element in radio status."); 62 }, "HTMLFormControlsCollection[name] must return the named item"); 63 64 //getter - namedItem 65 test(function () { 66 assert_equals(coll1.namedItem("r1"), rdo, "HTMLFormControlsCollection.namedItem(name) should return the 'input' element in radio status."); 67 }, "HTMLFormControlsCollection.namedItem(name) must return the named item"); 68 69 test(function () { 70 assert_true(coll1.namedItem("r1") instanceof Element, "Can not return 'Element' object."); 71 }, "The namedItem(name) must return an Element"); 72 73 test(function () { 74 assert_true(coll2.namedItem("cb") instanceof RadioNodeList, "Can not return 'RadioNodeList' object."); 75 }, "The namedItem(name) must return RadioNodeList"); 76 77 test(function () { 78 assert_equals(coll1.namedItem(""), null, "The return value of namedItem() should be null."); 79 }, "The namedItem(name) must return null if the name is empty"); 80 81 test(function () { 82 assert_equals(coll1.namedItem("test"), null, "The return value of namedItem() should be null."); 83 }, "The namedItem(name) must return null if there is no matched element"); 84 85 test(function () { 86 assert_equals(coll1.namedItem("r1"), document.getElementById("r1"), "Controls can be named by 'id' attribute."); 87 assert_equals(coll1.namedItem("ra"), document.getElementById("r1"), "Controls can be named by 'name' attribute."); 88 }, "Controls can be indexed by id or name attribute"); 89 90 test(function () { 91 assert_equals(coll1.namedItem("kg"), null, "Keygen does not show up when queried by id."); 92 assert_equals(coll1.namedItem("key"), null, "Keygen does not show up when queried by name."); 93 }, "Keygen controls do not show up at all"); 94 95 test(function () { 96 assert_equals(coll2.namedItem("btn").length, 2, "The length attribute should be 2."); 97 }, "The namedItem(name) must return the items with id or name attribute"); 98 99 //various controls in fieldset and form 100 var containers = ["form", "fieldset"], 101 controls = ["button", "fieldset", "input", "object", "output", "select", "textarea"]; 102 for (var m = 0; m < containers.length; m++) { 103 test(function () { 104 var container = document.createElement(containers[m]); 105 var len = controls.length; 106 for (var n = 0; n < len; n++) 107 container.appendChild(document.createElement(controls[n])); 108 document.body.appendChild(container); 109 assert_equals(container.elements.length, len, "The length should be " + len + "."); 110 }, "The HTMLFormControlsCollection interface is used for collections of listed elements in " + containers[m] + " element"); 111 } 112 113 //Check the controls' order 114 test(function () { 115 var opt = document.forms[1].insertBefore(document.createElement("output"), document.forms[1].firstChild); 116 assert_array_equals(document.forms[1].elements, 117 [opt, document.getElementsByTagName("input")[1], document.getElementsByTagName("input")[2], 118 document.getElementsByTagName("button")[0], document.getElementsByTagName("button")[1]]); 119 }, "The controls in the form element must be sorted in tree order"); 120 121 </script>