From 227d511fc0402efaaddb2250a9d05e42f3c4ae8f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Sat, 4 Jan 2014 22:10:16 +0100 Subject: [PATCH] Only include the global handlers in the return statement that actually seem to be defined. --- src/core/PaperScript.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/core/PaperScript.js b/src/core/PaperScript.js index 6b113504..e6603cae 100644 --- a/src/core/PaperScript.js +++ b/src/core/PaperScript.js @@ -239,7 +239,7 @@ var PaperScript = Base.exports.PaperScript = (function() { var view = scope.getView(), // Only create a tool object if something resembling a tool handler // definition is contained in the code. - tool = /\s+on(?:Key|Mouse)(?:Up|Down|Move|Drag)/.test(code) + tool = /\s+on(?:Key|Mouse)(?:Up|Down|Move|Drag)\b/.test(code) ? new Tool() : null, toolHandlers = tool ? tool._events : [], @@ -270,11 +270,16 @@ var PaperScript = Base.exports.PaperScript = (function() { // end of the code execution, so we can retrieve their values from the // function call. handlers = Base.each(handlers, function(key) { - params.push(key); - this.push(key + ': ' + key); + // Check for each handler explicitely and only return them if they + // seem to exist. + if (new RegExp('\\s+' + key + '\\b').test(code)) { + params.push(key); + this.push(key + ': ' + key); + } }, []).join(', '); // We need an additional line that returns the handlers in one object. - code += '\nreturn { ' + handlers + ' };'; + if (handlers) + code += '\nreturn { ' + handlers + ' };'; /*#*/ if (__options.environment == 'browser') { if (window.InstallTrigger || window.chrome) { // Firefox and Chrome // On Firefox, all error numbers inside dynamically compiled code @@ -301,7 +306,7 @@ var PaperScript = Base.exports.PaperScript = (function() { /*#*/ } else { // !__options.environment == 'browser' func = Function(params, code); /*#*/ } // !__options.environment == 'browser' - var res = func.apply(scope, args); + var res = func.apply(scope, args) || {}; // Now install the 'global' tool and view handlers, and we're done! Base.each(toolHandlers, function(key) { var value = res[key];