ASP.NET MVC Textarea HTML Helper Adding Strings When Using AntiXssLibrary

I noticed the most annoying (possible) error in the ASP.NET MVC TextAreaFor HTML helper. For some reason, the HTML helper adds NewLine in front of any content in the text box. Apparently, this is necessary to deal with the possible problem of individual content, starting from a new line, and the browser ignores it according to the specification.

However, adding it, I get a BIG more annoying error, which now in all my text areas automatically has an extra line in front of any content when loading the form (i.e. this appears before any content in my area:). It would seem that something encodes a "new line" before spitting it out.

Does anyone have a workaround for this? I would expect the intended behavior is to print

    <textarea>
    Stuff</textarea>

not

    <textarea>&#13;&#10;Stuff</textarea>

I get...

Edit On further inspection, it seems that this is due to the fact that I used AntiXssLibrary for encoding instead of the standard HtmlEncoder. I am using version 4.0, and my encoder class method is as follows:

    protected override void HtmlEncode(string value, TextWriter output)
    {
        output.Write(Microsoft.Security.Application.Encoder.HtmlEncode(value));
    }

So my THOUGHT is that since the TagBuilder class, which is called from TextAreaHelper, HTML encodes the contents of the tag, this TAKES the default behavior of the HTML encoder, but AntiXssLibrary is more thorough, and therefore you see this behavior?

+5
source share
1 answer

, . MVC3 TextArea HTML Helper TagBuilder :

tagBuilder.SetInnerText(Environment.NewLine + value);

SetInnerText HttpUtility.Encode , , VALUE, . NewNewLine, , HtmlEncoder (, AntiXssLibrary), - , .

:

tagBuilder.InnerHtml = Environment.NewLine + HttpUtility.Encode(value);

.

Javascript onLoad, :

$("textarea").each(function () { $(this).val($(this).val().trim()); });
+8

All Articles