Commit 56dce225 56dce2253f01775fda33af14ef742048289a2ada by Nicolas Perriault

fix #489 - evaluate() should return a result which can be altered

1 parent 3b7575e3
......@@ -667,7 +667,7 @@ Casper.prototype.evaluate = function evaluate(fn, context) {
this.injectClientUtils();
// function context
if (arguments.length === 1) {
return this.page.evaluate(fn);
return utils.clone(this.page.evaluate(fn));
} else if (arguments.length === 2) {
// check for closure signature if it matches context
if (utils.isObject(context) && eval(fn).length === Object.keys(context).length) {
......@@ -679,7 +679,7 @@ Casper.prototype.evaluate = function evaluate(fn, context) {
// phantomjs-style signature
context = [].slice.call(arguments).slice(1);
}
return this.page.evaluate.apply(this.page, [fn].concat(context));
return utils.clone(this.page.evaluate.apply(this.page, [fn].concat(context)));
};
/**
......
......@@ -90,3 +90,23 @@ casper.test.begin('thenEvaluate() tests', 2, function(test) {
test.done();
});
});
// https://github.com/n1k0/casperjs/issues/489
// https://groups.google.com/forum/?fromgroups=#!topic/casperjs/95IgDMFnEKM
casper.test.begin("evaluate() returns a value which can be altered", 1, function(test) {
var list;
casper.start().then(function() {
list = this.evaluate(function() {
return [{a: 1}, {b: 2}];
});
var first = list[0];
first.a = 42;
test.assertEquals(list, [{a: 42}, {b: 2}],
'evaluate() returns a cloned value which can be altered');
});
casper.run(function() {
test.done();
});
});
......