document.forms.html (2901B)
1 <!doctype html> 2 <meta charset=utf-8> 3 <title>Document.forms</title> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <div id=log></div> 7 <form id="form1"> 8 <input type="button" name="thebutton" value="alpha"> 9 <input type=radio name=r1 value=a> 10 <input type=radio name=r1 value=b> 11 <input type=radio name=r1 value=c> 12 <input type=radio name=r1 value=d> 13 <input type=radio name=r1 value=e> 14 </form> 15 16 <form id="form2"> 17 <input type="button" name="thebutton" value="alpha"> 18 <input type=radio name=r1 value="a"> 19 <input type=radio name=r1 value="b"> 20 <input type=radio name=r1 value="c"> 21 <input type=radio name=r1 value="d"> 22 <input type=radio name=r1 value="e"> 23 </form> 24 25 <form id=""></form> 26 <script> 27 test(function() { 28 assert_equals(document.forms.length, 3); 29 assert_equals(document.forms[0].id, "form1"); 30 assert_equals(document.forms[1].id, "form2"); 31 assert_equals(document.forms.form1.id, "form1"); 32 assert_equals(document.forms.form2.id, "form2"); 33 assert_equals(document.forms.item(0).id, "form1"); 34 assert_equals(document.forms.item(1).id, "form2"); 35 assert_equals(document.forms.namedItem("form1").id, "form1"); 36 assert_equals(document.forms.namedItem("form2").id, "form2"); 37 }, "document.forms") 38 39 test(function() { 40 // The `item` method takes one *numeric* argument. Passing a string to `item` 41 // results in that string getting converted to 0 42 assert_equals(document.forms.item("form1").id, "form1"); 43 assert_equals(document.forms.item("form2").id, "form1"); 44 }, "document.forms.item with string arg") 45 46 test(function() { 47 assert_equals(document.forms[""], undefined); 48 assert_equals(document.forms.namedItem(""), null); 49 }, "document.forms with empty string") 50 51 test(function() { 52 var result = []; 53 for (var p in document.forms) { 54 result.push(p); 55 } 56 // https://webidl.spec.whatwg.org/#property-enumeration 57 // If the object supports indexed properties, then the object’s supported 58 // property indices are enumerated first, in numerical order. 59 assert_array_equals(result.splice(0, 3), ["0", "1", "2"]); 60 // [...] 61 // Finally, any enumerable own properties or properties from the object’s 62 // prototype chain are then enumerated, in no defined order. 63 assert_array_equals(result.sort(), ["item", "namedItem", "length"].sort()) 64 }, "document.forms iteration") 65 66 test(function() { 67 var result = Object.getOwnPropertyNames(document.forms); 68 assert_array_equals(result, ["0", "1", "2", "form1", "form2"]) 69 }, "document.forms getOwnPropertyNames") 70 71 test(function() { 72 var forms = document.forms; 73 assert_true(forms instanceof HTMLCollection); 74 assert_equals(forms.length, 3); 75 76 var form = document.createElement("form"); 77 document.body.appendChild(form); 78 assert_equals(forms.length, 4); 79 80 document.body.removeChild(form); 81 assert_equals(forms.length, 3); 82 }, "Document.forms should be a live collection"); 83 </script>