3
Vote

Why are Input elements not part of a Form element

description

Why does this test fail?
 
    [TestMethod]
    public void HAP_FormWithInput_InputIsChildOfForm()
    {
        // Arrange
        var html = @"
<html>
<head> <title>Form Test</title> </head> <body>
<form method=""post"" name=""test"" action=""action"" />
<input type=""hidden"" name=""input1"" value=""value1"" />
</form>
</body>
</html>";
        var doc = new HtmlDocument();
        doc.LoadHtml(html);
        var form = doc.DocumentNode.Descendants("form").Single();
 
        // Act
        var actual = form.ChildNodes.Count();
 
        // Assert
        Assert.AreEqual(1, actual);
    }

comments

DarthObiwan wrote Oct 9, 2012 at 11:55 PM

HAP does not duplicate how the DOM works. It exposes the true node nesting of HTML. Form exposing the child input elements is part of the DOM specification used by browsers and is not actually part of the HTML specification.

That being said in the future HAP may expose this.
For now you can do a query for any input nodes under form.
doc.DocumentNode.Descendants("form").Descendants("input")