11

I'd like to add an area to a page where all of the dynamic content is rendered as plain text instead of markup. For example:

  <myMagicTag>
      <b>Hello</b> World
  </myMagicTag>

I want the <b> tag to show up as just text and not as a bold directive. I'd rather not have to write the code to convert every "<" to an "&lt;".

I know that <textarea> will do it, but it has other undesirable side effects like adding scroll bars.

Does myMagicTag exist?

Edit: A jQuery or javascript function that does this would also be ok. Can't do it server-side, unfortunately.

ccleve
  • 15,239
  • 27
  • 91
  • 157
  • 1
    see this question http://stackoverflow.com/questions/4611591/code-vs-pre-vs-samp-for-inline-and-block-code-snippets – Decker W Brower Apr 19 '13 at 17:36
  • You could put it in a – Šime Vidas Apr 19 '13 at 17:57

6 Answers6

17

You can do this with the script element (bolded by me):

The script element allows authors to include dynamic script and data blocks in their documents.

Example:

<script type="text/plain">
This content has the media type plain/text, so characters reserved in HTML have no special meaning here: <div> ← this will be displayed.
</script>

(Note that the allowed content of the script element is restricted, e.g. you can’t have </script> as text content (it would close the script element).)

Typically, script elements have display:none by default in browser’s CSS, so you’d need to overwrite that in your CSS, e.g.:

