Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Agility Pack stripping self-closing tags from input

This is how I'm creating my checkbox:

HtmlInputCheckBox checkbox = new HtmlInputCheckBox();
checkbox.ID = _Data.ControlID;
checkbox.Attributes.Add("class", "checkbox");
checkbox.Attributes.Add("autocomplete", "off");
sReplacementString = element.RenderToString();

RenderToString is an extension that does this:

public static string RenderToString(this Control control)
{
    StringBuilder sb = new StringBuilder();
    using (StringWriter writer = new StringWriter(sb))
    {
        using (HtmlTextWriter htmlWriter = new HtmlTextWriter(writer))
        {
            control.RenderControl(htmlWriter);
        }
    }
    return sb.ToString();
}

This produces a string with a closing tag on the input, I can see this when debugging.

<input name="ttWBF_1" type="checkbox" id="ttWBF_1" autocomplete="off" class="checkbox" />

It's then added to the HTML using the Agility Pack as such:

HtmlNode temp = doc.CreateElement("temp");
temp.InnerHtml = sReplacementString;
HtmlNode current = inputNode;

foreach (HtmlNode child in temp.ChildNodes)
{
    inputNode.ParentNode.InsertAfter(child, current);
    current = child;
}
inputNode.ParentNode.RemoveChild(inputNode);

However in the HTML the input tag for the checkbox is missing its self-closing slash and therefore fails WC3 validation.

<input name="ttWBF_1" type="checkbox" id="ttWBF_1" autocomplete="off" class="checkbox">

This happens with my textboxes which are generated in the same way. it looks like they're getting lost when adding the HTML to the page using the agility pack.

How do I prevent this?

like image 231
Jon Avatar asked Apr 17 '12 09:04

Jon


People also ask

Is HTML input tag self closing?

is input a self closing tag? Yes, input is a self closing tag. You need not close it as it is an empty tag. It only has attributes but no content.

How do I close a self closing tag?

A self-closing tag is an element of HTML code that has evolved in the language. Typically, the self-closing tag makes use of a “/” character in order to effectively close out a beginning tag enclosed in sideways carets.

Does html5 require closing tags?

Optional HTML Closing TagsSome closing tags are optional, however. The tags are optional because it's implied that a new tag would not be able to be started without closing it.

What is HTML agility pack?

Html Agility Pack (HAP) is a free and open-source HTML parser written in C# to read/write DOM and supports plain XPATH or XSLT. It is a . NET code library that allows you to parse "out of the web" HTML files.


2 Answers

Try setting "OptionWriteEmptyNodes" flag:

HtmlDocument doc = new HtmlDocument();
doc.OptionWriteEmptyNodes = true;

// ....

UPDATE

Since my original answer was rejected, here's another possible solution.

Are you passing the correct DOCTYPE to your HTML document before rendering it? Take a look at this SO question for how to insert a DOCTYPE: Add a doctype to HTML via HTML Agility pack

like image 120
volpav Avatar answered Sep 30 '22 15:09

volpav


In the end it pains me to say that I fell back on processing the HTML with regex to add in the mising self-closing tag. I'd love a better solution as this is hacky and not future proof - it has to be added in for every tag that needs correcting:

sXHTML = Regex.Replace(sXHTML, "<input(.*?)>", "<input $1 />");
like image 27
Jon Avatar answered Sep 30 '22 17:09

Jon