Browse Source

Perf: optimized html escape helper

pull/14/head
Vitaly Puzrin 10 years ago
parent
commit
c45c345c6b
  1. 12
      lib/common/utils.js

12
lib/common/utils.js

@ -30,11 +30,15 @@ function assign(obj /*from1, from2, from3, ...*/) {
}
var ESCAPE_TEST_RE = /[<>"]/;
function escapeHtml(str) {
if (str.indexOf('&') >= 0) { str = str.replace(/&/g, '&amp;'); }
if (str.indexOf('<') >= 0) { str = str.replace(/</g, '&lt;'); }
if (str.indexOf('>') >= 0) { str = str.replace(/>/g, '&gt;'); }
if (str.indexOf('"') >= 0) { str = str.replace(/"/g, '&quot;'); }
if (ESCAPE_TEST_RE.test(str)) {
if (str.indexOf('&') >= 0) { str = str.replace(/&/g, '&amp;'); }
if (str.indexOf('<') >= 0) { str = str.replace(/</g, '&lt;'); }
if (str.indexOf('>') >= 0) { str = str.replace(/>/g, '&gt;'); }
if (str.indexOf('"') >= 0) { str = str.replace(/"/g, '&quot;'); }
}
return str;
}

Loading…
Cancel
Save