Skip to content

Commit af7e1d9

Browse files
authored
Fix form.elements to use form owner instead of only descendants
Closes #2628.
1 parent 3010c09 commit af7e1d9

File tree

3 files changed

+47
-6
lines changed

3 files changed

+47
-6
lines changed

lib/jsdom/living/nodes/HTMLFormElement-impl.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const { serializeURL } = require("whatwg-url");
55
const HTMLElementImpl = require("./HTMLElement-impl").implementation;
66
const { domSymbolTree } = require("../helpers/internal-constants");
77
const { fireAnEvent } = require("../helpers/events");
8-
const { isListed, isSubmittable, isSubmitButton } = require("../helpers/form-controls");
8+
const { formOwner, isListed, isSubmittable, isSubmitButton } = require("../helpers/form-controls");
99
const HTMLCollection = require("../generated/HTMLCollection");
1010
const notImplemented = require("../../browser/not-implemented");
1111
const { parseURLToResultingURLRecord } = require("../helpers/document-base-url");
@@ -47,14 +47,24 @@ class HTMLFormElementImpl extends HTMLElementImpl {
4747
super._descendantRemoved.apply(this, arguments);
4848
}
4949

50+
_getElementNodes() {
51+
return domSymbolTree.treeToArray(this.getRootNode({}), {
52+
filter: node => {
53+
if (!isListed(node) || (node._localName === "input" && node.type === "image")) {
54+
return false;
55+
}
56+
57+
return formOwner(node) === this;
58+
}
59+
});
60+
}
61+
5062
// https://html.spec.whatwg.org/multipage/forms.html#dom-form-elements
5163
get elements() {
5264
// TODO: Return a HTMLFormControlsCollection
5365
return HTMLCollection.createImpl(this._globalObject, [], {
54-
element: this,
55-
query: () => domSymbolTree.treeToArray(this, {
56-
filter: node => isListed(node) && (node._localName !== "input" || node.type !== "image")
57-
})
66+
element: this.getRootNode({}),
67+
query: () => this._getElementNodes()
5868
});
5969
}
6070

test/web-platform-tests/to-run.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -707,7 +707,6 @@ DIR: html/semantics/forms/the-form-element
707707
form-action-submission-with-base-url.html: [timeout, Unknown]
708708
form-action-submission.html: [timeout, Unknown]
709709
form-autocomplete.html: [fail, Unknown]
710-
form-elements-filter.html: [fail, Unknown]
711710
form-elements-interfaces-01.html: [fail, Unknown]
712711
form-elements-nameditem-01.html: [fail, Unknown]
713712
form-indexed-element.html: [fail, Unknown]
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<meta charset="utf-8" />
3+
<title>Form.elements should update on form.id change</title>
4+
<script src="/resources/testharness.js"></script>
5+
<script src="/resources/testharnessreport.js"></script>
6+
<div id=log></div>
7+
8+
<form name="test">
9+
<input name="internal" form="testForm" type="hidden" />
10+
</form>
11+
<input name="external" form="testForm" type="hidden" />
12+
13+
<script>
14+
"use strict";
15+
16+
test(() => {
17+
const form = document.querySelector("form");
18+
19+
assert_equals(form.elements.length, 0);
20+
}, "`form.elements` should not contain elements inside form with different form owner");
21+
22+
test(() => {
23+
const form = document.querySelector("form");
24+
25+
assert_equals(form.elements.length, 0, "pre-condition");
26+
27+
form.id = "testForm";
28+
29+
assert_equals(form.elements.length, 2);
30+
}, "`form.elements` should update on form.id change");
31+
32+
</script>

0 commit comments

Comments
 (0)