Skip to content

Commit

Permalink
fix #489 - evaluate() should return a result which can be altered
Browse files Browse the repository at this point in the history
  • Loading branch information
n1k0 committed May 27, 2013
1 parent 3b7575e commit 56dce22
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions modules/casper.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)));
};

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/suites/casper/evaluate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});

0 comments on commit 56dce22

Please sign in to comment.