Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML: <textarea>-Tag: How to correctly escape HTML and JavaScript content displayed in there?

Tags:

html

textarea

I have a HTML Tag <textarea>$FOO</textarea> and the $FOO Variable will be filled with arbitrary HTML and JavaScript Content, to be displayed and edited within the textarea. What kind of "escaping" do I neet to apply to $FOO?

I first tought of escaping it HTML but this didnt work (as I will then get shown not the original HTML Code of $FOO but rather the escaped content. This is of course not what I want: I want to be displayed the unescaped HTML/JS Content of the variable...

Is it impossible to display HTML Content within a <textarea> tag and also allow it to be editable as full HTML?

thanks jens

like image 506
jens Avatar asked Mar 23 '10 17:03

jens


People also ask

How do you escape HTML tags in HTML?

Escape characters will always begin with the ampersand symbol (&) and end with a semicolon symbol (;). The characters in between the ampersand and semicolon make up the specific code name or number for a particular character.

Which is correct way to create textarea in HTML?

The HTML <textarea> tag is used within a form to declare a textarea element - a control that allows the user to input text over multiple rows.


2 Answers

I first tought of escaping it HTML

Yes, that's right. The contents of a <textarea> are no different from the contents of any other element like a <span> or a <p>: if you want to put some text inside you must HTML-escape any < or & characters in it to &lt; and &amp; respectively.

Browsers do tend to give you more leeway with fault markup in <textarea>​s, in that the fallback for invalid unescaped < symbols is to render them as text instead of tags, but that doesn't make it any less wrong or dangerous (for XSS).

but this didnt work

Please post what you did that didn't work. HTML-escaping is definitely the right thing.

like image 138
bobince Avatar answered Sep 22 '22 15:09

bobince


You need to replace the special character of HTML with character references (either numerical character references or entity references), in textarea, at least &, < and >.

like image 36
Gumbo Avatar answered Sep 21 '22 15:09

Gumbo