Unlike the recently closed [#1621], this problem isn't a regression and has existed at least as far back as 2.12.
Summary:
Owner forms returned by HtmlUnit do not match conventional browsers in certain cases when <form></form>s aren't aligned with the DOM tree.
For example, the following incorrectly returns "f1" when HtmlElement.getEnclosingForm() is called on the f2_submit element. Chrome/IE11/Firefox returns "f2":
<html>
<body>
<div>
<form name="f1" action="f1.html" method="GET">
<table>
<input type="submit" name="f1_submit" value="expect f1"/>
</form>
<form name="f2" action="f2.html" method="GET">
</table>
</div>
<input type="submit" name="f2_submit" value="expect f2"/>
</form>
</body>
</html>
After some testing in Chrome/IE11/Firefox, owner forms seem to be determined by these three rules:
1. Document order pure and simple. (Completely ignoring the DOM heirachy):
<html>
...
<form name="f1">
... everything in between belongs to f1 ...
</form>
...
<form name="f2">
... everything here and belongs to f2 ... (missing </form> so until the end of document)
2. Anything not encompassed between <form>...</form>, in document order, belong to the closest DOM-tree ancestor if any.
<html>
...
<form name="f1">
... everything here belongs to f1 ...
<div>
... everything here belongs to f1 ...
</form>
... things here belong to f1 because DOM-tree is `<form><div></div></form>` ...
</div>
...
3. <form>s within <form>s simply cease to exist. (This is also determined by document order pure and simple.)
<html>
...
<form name="f1">
... everything in between belongs to f1 ...
<form name="f2">
... everything in between belongs to f1 ...
</form><!--This actually closed f1's document-order enclosure-->
... things here belong to no one ...
</form>
... things here belong to no one ...
Conclusion: 1 and 3 seem to happen with no regards to the DOM tree and it doesn't seem to matter how whacky and nested the surrounding <element> and </element> placements are. Even though <form> nesting is not allowed in document order, it can be nested in the resulting DOM tree.
This code tests the above attachments:
~~~~~~~
public static void main(String[] args) throws IOException {
final String[] pageNames = { "doc-order1.html", "doc-order1-div.html",
"consumed1.html", "consumed1-div.html", "consumed2.html",
"dom-tree1.html", "dom-tree2.html" };
}
~~~~~~
Last edit: Atsushi Nakagawa 2014-08-08
Fixed in 2.71.0