2

I need to create a code sample where I want "<" and ">" to be interpreted as standard text. If I do the following

<pre>
    Some content that contains <and>
</pre>

It will not be interpreted properly unless I html encode my "<"/ ">" characters. Is there simple way around this?

Test Fiddle: http://jsfiddle.net/basarat/Az3hZ/

basarat
  • 261,912
  • 58
  • 460
  • 511
  • Don't know if you mean something like the top answer in this question: http://stackoverflow.com/questions/1787322/htmlspecialchars-equivalent-in-javascript ? – Michael Schmidt May 12 '13 at 07:47
  • 1
    Maybe this can help: http://stackoverflow.com/questions/1219860/javascript-jquery-html-encoding? – Andi May 12 '13 at 07:53
  • Thanks @Andi I'll use that. I'll have to load files via ajax calls instead of a simple copy paste but I guess its better than way anyways since code samples will stay relevant. – basarat May 12 '13 at 07:58

2 Answers2

4

You can encode the brackets:

  • > = &gt;
  • < = &lt;

So:

<pre>
    Some content that contains &lt;and&gt;
</pre>
Oded
  • 489,969
  • 99
  • 883
  • 1,009
1

Because your HTML document is XML, <and> is a new tag.

See XML predefined entities.

You have no choice but to encode it.

magnetik
  • 4,351
  • 41
  • 58
  • Thanks for explicitly mentioning "no choice". – basarat May 12 '13 at 07:55
  • -1 - HTML != XML. For example `
    ` is valid HTML, but not valid XML. HTML _is_ however SGML (though HTML 5 has divorced itself from that, IIRC)
    – Oded May 12 '13 at 08:10