My favorites | Sign in
Project Logo
             
Search
for
Updated Aug 05, 2007 by alex.gorbatchev
Labels: Documentation
PreAndTextarea  
Technical details about <pre> and <textarea>.

Technical details about <pre> and <textarea>

Problem description

The fundamental problem with using <pre> is inability to easily copy and paste code into it. In particular this applies to XML/HTML.

The problem with <pre> is that browsers parse HTML inside it where as in <textarea> they do not. For example:

<pre><b>Hello</b></pre>

Will render as Hello, where as

<textarea><b>Hello</b></textarea>

will render as <b>Hello</b>.

If you try to get HTML from <pre> using innerHTML property, it's not going to look anything like what you have originally in the HTML file. With <textarea> however, a property value would return your HTML exactly the same way as you have provided it.

Conclusion

This brings me to the final point. With <textarea> you can copy and paste source code without any alterations. With <pre> you have to make sure your source code won't be recognized as HTML, which means you have to replace every opening triangular bracket < with it's HTML encoded version &lt;. This is enough to make <pre> work fine.


Comment by brandon.byars, Aug 17, 2007

Is there any way to embed HTML that should be parsed inside a <pre> tag that gets altered with syntaxHighlighter? Specifically, I'd like to embed span tags that mark up newly added code to highlight the difference between two code blocks, but syntaxHighlighter is matching the class attribute and making the literal HTML render. Thanks!

Comment by docwhat, Oct 19, 2007

You also need to replace & with &amp; for XML/XHTML compatibility. Escaping " -> &quot; is also good.

Ciao!

Comment by dan82pettersson, May 15, 2008

Since pre is the only way to go if you want the code to be somewhat readable in various rss-feed tools (I tested with Bloglines) I made a little javascript "widget" for these replacements to take place. I made a blogpost about it on my blog at http://deapondotnet.blogspot.com/2008/05/lite-fix-med-syntaxhighlighter.htmlhttp://deapondotnet.blogspot.com. It's in swedish but I think you'll get the idea from the code i published. Feel free to use it.

Comment by dan82pettersson, May 27, 2008
Comment by fahd.shariff, Jul 21, 2008

Also note that if you use <textarea> then it won't come through to your RSS reader

Hosted by Google Code