Commit b8be54b5 b8be54b5c3e0c2232d5dd118fde057d1c85fa9f6 by Nicolas Perriault

enhanced reusability of FunctionArgsInjector

1 parent cbc01fc3
Showing 1 changed file with 10 additions and 5 deletions
...@@ -342,7 +342,7 @@ ...@@ -342,7 +342,7 @@
342 */ 342 */
343 evaluate: function(fn, context) { 343 evaluate: function(fn, context) {
344 context = isType(context, "object") ? context : {}; 344 context = isType(context, "object") ? context : {};
345 var newFn = new phantom.Casper.FunctionArgsInjector(fn, context).process(); 345 var newFn = new phantom.Casper.FunctionArgsInjector(fn).process(context);
346 return this.page.evaluate(newFn); 346 return this.page.evaluate(newFn);
347 }, 347 },
348 348
...@@ -1647,9 +1647,11 @@ ...@@ -1647,9 +1647,11 @@
1647 * Function argument injector. 1647 * Function argument injector.
1648 * 1648 *
1649 */ 1649 */
1650 phantom.Casper.FunctionArgsInjector = function(fn, values) { 1650 phantom.Casper.FunctionArgsInjector = function(fn) {
1651 if (!isType(fn, "function")) {
1652 throw "FunctionArgsInjector() can only process functions";
1653 }
1651 this.fn = fn; 1654 this.fn = fn;
1652 this.values = typeof values === "object" ? values : {};
1653 1655
1654 this.extract = function(fn) { 1656 this.extract = function(fn) {
1655 var match = /^function\s?(\w+)?\s?\((.*)\)\s?\{([\s\S]*)\}/i.exec(fn.toString().trim()); 1657 var match = /^function\s?(\w+)?\s?\((.*)\)\s?\{([\s\S]*)\}/i.exec(fn.toString().trim());
...@@ -1667,9 +1669,12 @@ ...@@ -1667,9 +1669,12 @@
1667 } 1669 }
1668 }; 1670 };
1669 1671
1670 this.process = function() { 1672 this.process = function(values) {
1671 var fnObj = this.extract(this.fn); 1673 var fnObj = this.extract(this.fn);
1672 var inject = this.getArgsInjectionString(fnObj.args, this.values); 1674 if (!isType(fnObj, "object")) {
1675 throw "Unable to process function " + this.fn.toString();
1676 }
1677 var inject = this.getArgsInjectionString(fnObj.args, values);
1673 return 'function ' + (fnObj.name || '') + '(){' + inject + fnObj.body + '}'; 1678 return 'function ' + (fnObj.name || '') + '(){' + inject + fnObj.body + '}';
1674 }; 1679 };
1675 1680
......