Skip to content

Instantly share code, notes, and snippets.

@aishikaty
Created November 14, 2017 17:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aishikaty/dcb6e7f3441c1c8321a34437139bf17f to your computer and use it in GitHub Desktop.
Save aishikaty/dcb6e7f3441c1c8321a34437139bf17f to your computer and use it in GitHub Desktop.
/** @version 0.1 */
const html = (strings, ...values) => (
Array.from(strings).map((string, index) => (
string + (
Array.isArray(values[index])
? values[index].join("")
: (new Option(values[index])).innerHTML
)
)).join('')
)
<script src="safe-html-template-literal.js"></script>
<script>
document.addEventListener("DOMContentLoaded", () => {
document.body.innerHTML = html`
<pre>${"<b>escaped html</b>"}</pre>
<pre>${["<b>unescaped html</b>"]}<pre>
`
})
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment