Skip to content

Instantly share code, notes, and snippets.

@dergachev
Last active December 18, 2015 05:19
Show Gist options
  • Save dergachev/5732338 to your computer and use it in GitHub Desktop.
Save dergachev/5732338 to your computer and use it in GitHub Desktop.
Safely embedding code snippets in HTML.
<h1>Embedding code snippets in HTML</h1>
<p> Source (bootstrapped!):</p>
<script type="text/snippet">
<script type="text/snippet">
<small>bob</small>
</ script>
</script>
<p> Results:</p>
<script type="text/snippet">
<small>bob</small>
</script>
<p> As you can see, this doesn't work well for &lt;/script&gt; is still problematic, and, need to be escaped in some awkward way.</p>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="snippet-embed.js"></script>
var $ = jQuery,
$('script[type="text/snippet"]').each(function() {
var el = $(this);
var safe = $('<pre/>').text(el.html().trim());
el.after(safe);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment