I'm writing a Chrome extension that involves doing a lot of the following job: sanitizing strings that might contain HTML tags, by converting <
, >
and &
to <
, >
and &
, respectively.
(In other words, the same as PHP's htmlspecialchars(str, ENT_NOQUOTES)
– I don't think there's any real need to convert double-quote characters.)
This is the fastest function I have found so far:
function safe_tags(str) {
return str.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>') ;
}
But there's still a big lag when I have to run a few thousand strings through it in one go.
Can anyone improve on this? It's mostly for strings between 10 and 150 characters, if that makes a difference.
(One idea I had was not to bother encoding the greater-than sign – would there be any real danger with that?)
Martijn's method as a prototype function:
You could try passing a callback function to perform the replacement:
Here is a performance test: http://jsperf.com/encode-html-entities to compare with calling the
replace
function repeatedly, and using the DOM method proposed by Dmitrij.Your way seems to be faster...
Why do you need it, though?
Martijn's method as single function with handling " mark (using in javascript) :
The AngularJS source code also has a version inside of angular-sanitize.js.
All-in-one script:
http://pastebin.com/JGCVs0Ts
I'm not entirely sure about speed, but if you are looking for simplicity I would suggest using the lodash/underscore escape function.