3 <script src="../../resources/js-test.js"></script>
7 description("This tests accessing form elements by name. "
8 + "IE only lets you look up names under the first name the element had and "
9 + "does not respond to name changes. Firefox remembers every name item has been "
10 + "accessed with, but forgets items that have not been accessed. "
11 + "This test has been written to expect the Firefox behavior.");
13 form = document.getElementById('form');
14 a = document.getElementById('a');
15 b = document.getElementById('b');
17 shouldBe('form.length', '2');
18 shouldBe('form.original', 'a');
19 shouldBe('form.originalB', 'b');
20 shouldBe('form.second', 'undefined');
21 shouldBe('form.third', 'undefined');
22 shouldBe('form.elements.original', 'a');
23 shouldBe('form.elements.originalB', 'b');
24 shouldBe('form.elements.second', 'undefined');
25 shouldBe('form.elements.third', 'undefined');
28 debug("change the form item a's name to thisWillBeRemembered");
30 a.name="thisWillBeRemembered";
31 debug("get the variable value through form element");
32 shouldBe('form.thisWillBeRemembered', 'a');
34 debug("now change the form item a's name to thisWillBeRememberedToo");
35 debug("access it in boolean context");
36 a.name="thisWillBeRememberedToo";
38 if (form.thisWillBeRememberedToo)
39 debug('accessed form.thisWillBeRememberedToo');
41 debug("now change the form item a's name to thisWillBeForgotten");
43 a.name="thisWillBeForgotten";
44 debug("get the variable value through collection");
45 shouldBe('form.elements.thisWillBeForgotten', 'a');
47 debug("now change the form item a's name to thisWillBeForgottenToo, but don't access it afterwards");
48 a.name="thisWillBeForgottenToo";
51 debug("now change the form item a's name to second");
55 shouldBe('form.length', '2');
56 shouldBe('form.original', 'a');
57 shouldBe('form.originalB', 'b');
58 shouldBe('form.second', 'a');
59 shouldBe('form.third', 'undefined');
60 shouldBe('form.elements.original', 'undefined');
61 shouldBe('form.elements.originalB', 'b');
62 shouldBe('form.elements.second', 'a');
63 shouldBe('form.elements.third', 'undefined');
66 debug("now change the form item a's name to third");
71 shouldBe('form.length', '2');
72 shouldBe('form.original', 'a');
73 shouldBe('form.originalB', 'b');
74 shouldBe('form.second', 'a');
75 shouldBe('form.third', 'a');
76 shouldBe('form.elements.original', 'undefined');
77 shouldBe('form.elements.originalB', 'b');
78 shouldBe('form.elements.second', 'undefined');
79 shouldBe('form.elements.third', 'a');
82 debug("now change form item b's name to second");
87 shouldBe('form.length', '2');
88 shouldBe('form.original', 'a');
89 shouldBe('form.originalB', 'b');
90 shouldBe('form.second', 'b');
91 shouldBe('form.elements.original', 'undefined');
92 shouldBe('form.elements.originalB', 'undefined');
93 shouldBe('form.elements.second', 'b');
96 debug("now change a form item b's name to third");
99 form.originalB.name="third";
101 shouldBe('form.length', '2');
102 shouldBe('form.original', 'a');
103 shouldBe('form.originalB', 'b');
104 shouldBe('form.second', 'b');
105 shouldBe('form.third.length', '2');
106 shouldBe('form.third[0]', 'a');
107 shouldBe('form.third[1]', 'b');
108 shouldBe('form.elements.original', 'undefined');
109 shouldBe('form.elements.originalB', 'undefined');
110 shouldBe('form.elements.second', 'undefined');
111 shouldBe('form.elements.third.length', '2');
112 shouldBe('form.elements.third[0]', 'a');
113 shouldBe('form.elements.third[1]', 'b');
116 debug("now change a form item b's name to fourth");
119 form.originalB.name="fourth";
121 shouldBe('form.third', 'a');
122 shouldBe('form.third.length', 'undefined');
123 shouldBe('form.elements.third', 'a');
124 shouldBe('form.elements.third.length', 'undefined');
127 debug("now remove element a");
132 shouldBe('form.length', '1');
133 shouldBeUndefined('form.original', 'a');
134 shouldBe('form.originalB', 'b');
135 shouldBe('form.second', 'b');
136 shouldBeUndefined('form.third', 'a');
137 shouldBe('form.fourth', 'b');
138 shouldBe('form.elements.original', 'undefined');
139 shouldBe('form.elements.originalB', 'undefined');
140 shouldBe('form.elements.second', 'undefined');
141 shouldBe('form.elements.third', 'undefined');
142 shouldBe('form.elements.fourth', 'b');
145 debug("check that we no longer remember the past names of a");
148 shouldBe('form.thisWillBeForgotten', 'undefined');
149 shouldBe('form.thisWillBeForgottenToo', 'undefined');
150 shouldBeUndefined('form.thisWillBeRemembered');
151 shouldBeUndefined('form.thisWillBeRememberedToo');
158 <input type='hidden' id='a' name='original'>
159 <input type='hidden' id='b' name='originalB'>
162 <form id="form1"><input name="foo" id="input3"></form>
163 <form id="form2"></form>
165 <form id="form4"><input name="foo"><input name="bar"><input name="bar"></form>
167 <p id="description"></p>
168 <div id="console"></div>
172 debug('Ensures elements are removed from the past names map of a form element once they are no longer associated with the form element.');
173 var form1 = document.querySelector("#form1");
174 var input3 = document.querySelector("#input3");
175 shouldBe("form1['foo']", "input3");
176 shouldBeUndefined("form2.appendChild(form1.firstChild); form1['foo']");
178 shouldBe("form2['foo']", "input3");
179 shouldBeUndefined("form2.removeChild(input3); form2['foo']");
181 shouldBe("form1.appendChild(input3); form1['foo']", "input3");
182 // input3 will be an orphan. So form content attribute doesn't work.
183 shouldBeUndefined("input3.setAttribute('form', 'form1'); form1.removeChild(input3); input3.appendChild(form1); form1['foo']");
184 shouldNotBe("form1['foo']", "input3");
187 debug("Don't add the element from the past names map if we've found elements of the given name.");
188 var form4 = document.querySelector("#form4");
189 shouldBe("form4['foo']", "form4.childNodes[0]");
190 shouldBe("form4['bar'][0]", "form4.childNodes[1]");
191 shouldBe("form4['bar'][1]", "form4.childNodes[2]");
192 shouldBe("form4.childNodes[0].name = 'bar'; form4.childNodes[1].name = form4.childNodes[2].name = 'foo'; form4['foo'].length", "2");