Created
February 10, 2009 18:52
-
-
Save mislav/61530 to your computer and use it in GitHub Desktop.
advanced String#stripTags() with whitelisting
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
String.prototype.stripTags = String.prototype.stripTags.wrap( | |
function(proceed, allowTags) { | |
if (allowTags) { | |
if (Object.isString(allowTags)) allowTags = $w(allowTags) | |
this.gsub(/(<\/?\s*)([^\s>]+)(\s[^>]*)?>/, function(match) { | |
if (allowTags.include(match[2].toLowerCase())) | |
return match[1] + match[2] + match[3] + '>' | |
}) | |
} else { | |
// proceed using the original function | |
return proceed(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment