diff --git a/build/pre-compile.js b/build/pre-compile.js index 17bc43bce..4fc1bdd63 100644 --- a/build/pre-compile.js +++ b/build/pre-compile.js @@ -202,8 +202,8 @@ // http://code.google.com/closure/compiler/docs/api-tutorial3.html#export source = source.replace(RegExp('\\.(' + propWhitelist.join('|') + ')\\b', 'g'), "['$1']"); - // remove brackets from `_.escape(__t)` in `tokenizeEscape` - source = source.replace("_['escape'](__t)", '_.escape(__t)'); + // remove brackets from `_.escape()` in `tokenizeEscape` + source = source.replace("_['escape'](\"", '_.escape("'); // remove whitespace from string literals source = source.replace(/'(?:(?=(\\?))\1.)*?'/g, function(string) { diff --git a/lodash.js b/lodash.js index 673a59bf6..081e8b0f4 100644 --- a/lodash.js +++ b/lodash.js @@ -525,7 +525,7 @@ */ function tokenizeEscape(match, value) { var index = tokenized.length; - tokenized[index] = "'+\n((__t = (" + value + ")) == null ? '' : _.escape(__t)) +\n'"; + tokenized[index] = "'+\n_.escape(" + value + ") +\n'"; return token + index; } @@ -2985,7 +2985,7 @@ * // => "Curly, Larry & Moe" */ function escape(string) { - return (string + '').replace(reUnescapedHtml, escapeHtmlChar); + return string == null ? '' : (string + '').replace(reUnescapedHtml, escapeHtmlChar); } /** diff --git a/test/test.js b/test/test.js index 1c27d23b1..764d4d311 100644 --- a/test/test.js +++ b/test/test.js @@ -157,6 +157,11 @@ test('should not escape the "/" character', function() { equal(_.escape('/'), '/'); }); + + test('should return empty string when passed `null` or `undefined`', function() { + equal(_.escape(null), ''); + equal(_.escape(undefined), ''); + }); }()); /*--------------------------------------------------------------------------*/