With textboxes, Html Agility Pack InnerHtml gives an invalid string.

html-agility-pack innerhtml

Question

The following test code:

[Test]
public void PossibleHtmlAgilityPackBug()
{
    const string html = @"<input type=""text"" name=""shouldNotTrim"" />";
    var doc = new HtmlDocument();
    doc.LoadHtml(html);

    Assert.That(doc.DocumentNode.InnerHtml, Is.EqualTo(html));
}

a result is

Expected string length 42 but was 40. Strings differ at index 39.
Expected: "<input type="text" name="shouldNotTrim" />"
But was:  "<input type="text" name="shouldNotTrim">"
--------------------------------------------------^

Is this an error? Or can I alter the configuration to emit the additional "/" I need?

Thanks,

Chi

1
2
1/17/2011 3:52:39 AM

Accepted Answer

There is no bug here. INPUT is shown by default without the closing / since the parser views it as a "empty" element (see, for instance, The original empty tags are not preserved by HTMLAgilityPack. on the empty elements topics).

The causes have a past connection to HTML 3.2. Although it seems to be a problem now, INPUT did not have to be closed back then.

This will solve your issue:

public void PossibleHtmlAgilityPackBug()
{
    const string html = @"<input type=""text"" name=""shouldNotTrim"" />";
    var doc = new HtmlDocument();
    doc.OptionWriteEmptyNodes = true;
    doc.LoadHtml(html);

    Assert.That(doc.DocumentNode.InnerHtml, Is.EqualTo(html));
}

As a side note, the HTML agility pack will always attempt to construct something that will be rendered the same manner, even if it may not always produce an identical replica of the html text. Unclosed INPUTs are supported by browsers without any issues.

3
5/23/2017 9:58:19 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow