Skip to content

Commit eadb5b6

Browse files
committed
Use css.querySelectorAll to find form elements
Libdom's formGetCollection doesn't work (like I would expect) for dynamically added elements. For example, given: ``` let el = document.createElement('input'); document.getElementsByTagName('form')[0].append(el); ``` (and assume the page has a form), I'd expect `el.form` to be equal to the form the input was added to. Instead, it's null. This is a problem given that `dom_html_form_element_get_elements` uses the element's `form` attribute to "collect" the elements. This uses our existing querySelector to find the form elements.
1 parent faebabe commit eadb5b6

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/browser/xhr/form_data.zig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,16 @@ const EntryIterable = iterator.Iterable(kv.EntryIterator, "FormDataEntryIterator
115115
// TODO: handle disabled fieldsets
116116
fn collectForm(form: *parser.Form, submitter_: ?*parser.ElementHTML, page: *Page) !kv.List {
117117
const arena = page.arena;
118-
const collection = try parser.formGetCollection(form);
119-
const len = try parser.htmlCollectionGetLength(collection);
118+
const node_list = try @import("../dom/css.zig").querySelectorAll(arena, @alignCast(@ptrCast(form)), "input,select,button,textarea");
119+
const nodes = node_list.nodes.items;
120120

121121
var entries: kv.List = .{};
122-
try entries.ensureTotalCapacity(arena, len);
122+
try entries.ensureTotalCapacity(arena, nodes.len);
123123

124124
var submitter_included = false;
125125
const submitter_name_ = try getSubmitterName(submitter_);
126126

127-
for (0..len) |i| {
128-
const node = try parser.htmlCollectionItem(collection, @intCast(i));
127+
for (nodes) |node| {
129128
const element = parser.nodeToElement(node);
130129

131130
// must have a name
@@ -181,10 +180,7 @@ fn collectForm(form: *parser.Form, submitter_: ?*parser.ElementHTML, page: *Page
181180
submitter_included = true;
182181
}
183182
},
184-
else => {
185-
log.warn(.web_api, "unsupported form element", .{ .tag = @tagName(tag) });
186-
continue;
187-
},
183+
else => unreachable,
188184
}
189185
}
190186

@@ -297,6 +293,7 @@ test "Browser.FormData" {
297293
\\ <input type=submit name=s2 value=s2-v>
298294
\\ <input type=image name=i1 value=i1-v>
299295
\\ </form>
296+
\\ <input type=text name=abc value=123 form=form1>
300297
});
301298
defer runner.deinit();
302299

@@ -356,6 +353,8 @@ test "Browser.FormData" {
356353

357354
try runner.testCases(&.{
358355
.{ "let form1 = document.getElementById('form1')", null },
356+
.{ "let input = document.createElement('input');", null },
357+
.{ "input.name = 'dyn'; input.value= 'dyn-v'; form1.appendChild(input);", null },
359358
.{ "let submit1 = document.getElementById('s1')", null },
360359
.{ "let f2 = new FormData(form1, submit1)", null },
361360
.{ "acc = '';", null },
@@ -378,6 +377,7 @@ test "Browser.FormData" {
378377
\\mlt-2=water
379378
\\mlt-2=tea
380379
\\s1=s1-v
380+
\\dyn=dyn-v
381381
},
382382
}, .{});
383383
}

0 commit comments

Comments
 (0)