Skip to content
This repository has been archived by the owner on May 30, 2023. It is now read-only.

Commit

Permalink
Squashed commit of the following:
Browse files Browse the repository at this point in the history
commit c373ac4
Author: Danny Wang <wangyang0123@gmail.com>
Date:   Tue Apr 10 12:38:13 2012 +0800

    moved i and l delarations to the top of page.evaluate()

commit bf24d4d
Author: Danny Wang <wangyang0123@gmail.com>
Date:   Tue Apr 10 08:54:55 2012 +0800

    fixed defects in evaluate() pointed out by detro

commit 0bb8cff
Author: Danny Wang <wangyang0123@gmail.com>
Date:   Fri Apr 6 19:21:47 2012 +0800

    added passing variables to function for page.evaluate

    http://code.google.com/p/phantomjs/issues/detail?id=132
  • Loading branch information
ariya committed Apr 11, 2012
1 parent afb0707 commit 81794f9
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 2 deletions.
24 changes: 24 additions & 0 deletions src/modules/webpage.js
Expand Up @@ -187,6 +187,30 @@ exports.create = function (opts) {
this._appendScriptElement(scriptUrl);
};

/**
* evaluate a function in the page
* @param {function} func the function to evaluate
* @param {...} args function arguments
* @return {*} the function call result
*/
page.evaluate = function (func, args) {
var str, arg, i, l;
if (!(func instanceof Function || typeof func === 'string' || func instanceof String)) {
throw "Wrong use of WebPage#evaluate";
}
str = 'function() { return (' + func.toString() + ')(';
for (i = 1, l = arguments.length; i < l; i++) {
arg = arguments[i];
if (/object|string/.test(typeof arg) && !(arg instanceof RegExp)) {
str += 'JSON.parse(' + JSON.stringify(JSON.stringify(arg)) + '),';
} else {
str += arg + ',';
}
}
str = str.replace(/,$/, '') + '); }';
return this.evaluateJavaScript(str);
}

// Copy options into page
if (opts) {
page = copyInto(page, opts);
Expand Down
2 changes: 1 addition & 1 deletion src/webpage.cpp
Expand Up @@ -291,7 +291,7 @@ QVariantMap WebPage::paperSize() const
return m_paperSize;
}

QVariant WebPage::evaluate(const QString &code)
QVariant WebPage::evaluateJavaScript(const QString &code)
{
QString function = "(" + code + ")()";
return m_mainFrame->evaluateJavaScript(function, QString("phantomjs://webpage.evaluate()"));
Expand Down
2 changes: 1 addition & 1 deletion src/webpage.h
Expand Up @@ -89,7 +89,7 @@ public slots:
void openUrl(const QString &address, const QVariant &op, const QVariantMap &settings);
void release();

QVariant evaluate(const QString &code);
QVariant evaluateJavaScript(const QString &code);
bool render(const QString &fileName);
bool injectJs(const QString &jsFilePath);
void _appendScriptElement(const QString &scriptUrl);
Expand Down
59 changes: 59 additions & 0 deletions test/webpage-spec.js
Expand Up @@ -323,6 +323,65 @@ describe("WebPage object", function() {
});

});

it("should pass variables to functions properly", function() {
var testPrimitiveArgs = function() {
var samples = [
true,
0,
"`~!@#$%^&*()_+-=[]\{}|;':\",./<>?",
undefined,
null
];
for (var i = 0; i < samples.length; i++) {
if (samples[i] !== arguments[i]) {
console.log("FAIL");
}
}
};

var testComplexArgs = function() {
var samples = [
{a:true, b:0, c:"string"},
function() { return true; },
[true, 0, "string"],
/\d+\w*\//
];
for (var i = 0; i < samples.length; i++) {
if (typeof samples[i] !== typeof arguments[i]
|| samples[i].toString() !== arguments[i].toString()) {
console.log("FAIL");
}
}
};

var message;
runs(function() {
page.onConsoleMessage = function (msg) {
message = msg;
}
});

waits(0);

runs(function() {
page.evaluate(function() {
console.log("PASS");
});
page.evaluate(testPrimitiveArgs,
true,
0,
"`~!@#$%^&*()_+-=[]\{}|;':\",./<>?",
undefined,
null);
page.evaluate(testComplexArgs,
{a:true, b:0, c:"string"},
function() { return true; },
[true, 0, "string"],
/\d+\w*\//);
expect(message).toEqual("PASS");
});
});
});

describe("WebPage construction with options", function () {
Expand Down

0 comments on commit 81794f9

Please sign in to comment.