refs #482: sync with master
Showing
5 changed files
with
34 additions
and
7 deletions
... | @@ -28,11 +28,17 @@ | ... | @@ -28,11 +28,17 @@ |
28 | * | 28 | * |
29 | */ | 29 | */ |
30 | 30 | ||
31 | /*global console, phantom, require:true*/ | 31 | /*global process, console, phantom, require:true*/ |
32 | /*jshint maxstatements:33, maxcomplexity:10*/ | 32 | /*jshint maxstatements:33, maxcomplexity:10*/ |
33 | 33 | ||
34 | // node check | ||
35 | if ('process' in this && process.title === "node") { | ||
36 | console.error('CasperJS cannot be executed within a nodejs environment'); | ||
37 | process.exit(1); | ||
38 | } | ||
39 | |||
34 | // phantom check | 40 | // phantom check |
35 | if (!phantom) { | 41 | if (!('phantom' in this)) { |
36 | console.error('CasperJS needs to be executed in a PhantomJS environment http://phantomjs.org/'); | 42 | console.error('CasperJS needs to be executed in a PhantomJS environment http://phantomjs.org/'); |
37 | } | 43 | } |
38 | 44 | ... | ... |
... | @@ -667,7 +667,7 @@ Casper.prototype.evaluate = function evaluate(fn, context) { | ... | @@ -667,7 +667,7 @@ Casper.prototype.evaluate = function evaluate(fn, context) { |
667 | this.injectClientUtils(); | 667 | this.injectClientUtils(); |
668 | // function context | 668 | // function context |
669 | if (arguments.length === 1) { | 669 | if (arguments.length === 1) { |
670 | return this.page.evaluate(fn); | 670 | return utils.clone(this.page.evaluate(fn)); |
671 | } else if (arguments.length === 2) { | 671 | } else if (arguments.length === 2) { |
672 | // check for closure signature if it matches context | 672 | // check for closure signature if it matches context |
673 | if (utils.isObject(context) && eval(fn).length === Object.keys(context).length) { | 673 | if (utils.isObject(context) && eval(fn).length === Object.keys(context).length) { |
... | @@ -679,7 +679,7 @@ Casper.prototype.evaluate = function evaluate(fn, context) { | ... | @@ -679,7 +679,7 @@ Casper.prototype.evaluate = function evaluate(fn, context) { |
679 | // phantomjs-style signature | 679 | // phantomjs-style signature |
680 | context = [].slice.call(arguments).slice(1); | 680 | context = [].slice.call(arguments).slice(1); |
681 | } | 681 | } |
682 | return this.page.evaluate.apply(this.page, [fn].concat(context)); | 682 | return utils.clone(this.page.evaluate.apply(this.page, [fn].concat(context))); |
683 | }; | 683 | }; |
684 | 684 | ||
685 | /** | 685 | /** | ... | ... |
... | @@ -33,6 +33,7 @@ | ... | @@ -33,6 +33,7 @@ |
33 | var require = patchRequire(require); | 33 | var require = patchRequire(require); |
34 | var fs = require('fs'); | 34 | var fs = require('fs'); |
35 | var utils = require('utils'); | 35 | var utils = require('utils'); |
36 | var env = require('system').env; | ||
36 | 37 | ||
37 | exports.create = function create(type) { | 38 | exports.create = function create(type) { |
38 | "use strict"; | 39 | "use strict"; |
... | @@ -79,7 +80,7 @@ var Colorizer = function Colorizer() { | ... | @@ -79,7 +80,7 @@ var Colorizer = function Colorizer() { |
79 | * @return String | 80 | * @return String |
80 | */ | 81 | */ |
81 | this.colorize = function colorize(text, styleName, pad) { | 82 | this.colorize = function colorize(text, styleName, pad) { |
82 | if (fs.isWindows() || !(styleName in styles)) { | 83 | if ((fs.isWindows() && !env['ANSICON']) || !(styleName in styles)) { |
83 | return text; | 84 | return text; |
84 | } | 85 | } |
85 | return this.format(text, styles[styleName], pad); | 86 | return this.format(text, styles[styleName], pad); |
... | @@ -93,7 +94,7 @@ var Colorizer = function Colorizer() { | ... | @@ -93,7 +94,7 @@ var Colorizer = function Colorizer() { |
93 | * @return String | 94 | * @return String |
94 | */ | 95 | */ |
95 | this.format = function format(text, style, pad) { | 96 | this.format = function format(text, style, pad) { |
96 | if (fs.isWindows() || !utils.isObject(style)) { | 97 | if ((fs.isWindows() && !env['ANSICON']) || !utils.isObject(style)) { |
97 | return text; | 98 | return text; |
98 | } | 99 | } |
99 | var codes = []; | 100 | var codes = []; | ... | ... |
... | @@ -40,7 +40,7 @@ casper.echo("Let the match begin between \"" + (terms.join('", "')) + "\"!"); | ... | @@ -40,7 +40,7 @@ casper.echo("Let the match begin between \"" + (terms.join('", "')) + "\"!"); |
40 | casper.start("http://google.fr/"); | 40 | casper.start("http://google.fr/"); |
41 | 41 | ||
42 | casper.each(terms, function(casper, term, i) { | 42 | casper.each(terms, function(casper, term, i) { |
43 | this.echo('Fecthing score for ' + term); | 43 | this.echo('Fetching score for ' + term); |
44 | this.then(function() { | 44 | this.then(function() { |
45 | this.fill('form[action="/search"]', {q: term}, true); | 45 | this.fill('form[action="/search"]', {q: term}, true); |
46 | }); | 46 | }); | ... | ... |
... | @@ -90,3 +90,23 @@ casper.test.begin('thenEvaluate() tests', 2, function(test) { | ... | @@ -90,3 +90,23 @@ casper.test.begin('thenEvaluate() tests', 2, function(test) { |
90 | test.done(); | 90 | test.done(); |
91 | }); | 91 | }); |
92 | }); | 92 | }); |
93 | |||
94 | // https://github.com/n1k0/casperjs/issues/489 | ||
95 | // https://groups.google.com/forum/?fromgroups=#!topic/casperjs/95IgDMFnEKM | ||
96 | casper.test.begin("evaluate() returns a value which can be altered", 1, function(test) { | ||
97 | var list; | ||
98 | |||
99 | casper.start().then(function() { | ||
100 | list = this.evaluate(function() { | ||
101 | return [{a: 1}, {b: 2}]; | ||
102 | }); | ||
103 | var first = list[0]; | ||
104 | first.a = 42; | ||
105 | test.assertEquals(list, [{a: 42}, {b: 2}], | ||
106 | 'evaluate() returns a cloned value which can be altered'); | ||
107 | }); | ||
108 | |||
109 | casper.run(function() { | ||
110 | test.done(); | ||
111 | }); | ||
112 | }); | ... | ... |
-
Please register or sign in to post a comment