From 9d7136c63c7274259a734161fdf73adc4434cefb Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Thu, 5 Jul 2012 14:56:27 -0400 Subject: [PATCH] Add more checks to the `reComplexDelimiter` regexp. Former-commit-id: 87fba2813619882d388261a1226e75503ec9b8d0 --- lodash.js | 2 +- test/test.js | 31 ++++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lodash.js b/lodash.js index 9f6157d35..13d71255e 100644 --- a/lodash.js +++ b/lodash.js @@ -55,7 +55,7 @@ var oldDash = window._; /** Used to detect delimiter values that should be processed by `tokenizeEvaluate` */ - var reComplexDelimiter = /[-)}={(+]|\[\D/; + var reComplexDelimiter = /[-+=!~*%&^<>|{(\/]|\[\D|\b(?:delete|in|instanceof|new|typeof|void)\b/; /** Used to match code generated in place of template delimiters */ var reDelimiterCodeLeading = /^';\n/, diff --git a/test/test.js b/test/test.js index b6e61311b..a857fe88c 100644 --- a/test/test.js +++ b/test/test.js @@ -671,10 +671,35 @@ }); test('should work with complex "interpolate" delimiters', function() { - var compiled = _.template('<%= a + b %>'), - data = { 'a': 1, 'b': 2 }; + _.each({ + '<%= a + b %>': '3', + '<%= b - a %>': '1', + '<%= a = b %>': '2', + '<%= !a %>': 'false', + '<%= ~a %>': '-2', + '<%= a * b %>': '2', + '<%= a / b %>': '0.5', + '<%= a % b %>': '1', + '<%= a >> b %>': '0', + '<%= a << b %>': '4', + '<%= a & b %>': '0', + '<%= a ^ b %>': '3', + '<%= a | b %>': '3', + '<%= {}.toString.call(0) %>': '[object Number]', + '<%= a.toFixed(2) %>': '1.00', + '<%= obj["a"] %>': '1', + '<%= delete a %>': 'true', + '<%= "a" in obj %>': 'true', + '<%= obj instanceof Object %>': 'true', + '<%= new Boolean %>': 'false', + '<%= typeof a %>': 'number', + '<%= void a %>': '' + }, function(value, key) { + var compiled = _.template(key), + data = { 'a': 1, 'b': 2 }; - equal(compiled(data), 3); + equal(compiled(data), value); + }); }); }());