From 81794f9096c3934f41e54bd09f8942994698d8fc Mon Sep 17 00:00:00 2001 From: Ariya Hidayat Date: Tue, 10 Apr 2012 23:52:56 -0700 Subject: [PATCH] Squashed commit of the following: commit c373ac4d17814588f4e3344f634ec469e56c0303 Author: Danny Wang Date: Tue Apr 10 12:38:13 2012 +0800 moved i and l delarations to the top of page.evaluate() commit bf24d4d1ecdb9e06c7bf461e87c222b10b74bc9d Author: Danny Wang Date: Tue Apr 10 08:54:55 2012 +0800 fixed defects in evaluate() pointed out by detro commit 0bb8cff7803b70fe60fd761b1b748b5510705ee0 Author: Danny Wang 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 --- src/modules/webpage.js | 24 +++++++++++++++++ src/webpage.cpp | 2 +- src/webpage.h | 2 +- test/webpage-spec.js | 59 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+), 2 deletions(-) diff --git a/src/modules/webpage.js b/src/modules/webpage.js index 47c1e82563..0678107234 100644 --- a/src/modules/webpage.js +++ b/src/modules/webpage.js @@ -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); diff --git a/src/webpage.cpp b/src/webpage.cpp index 7bc3784680..426a617322 100644 --- a/src/webpage.cpp +++ b/src/webpage.cpp @@ -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()")); diff --git a/src/webpage.h b/src/webpage.h index c9dcacaccc..f6b0a80576 100644 --- a/src/webpage.h +++ b/src/webpage.h @@ -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); diff --git a/test/webpage-spec.js b/test/webpage-spec.js index 07ddea37b4..f2048fd97e 100644 --- a/test/webpage-spec.js +++ b/test/webpage-spec.js @@ -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 () {