script[type="text/plain"] {display:block;}
unor
  • 92,415
  • 26
  • 211
  • 360
  • This is really interesting, specially for rendering source code. Thank you for sharing this answer. Is this reliable? What are the limitations of this approach (besides what is documented in the link you've posted)? – alecov Jul 02 '16 at 18:04
  • Edit: Well, I've just found that this usage (using ` – alecov Jul 02 '16 at 18:28
3

The tag used to be <XMP> but in HTML 4 it was already deprecated. Browser's don't seem to have dropped its support but I would not recommend it for anything beyond quick debugging. The MDN article about <XMP> lists two other tags, <plaintext> and <listing>, that were deprecated even earlier. I'm not aware of any current alternative.

Whatever, the code to encode plain text into HTML is pretty straightforward in most programming languages.

Note: the term similar means exactly that—all three are designed to inject plain text into HTML. I'm not implying that they are synonyms or that they behave identically—they don't.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • There is no reason to expect browsers to drop support, partly because HTML5 requires continued support to `xmp` in browsers. The two other elements mentioned in the MDN article are *not* similar to `xmp` (and have hardly been used for a decade). – Jukka K. Korpela Apr 19 '13 at 19:26
  • @JukkaK.Korpela - Well, that's what the article says, *The `` and `<listing>` elements, similar to `<xmp>` but also obsolete.* &lt;:-)</xmp></listing></plaintext></span> –&nbsp;<a href="../../users/13508/alvaro-gonzalez" title="142,137 reputation" class="comment-user ">Álvaro González</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/16110382/html-tag-that-causes-other-tags-to-be-rendered-as-plain-text#comment23009823_16110555"><span title="2013-04-19T19:31:25.703 License: CC BY-SA 3.0" class="relativetime-clean">Apr 19 '13 at 19:31</span></a></span> </div> </div> </li> <li id="comment-23010539" class="comment js-comment " data-comment-id="23010539" data-comment-owner-id="1084437" data-comment-score="1"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> <span title="number of 'useful comment' votes received" class="warm">1</span> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment23010539_16110555"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">It says they are similar. They are not. The `plaintext` element has no end tag and always extends to the end of the document. The `listing` element was meant to be similar to `xmp`, but it was actually implemented like `pre`.</span> –&nbsp;<a href="../../users/1084437/jukka-k-korpela" title="195,524 reputation" class="comment-user ">Jukka K. Korpela</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/16110382/html-tag-that-causes-other-tags-to-be-rendered-as-plain-text#comment23010539_16110555"><span title="2013-04-19T19:55:50.027 License: CC BY-SA 3.0" class="relativetime-clean">Apr 19 '13 at 19:55</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="16111168"></a> <div id="answer-16111168" class="answer " data-answerid="16111168" data-ownerid="2146609" data-score="3" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="16111168"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="3">3</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>You can use a function to escape the &lt; &gt;, eg:</p> <pre><code>'span.name': function(){ return this.name.replace(/&lt;/g, '&amp;lt;').replace(/&gt;/g, '&amp;gt;'); } </code></pre> <p>Also take a look at <code>&lt;plaintext&gt;&lt;/plaintext&gt;</code>. I haven't used it myself but it is known to render everything that follows as plain text(by everything i mean to say it ignores the closing tag, so all the following code is rendered as text)</p></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Apr 19 '13 at 18:21">answered Apr 19 '13 at 18:21</time> <a href="../../users/2146609/detraveller" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/2146609.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="detraveller" /> </a> <div class="s-user-card--info"> <a href="../../users/2146609/detraveller" class="s-user-card--link">detraveller</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">285</li> <li class="s-award-bling s-award-bling__silver" title="3 silver badges">3</li> <li class="s-award-bling s-award-bling__bronze" title="17 bronze badges">17</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-16111168" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="16111168" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-23016430" class="comment js-comment " data-comment-id="23016430" data-comment-owner-id="237815" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment23016430_16111168"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">Not sure why this was downvoted, so I voted it back up. The javascript is a good solution.</span> –&nbsp;<a href="../../users/237815/ccleve" title="15,239 reputation" class="comment-user owner">ccleve</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/16110382/html-tag-that-causes-other-tags-to-be-rendered-as-plain-text#comment23016430_16111168"><span title="2013-04-20T02:15:16.060 License: CC BY-SA 3.0" class="relativetime-clean">Apr 20 '13 at 02:15</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> <a name="57347205"></a> <div id="answer-57347205" class="answer " data-answerid="57347205" data-ownerid="" data-score="1" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="57347205"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="1">1</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>There is no specific tag except the deprecated <code>&lt;xmp&gt;</code>.<br/> But a script tag is allowed to store unformatted data.</p> <p>Here is the only solution so far showing <strong>dynamic</strong> content, as you wanted.<br/> Run code snippet for more info.</p> <p></p><div class="snippet" data-babel="false" data-console="true" data-hide="false" data-lang="js"> <div class="snippet-code"> <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script id="myMagicTag" type="text/plain" style="display:block;"&gt; &lt;b&gt;Hello&lt;/b&gt; World &lt;/script&gt; Use &lt;a href="https://stackoverflow.com/questions/38143580/when-should-script-tags-be-visible-and-why-can-they"&gt;Visible&lt;/a&gt; &lt;a href="https://html.spec.whatwg.org/multipage/scripting.html#data-block"&gt;Data-blocks&lt;/a&gt; &lt;script&gt; document.querySelector("#myMagicTag").innerHTML = "&lt;b&gt;Unformatted&lt;/b&gt; dynamic content" &lt;/script&gt;</code></pre> </div> </div> </div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="user-info "> <div class="user-action-time">edited <span title="2019-08-04T14:22:23.200" class="relativetime">Aug 04 '19 at 14:22</span></div> <div class="user-gravatar32"></div> <div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person"> <span class="d-none" itemprop="name"></span> <div class="-flair"></div> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card__deleted"> <time class="s-user-card--time" datetime="answered Aug 04 '19 at 14:03">answered Aug 04 '19 at 14:03</time> <div class="s-avatar s-avatar__32 s-user-card--avatar"> </div> <div class="s-user-card--info"></div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="16110415"></a> <div id="answer-16110415" class="answer " data-answerid="16110415" data-ownerid="1816704" data-score="0" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="16110415"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="0">0</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>No, that's not possible, you need to HtmlEncode it.</p> <p>If your using a server-side language, that's not really difficult though.</p> <p>In .NET you would do something like this:</p> <pre><code>string encodedtext = HttpContext.Current.Server.HtmlEncode(plaintext); </code></pre></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Apr 19 '13 at 17:35">answered Apr 19 '13 at 17:35</time> <a href="../../users/1816704/kenneth" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/1816704.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Kenneth" /> </a> <div class="s-user-card--info"> <a href="../../users/1816704/kenneth" class="s-user-card--link">Kenneth</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">28,294</li> <li class="s-award-bling s-award-bling__gold" title="6 gold badges">6</li> <li class="s-award-bling s-award-bling__silver" title="61 silver badges">61</li> <li class="s-award-bling s-award-bling__bronze" title="84 bronze badges">84</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> </div> </div> </div> <a name="34697716"></a> <div id="answer-34697716" class="answer " data-answerid="34697716" data-ownerid="1925948" data-score="0" itemprop="suggestedAnswer" itemscope="" itemtype="https://schema.org/Answer"> <div class="post-layout"> <div class="votecell post-layout--left"> <div class="js-voting-container grid jc-center fd-column ai-stretch gs4 fc-black-200" data-post-id="34697716"> <button class="js-vote-up-btn grid--cell s-btn s-btn__unset c-pointer"><svg aria-hidden="true" class="m0 svg-icon iconArrowUpLg" width="36" height="36" viewBox="0 0 36 36"><path d="M2 26h32L18 10 2 26z"></path></svg></button> <div class="js-vote-count grid--cell fc-black-500 fs-title grid fd-column ai-center" itemprop="upvoteCount" data-value="0">0</div> </div> </div> <div class="postcell post-layout--right"> <div class="s-prose js-post-body" itemprop="text"><p>In my application, I need to prevent HTML from rendering </p> <pre><code>"if (a&lt;b || c&gt;100) ..." </code></pre> <p>and</p> <pre><code>"cout &lt;&lt; ...". </code></pre> <p>Also the entire C++ code region HTML must pass through the GCC compiler with the desired effect. I've hit on two schemes:</p> <p>First:</p> <pre><code>//&lt;xmp&gt; #include &lt;string&gt; //&lt;/xmp&gt;} </code></pre> <p>For reasons that escape me, the <code>&lt;xmp&gt;</code> tag is deprecated. I find (2016-01-09) that Chrome and FF, at least, render the tag the way I want. While researching my problem, I saw a remark that &lt;xmp&gt; is required in HTML 5.</p> <p>Second, in <code>&lt;head&gt; ... &lt;/head&gt;</code>, insert:</p> <pre><code>&lt;style type="text/css"&gt; textarea { border: none; } &lt;/style&gt; </code></pre> <p>Then in <code>&lt;body&gt; ... &lt;/body&gt;</code>, write:</p> <pre><code>//&lt;br /&gt; &lt;textarea rows="4" disabled cols="80"&gt; #include &lt;stdlib.h&gt; #include &lt;iostream&gt; #include &lt;string&gt; //&lt;/textarea&gt; &lt;br /&gt; </code></pre> <p>Note: Set <code>"cols="80"</code> to prevent following text from appearing on the right. Set <code>"rows=..."</code> to one more line than you enclose in the tag. This prevents scroll bars. This second technique has several disadvantages:</p> <ul> <li>The "disabled" attribute shades the region</li> <li>Incomprehensible, complex comments in the code sent to the compiler</li> <li>Harder to understand</li> <li>More typing</li> </ul> <p>However, this methhod is neither obsolete nor deprecated. The gods of HTML will make their faces to shine unto you.</p></div> <div class="mb0"> <div class="mt16 grid gs8 gsy fw-wrap jc-end ai-start pt4 mb16"> <div class="grid--cell mr16 fl1 w96"></div> <div class="post-signature grid--cell"> <div class="user-info "> <div class="user-action-time">edited <span title="2016-01-12T14:24:24.017" class="relativetime">Jan 12 '16 at 14:24</span></div> <div class="user-gravatar32"></div> <div class="user-details" itemprop="author" itemscope="" itemtype="http://schema.org/Person"> <span class="d-none" itemprop="name">Bill Drissel</span> <div class="-flair"></div> </div> </div> </div> <div class="post-signature grid--cell"> <div class="s-user-card s-user-card"> <time class="s-user-card--time" datetime="answered Jan 09 '16 at 19:08">answered Jan 09 '16 at 19:08</time> <a href="../../users/1925948/bill-drissel" class="s-avatar s-avatar__32 s-user-card--avatar"> <img class="s-avatar--image" src="../../users/profiles/1925948.webp" data-jdenticon-width="32" data-jdenticon-height="32" data-jdenticon-value="Bill Drissel" /> </a> <div class="s-user-card--info"> <a href="../../users/1925948/bill-drissel" class="s-user-card--link">Bill Drissel</a> <ul class="s-user-card--awards"> <li class="s-user-card--rep" title="reputation score">186</li> <li class="s-award-bling s-award-bling__silver" title="1 silver badges">1</li> <li class="s-award-bling s-award-bling__bronze" title="6 bronze badges">6</li> </ul> </div> </div> </div> </div> </div> </div> <div class="post-layout--right js-post-comments-component"> <div id="comments-34697716" class="comments js-comments-container bt bc-black-075 mt12 " data-post-id="34697716" data-min-length="15"> <ul class="comments-list js-comments-list" data-remaining-comments-count="0" data-canpost="false" data-cansee="true" data-comments-unavailable="false" data-addlink-disabled="true"> <li id="comment-57393645" class="comment js-comment " data-comment-id="57393645" data-comment-owner-id="1016716" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment57393645_34697716"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">To address your question about why xmp is deprecated, there is this remark in the [HTML3 DTD](https://www.w3.org/MarkUp/html3/html3.dtd): " -- The XMP, LISTING and PLAINTEXT tags are incompatible with SGML and derive from very early versions of HTML. They require non- standard parsers and will cause problems for processing documents with standard SGML tools. --"</span> –&nbsp;<a href="../../users/1016716/mr-lister" title="45,515 reputation" class="comment-user ">Mr Lister</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/16110382/html-tag-that-causes-other-tags-to-be-rendered-as-plain-text#comment57393645_34697716"><span title="2016-01-16T10:28:25.997 License: CC BY-SA 3.0" class="relativetime-clean">Jan 16 '16 at 10:28</span></a></span> </div> </div> </li> <li id="comment-57417777" class="comment js-comment " data-comment-id="57417777" data-comment-owner-id="1925948" data-comment-score="0"> <div class="js-comment-actions comment-actions"> <div class="comment-score js-comment-edit-hide"> </div> </div> <div class="comment-text js-comment-text-and-form"> <a name="comment57417777_34697716"></a> <div class="comment-body js-comment-edit-hide"> <span class="comment-copy">I don't know how many people write SGML but I'd guess HTML predominates. Maybe the SGML spec should be mod'ed. After all, in-band signalling systems always need escape sequences. Many mark-up languages use "\n\n" to cause a 'new paragraph' action.</span> –&nbsp;<a href="../../users/1925948/bill-drissel" title="186 reputation" class="comment-user ">Bill Drissel</a> <span class="comment-date" dir="ltr"><a class="comment-link" href="../../questions/16110382/html-tag-that-causes-other-tags-to-be-rendered-as-plain-text#comment57417777_34697716"><span title="2016-01-17T12:52:01.113 License: CC BY-SA 3.0" class="relativetime-clean">Jan 17 '16 at 12:52</span></a></span> </div> </div> </li> </ul> </div> </div> </div> </div> </div> </div> <div id="sidebar" class="show-votes" role="complementary" aria-label="sidebar"> <div class="module sidebar-linked"> <h4 id="h-linked">Linked</h4> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">143</div></a> <a href="../../questions/38143580/when-should-script-tags-be-visible-and-why-can-they" class="question-hyperlink">When should &lt;script&gt; tags be visible and why can they?</a> </div> </div> <div class="linked"> <div class="spacer"> <a title="Vote score (upvotes - downvotes)"><div class="answer-votes answered-accepted default">0</div></a> <a href="../../questions/51222713/using-an-individual-tag-without-breaking-the-standards" class="question-hyperlink">Using an individual tag without breaking the standards?</a> </div> </div> </div> </div> </div> </div> <script src="../../static/js/stack-icons.js"></script> <script src="../../static/js/fromnow.js"></script> </body> </html>