Permalink
Browse files

Added getters for all of the WebPage signal//callback handlers.

  • Loading branch information...
1 parent a777797 commit c9f9b5a14b2ef17b14eac065f81af3dfadaac2e5 @JamesMGreene JamesMGreene committed Dec 6, 2012
Showing with 88 additions and 32 deletions.
  1. +50 −28 src/modules/webpage.js
  2. +38 −4 test/webpage-spec.js
@@ -8,6 +8,7 @@
Copyright (C) 2011 Ivan De Marino <ivan.de.marino@gmail.com>
Copyright (C) 2011 James Roe <roejames12@hotmail.com>
Copyright (C) 2011 execjosh, http://execjosh.blogspot.com
+ Copyright (C) 2012 James M. Greene <james.m.greene@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
@@ -88,56 +89,77 @@ function copyInto(target, source) {
return target;
}
-function definePageSignalSetter(page, handlers, handlerName, signalName) {
+function definePageSignalHandler(page, handlers, handlerName, signalName) {
page.__defineSetter__(handlerName, function (f) {
// Disconnect previous handler (if any)
- if (handlers && typeof(handlers[signalName]) === "function") {
+ if (!!handlers[handlerName] && typeof handlers[handlerName].callback === "function") {
try {
- this[signalName].disconnect(handlers[signalName]);
+ this[signalName].disconnect(handlers[handlerName].callback);
} catch (e) {}
}
// Delete the previous handler
- delete handlers[signalName];
+ delete handlers[handlerName];
// Connect the new handler iff it's a function
- if (typeof(f) === "function") {
+ if (typeof f === "function") {
// Store the new handler for reference
- handlers[signalName] = f;
+ handlers[handlerName] = {
+ callback: f
+ }
this[signalName].connect(f);
}
});
+
+ page.__defineGetter__(handlerName, function() {
+ return !!handlers[handlerName] && typeof handlers[handlerName].callback === "function" ?
+ handlers[handlerName].callback :
+ undefined;
+ });
}
-function definePageCallbackSetter(page, handlers, handlerName, callbackConstructor) {
+function definePageCallbackHandler(page, handlers, handlerName, callbackConstructor) {
page.__defineSetter__(handlerName, function(f) {
// Fetch the right callback object
var callbackObj = page[callbackConstructor]();
// Disconnect previous handler (if any)
- if (handlers && typeof(handlers[callbackConstructor]) === "function") {
+ var handlerObj = handlers[handlerName];
+ if (!!handlerObj && typeof handlerObj.callback === "function" && typeof handlerObj.connector === "function") {
try {
- callbackObj.called.disconnect(handlers[callbackConstructor]);
+ callbackObj.called.disconnect(handlerObj.connector);
} catch (e) {
console.log(e);
}
}
// Delete the previous handler
- delete handlers[callbackConstructor];
+ delete handlers[handlerName];
// Connect the new handler iff it's a function
- if (typeof(f) === "function") {
+ if (typeof f === "function") {
// Store the new handler for reference
- handlers[callbackConstructor] = function() {
+ var connector = function() {
// Callback will receive a "deserialized", normal "arguments" array
callbackObj.returnValue = f.apply(this, arguments[0]);
};
+
+ handlers[handlerName] = {
+ callback: f,
+ connector: connector
+ };
// Connect a new handler
- callbackObj.called.connect(handlers[callbackConstructor]);
+ callbackObj.called.connect(connector);
}
});
+
+ page.__defineGetter__(handlerName, function() {
+ var handlerObj = handlers[handlerName];
+ return (!!handlerObj && typeof handlerObj.callback === "function" && typeof handlerObj.connector === "function") ?
+ handlers[handlerName].callback :
+ undefined;
+ });
}
// Inspired by Douglas Crockford's remedies: proper String quoting.
@@ -216,28 +238,28 @@ function decorateNewPage(opts, page) {
// deep copy
page.settings = JSON.parse(JSON.stringify(phantom.defaultPageSettings));
- definePageSignalSetter(page, handlers, "onInitialized", "initialized");
+ definePageSignalHandler(page, handlers, "onInitialized", "initialized");
- definePageSignalSetter(page, handlers, "onLoadStarted", "loadStarted");
+ definePageSignalHandler(page, handlers, "onLoadStarted", "loadStarted");
- definePageSignalSetter(page, handlers, "onLoadFinished", "loadFinished");
+ definePageSignalHandler(page, handlers, "onLoadFinished", "loadFinished");
- definePageSignalSetter(page, handlers, "onUrlChanged", "urlChanged");
+ definePageSignalHandler(page, handlers, "onUrlChanged", "urlChanged");
- definePageSignalSetter(page, handlers, "onNavigationRequested", "navigationRequested");
+ definePageSignalHandler(page, handlers, "onNavigationRequested", "navigationRequested");
- definePageSignalSetter(page, handlers, "onResourceRequested", "resourceRequested");
+ definePageSignalHandler(page, handlers, "onResourceRequested", "resourceRequested");
- definePageSignalSetter(page, handlers, "onResourceReceived", "resourceReceived");
+ definePageSignalHandler(page, handlers, "onResourceReceived", "resourceReceived");
- definePageSignalSetter(page, handlers, "onAlert", "javaScriptAlertSent");
+ definePageSignalHandler(page, handlers, "onAlert", "javaScriptAlertSent");
- definePageSignalSetter(page, handlers, "onConsoleMessage", "javaScriptConsoleMessageSent");
+ definePageSignalHandler(page, handlers, "onConsoleMessage", "javaScriptConsoleMessageSent");
- definePageSignalSetter(page, handlers, "onClosing", "closing");
+ definePageSignalHandler(page, handlers, "onClosing", "closing");
// Private callback for "page.open()"
- definePageSignalSetter(page, handlers, "_onPageOpenFinished", "loadFinished");
+ definePageSignalHandler(page, handlers, "_onPageOpenFinished", "loadFinished");
phantom.__defineErrorSetter__(page, page);
@@ -416,18 +438,18 @@ function decorateNewPage(opts, page) {
}
// Calls from within the page to "phantomCallback()" arrive to this handler
- definePageCallbackSetter(page, handlers, "onCallback", "_getGenericCallback");
+ definePageCallbackHandler(page, handlers, "onCallback", "_getGenericCallback");
// Calls arrive to this handler when the user is asked to pick a file
- definePageCallbackSetter(page, handlers, "onFilePicker", "_getFilePickerCallback");
+ definePageCallbackHandler(page, handlers, "onFilePicker", "_getFilePickerCallback");
// Calls from within the page to "window.confirm(message)" arrive to this handler
// @see https://developer.mozilla.org/en/DOM/window.confirm
- definePageCallbackSetter(page, handlers, "onConfirm", "_getJsConfirmCallback");
+ definePageCallbackHandler(page, handlers, "onConfirm", "_getJsConfirmCallback");
// Calls from within the page to "window.prompt(message, defaultValue)" arrive to this handler
// @see https://developer.mozilla.org/en/DOM/window.prompt
- definePageCallbackSetter(page, handlers, "onPrompt", "_getJsPromptCallback");
+ definePageCallbackHandler(page, handlers, "onPrompt", "_getJsPromptCallback");
page.event = {};
page.event.key = {
View
@@ -102,10 +102,44 @@ describe("WebPage object", function() {
expect(page).toNotEqual(null);
});
+ it("should be able to get any signal handler that are currently set on it", function() {
+ page.onInitialized = undefined;
+ expect(page.onInitialized).toBeUndefined();
+ var onInitialized1 = function() { var x = "x"; };
+ page.onInitialized = onInitialized1;
+ expect(page.onInitialized).toEqual(onInitialized1);
+ var onInitialized2 = function() { var y = "y"; };
+ page.onInitialized = onInitialized2;
+ expect(page.onInitialized).toEqual(onInitialized2);
+ expect(page.onInitialized).toNotEqual(onInitialized1);
+ page.onInitialized = null;
+ // Will only allow setting to a function value, so setting it to `null` returns `undefined`
+ expect(page.onInitialized).toBeUndefined();
+ page.onInitialized = undefined;
+ expect(page.onInitialized).toBeUndefined();
+ });
+
+ it("should be able to get any callback handler that are currently set on it", function() {
+ page.onConfirm = undefined;
+ expect(page.onConfirm).toBeUndefined();
+ var onConfirmFunc1 = function() { return !"x"; };
+ page.onConfirm = onConfirmFunc1;
+ expect(page.onConfirm).toEqual(onConfirmFunc1);
+ var onConfirmFunc2 = function() { return !!"y"; };
+ page.onConfirm = onConfirmFunc2;
+ expect(page.onConfirm).toEqual(onConfirmFunc2);
+ expect(page.onConfirm).toNotEqual(onConfirmFunc1);
+ page.onConfirm = null;
+ // Will only allow setting to a function value, so setting it to `null` returns `undefined`
+ expect(page.onConfirm).toBeUndefined();
+ page.onConfirm = undefined;
+ expect(page.onConfirm).toBeUndefined();
+ });
+
checkPageCallback(page);
checkPageConfirm(page);
checkPagePrompt(page);
-
+
checkClipRect(page, {height:0,left:0,top:0,width:0});
expectHasPropertyString(page, 'content');
@@ -121,7 +155,7 @@ describe("WebPage object", function() {
expectHasProperty(page, 'paperSize');
it("should have paperSize as an empty object", function() {
- expect(page.paperSize).toEqual({});
+ expect(page.paperSize).toEqual({});
});
checkScrollPosition(page, {left:0,top:0});
@@ -134,12 +168,12 @@ describe("WebPage object", function() {
expectHasProperty(page, 'customHeaders');
it("should have customHeaders as an empty object", function() {
- expect(page.customHeaders).toEqual({});
+ expect(page.customHeaders).toEqual({});
});
expectHasProperty(page, 'zoomFactor');
it("should have zoomFactor of 1", function() {
- expect(page.zoomFactor).toEqual(1.0);
+ expect(page.zoomFactor).toEqual(1.0);
});
expectHasProperty(page, 'event');

0 comments on commit c9f9b5a

Please sign in to comment.