From b60d43e5f6831a4d1c80702d9a87907b782ab84a Mon Sep 17 00:00:00 2001 From: John-David Dalton Date: Tue, 12 Jun 2012 19:28:47 -0400 Subject: [PATCH] Ensure `sourceURL` support doesn't cause errors in Adobe's JS engine. Former-commit-id: 2ad1214e6a58832c732499b272ebfc434953213b --- build/pre-compile.js | 2 +- lodash.js | 23 ++++++++++++++++------- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/build/pre-compile.js b/build/pre-compile.js index 4fc1bdd63..8f00ee075 100644 --- a/build/pre-compile.js +++ b/build/pre-compile.js @@ -217,7 +217,7 @@ source = source.replace('"\';\\n"', '"\';"'); // remove debug sourceURL in `_.template` - source = source.replace(/\+(?:\s*\/\/.*)*\s*'\/\/@ sourceURL=[^;]+/, ''); + source = source.replace(/(?:\s*\/\/.*\n)* *if *\(useSourceURL[^}]+}/, ''); // minify `_.sortBy` internal properties (function() { diff --git a/lodash.js b/lodash.js index 663539899..43ddcb208 100644 --- a/lodash.js +++ b/lodash.js @@ -54,6 +54,12 @@ /** Used to store tokenized template text snippets */ var tokenized = []; + /** Detect if sourceURL syntax is usable without erroring */ + try { + // Adobe's and Narwhal's JS engines will error + var useSourceURL = (Function('//@')(), true); + } catch(e){ } + /** * Used to escape characters for inclusion in HTML. * The `>` and `/` characters don't require escaping in HTML and have no @@ -2641,8 +2647,9 @@ // ensure both objects have the same number of properties if (result) { for (prop in b) { - // Adobe JS engines have an operator precedence bug that causes `!size--` - // to produce the wrong result so it must be wrapped in parentheses. + // Adobe's JS engine, embedded in applications like InDesign, has a + // bug that causes `!size--` to throw an error so it must be wrapped + // in parentheses. // https://github.com/documentcloud/underscore/issues/355 if (hasOwnProperty.call(b, prop) && !(size--)) { break; @@ -3208,11 +3215,13 @@ 'var __p, __t, __j = Array.prototype.join;\n' + 'function print() { __p += __j.call(arguments, \'\') }\n' + text + - 'return __p\n}\n' + - // add sourceURL for easier debugging - // (Narwhal requires a trailing newline to prevent a syntax error) - // http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl - '//@ sourceURL=/lodash/template/source[' + (templateCounter++) + ']\n'; + 'return __p\n}'; + + // add a sourceURL for easier debugging + // http://www.html5rocks.com/en/tutorials/developertools/sourcemaps/#toc-sourceurl + if (useSourceURL) { + text += '\n//@ sourceURL=/lodash/template/source[' + (templateCounter++) + ']'; + } result = Function('_', 'return ' + text)(lodash);