From c45c345c6bd046383c973cb90dc025a1229ce086 Mon Sep 17 00:00:00 2001 From: Vitaly Puzrin Date: Sun, 26 Oct 2014 05:47:11 +0300 Subject: [PATCH] Perf: optimized html escape helper --- lib/common/utils.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/common/utils.js b/lib/common/utils.js index 97372f6..51d91f3 100644 --- a/lib/common/utils.js +++ b/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, '&'); } - if (str.indexOf('<') >= 0) { str = str.replace(/') >= 0) { str = str.replace(/>/g, '>'); } - if (str.indexOf('"') >= 0) { str = str.replace(/"/g, '"'); } + if (ESCAPE_TEST_RE.test(str)) { + if (str.indexOf('&') >= 0) { str = str.replace(/&/g, '&'); } + if (str.indexOf('<') >= 0) { str = str.replace(/') >= 0) { str = str.replace(/>/g, '>'); } + if (str.indexOf('"') >= 0) { str = str.replace(/"/g, '"'); } + } return str; }