Skip to content

Instantly share code, notes, and snippets.

@beratdogan
Last active August 29, 2015 14:01
Show Gist options
  • Save beratdogan/97d0b9c3fedebf65bfca to your computer and use it in GitHub Desktop.
Save beratdogan/97d0b9c3fedebf65bfca to your computer and use it in GitHub Desktop.
Coffee type extends
String::htmlspecialchars = (quote_style = 2, double_encode) ->
optTemp = 0
i = 0
noquotes = false
string = @.toString()
string = string.replace(/&/g, '&') if double_encode isnt false
string = string.replace(/</g, '&lt;').replace(/>/g, '&gt;')
OPTS =
ENT_NOQUOTES: 0
ENT_HTML_QUOTE_SINGLE: 1
ENT_HTML_QUOTE_DOUBLE: 2
ENT_COMPAT: 2
ENT_QUOTES: 3
ENT_IGNORE: 4
noquotes = true if quote_style is 0
if typeof quote_style isnt 'number'
quote_style = [].concat(quote_style)
for style in [0...quote_style]
if OPTS[style] is 0
noquotes = true
else if OPTS[style]
optTemp = optTemp | OPTS[style]
quote_style = optTemp
string = string.replace(/'/g, '&#039;') if quote_style & OPTS.ENT_HTML_QUOTE_SINGLE
string = string.replace(/"/g, '&quot;') if !noquotes
string
String::stripslashes = ->
(@ + '').replace(/\\(.?)/g, (s, n1) ->
switch n1
when '\\' then return '\\'
when '0' then return '\u0000'
when '' then return ''
else return n1
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment