Skip to content

Instantly share code, notes, and snippets.

@adamyonk
Forked from CatTail/htmlentity.js
Last active January 4, 2016 08:59
Show Gist options
  • Save adamyonk/8598757 to your computer and use it in GitHub Desktop.
Save adamyonk/8598757 to your computer and use it in GitHub Desktop.
decodeHtmlEntity = (str) ->
str.replace /&#(\d+);/g, (match, dec) ->
String.fromCharCode(dec)
encodeHtmlEntity = (str) ->
buf = []
for (var i=str.length-1;i>=0;i--) {
buf.unshift(['&#', str[i].charCodeAt(), ';'].join(''))
buf.join('')
encodeHtmlEntity2 = (string) ->
if string and typeof(string) is 'string'
document.createElement('div').appendChild(document.createTextNode(string)).parentNode.innerHTML
else
string
encodeHtmlEntity3 = (string) ->
if string and typeof(string) is 'string'
String(string).replace(/&amp;/g, '&').replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;') if string
else
string
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment