summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/html/semantics/forms/form-control-infrastructure/form_attribute.html
blob: dde3250dbf7c10a767e91c28c3b42078533e8748 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<!DOCTYPE html>
<html>
  <head>
    <script src="/resources/testharness.js"></script>
    <script src="/resources/testharnessreport.js"></script>
  </head>
  <body>
    <div>
      <form id="f1"></form>
      <form id="f2">
        <input id="i1" />
        <input id="i2" form="f1" />
        <input id="i3" />
      </form>
      <script>
        test(function() {
          var i1 = document.getElementById("i1");
          var i2 = document.getElementById("i2");
          var i3 = document.getElementById("i3");
          var f1 = document.getElementById("f1");
          var f2 = document.getElementById("f2");

          assert_equals(i1.form, f2,
            "i1 must be associated with f2 by the parser");
          assert_equals(i2.form, f1,
            "i2 is not associated with f2 by the parser " +
            "since it has the form attribute set to f1");

          f1.appendChild(i1);
          i3.setAttribute("form", "f1");

          assert_equals(i1.form, f1,
            "i1's form owner must be reset when parent changes");
          assert_equals(i3.form, f1,
            "i3's form owner must be reset when the form" +
            "attribute is set");

          assert_equals(i2.form, f1);
        }, "Tests for parser inserted controls");
      </script>
    </div>

    <div id="placeholder">
    </div>

    <script>
      var reassociateableElements = [
        "button",
        "fieldset",
        "input",
        "object",
        "output",
        "select",
        "textarea",
      ];

      var form1 = null;
      var form2 = null;
      var placeholder = document.getElementById("placeholder");

      reassociateableElements.forEach(function(localName) {
        function testControl(test_, desc) {
          test(function() {
            var control = document.createElement(localName);

          while(placeholder.firstChild)
            placeholder.removeChild(placeholder.firstChild);

            form1 = document.createElement("form");
            form2 = document.createElement("form");
            form1.id = "form1";
            form2.id = "form2";
            placeholder.appendChild(form1);
            placeholder.appendChild(form2);

            test_.call(control);
          }, "[" + localName.toUpperCase() + "] " + desc);
        }

        testControl(function() {
          form1.appendChild(this);
          assert_equals(this.form, form1);
        }, "Basic form association - control with no form attribute is associated with ancestor");

        testControl(function() {
          this.setAttribute("form", "form1");
          form1.appendChild(this);
          assert_equals(this.form, form1);

          form1.id = "form-one";
          assert_equals(this.form, null);
        }, "Form owner is reset to null when control's form attribute is set to an ID " +
           "that does not exist in the document");

        testControl(function() {
          this.setAttribute("form", "");
          form1.appendChild(this);
          assert_equals(this.form, null);
        }, "Control whose form attribute is an empty string has no form owner");

        testControl(function() {
          form1.id = "";
          this.setAttribute("form", "");
          form1.appendChild(this);
          assert_equals(this.form, null);
        }, "Control whose form attribute is an empty string has no form owner " +
           "even when form with empty attribute is present");

        testControl(function() {
          form1.id = "FORM1";
          this.setAttribute("form", "form1");
          form1.appendChild(this);
          assert_equals(this.form, null);
        }, "Control's form attribute must be a case sensitive match for the form's id");

        testControl(function() {
          form1.appendChild(this);
          assert_equals(this.form, form1);

          this.setAttribute("form", "form2");
          assert_equals(this.form, form2);
        }, "Setting the form attribute of a control to the id of a non-ancestor form works");

        testControl(function() {
          this.setAttribute("form", "form1");

          form2.appendChild(this);
          assert_equals(this.form, form1);

          this.removeAttribute("form");
          assert_equals(this.form, form2);
        }, "Removing form id from a control resets the form owner to ancestor");

        testControl(function() {
          this.setAttribute("form", "form1");

          form2.appendChild(this);
          assert_equals(this.form, form1);

          placeholder.removeChild(form1);

          assert_equals(this.form, null);
        }, "Removing the form owner of a control with form attribute resets " +
           "the form owner to null");

        testControl(function() {
          var form3 = document.createElement("form");
          form3.id = "form3";
          placeholder.appendChild(form3);
          form3.appendChild(this);
          assert_equals(this.form, form3);

          this.setAttribute("form", "form2");
          assert_equals(this.form, form2);

          this.setAttribute("form", "form1");
          assert_equals(this.form, form1);
        }, "Changing form attibute of control resets form owner to correct form");

        testControl(function() {
          this.setAttribute("form", "form1");
          var form3 = document.createElement("form");
          form3.id = "form3";

          placeholder.appendChild(form3);
          placeholder.appendChild(this);
          assert_equals(this.form, form1);

          form1.appendChild(this);
          assert_equals(this.form, form1);

          form2.appendChild(this);
          assert_equals(this.form, form1);
        }, "Moving a control with form attribute within the document " +
           "does not change the form owner");

        testControl(function() {
          form1.id = "form-one";
          this.setAttribute("form", "form1");
          form2.appendChild(this);
          assert_equals(this.form, null);

          form1.id = "form1";
          assert_equals(this.form, form1);
        }, "When the id of a non-ancestor form changes from not being a match for the " +
           "form attribute to being a match, the control's form owner is reset");

        testControl(function() {
          this.setAttribute("form", "form1");
          form1.appendChild(this);
          assert_equals(this.form, form1);

          form2.id = "form1";
          form1.parentNode.insertBefore(form2, form1);
          assert_equals(this.form, form2);

          form2.parentNode.removeChild(form2);
          assert_equals(this.form, form1);

          form1.parentNode.appendChild(form2);
          assert_equals(this.form, form1);
        }, "When form element with same ID as the control's form attribute is inserted " +
           "earlier in tree order, the form owner is changed to the inserted form");

        testControl(function() {
          this.setAttribute("form", "form1");
          form2.appendChild(this);
          assert_equals(this.form, form1);

          var span = document.createElement("span");
          span.id = "form1";
          form1.parentNode.insertBefore(span, form1);
          assert_equals(this.form, null);

          form1.parentNode.appendChild(span);
          assert_equals(this.form, form1);
        }, "When non-form element with same ID as the control's form attribute is " +
           "inserted earlier in tree order, the control does not have a form owner");

        testControl(function() {
          this.setAttribute("form", "form1");
          form1.appendChild(this);
          assert_equals(this.form, form1);

          form1.parentNode.removeChild(form1);
          assert_equals(this.form, form1);
        }, "A control that is not in the document but has the form attribute set " +
           "is associated with the nearest ancestor form if one exists");

      });
    </script>
  </body>
</html>