Permalink
Browse files

[api] Updated arguments magic in jsdom.jQueryify

  • Loading branch information...
2 parents d824d63 + 82bccb8 commit 6b4fa23b43c82cec696b9acb53845fc978a57abd @indexzero indexzero committed Aug 12, 2010
Showing with 36 additions and 25 deletions.
  1. +1 −0 .gitignore
  2. +6 −17 example/jquery/run.js
  3. +17 −6 lib/jsdom.js
  4. +12 −2 test/jsdom/index.js
View
@@ -0,0 +1 @@
+.DS_Store
View
@@ -1,21 +1,10 @@
var sys = require("sys"),
- jsdom = require(__dirname + "/../../lib/jsdom").jsdom,
- window = jsdom().createWindow(),
- head = window.document.getElementsByTagName('head')[0],
- jQueryTag = window.document.createElement("script");
-
-jQueryTag.src = "file://" + __dirname + "/jquery.js";
+ jsdom = require(__dirname + "/../../lib/jsdom"),
+ window = jsdom.jsdom().createWindow();
// this also works:
// jQueryTag.src = "http://code.jquery.com/jquery-1.4.2.js";
-
-head.appendChild(jQueryTag);
-
-jQueryTag.onload = function() {
- if (this.readyState === 'complete') {
- // jQuery is ready!
- window.jQuery('body').append("<div class='testing'>Hello World, It works!</div>");
- sys.puts(window.jQuery(".testing").text());
- }
-};
-
+jsdom.jQueryify(window, __dirname + "/jquery.js", function() {
+ window.jQuery('body').append("<div class='testing'>Hello World, It works!</div>");
+ sys.puts(window.jQuery(".testing").text());
+});
View
@@ -18,18 +18,29 @@ exports.jsdom = function (level) {
exports.createWindow = function (html, level) {
var window = exports.jsdom(level).createWindow();
window.document.innerHTML = html;
-
return window;
};
-exports.jQueryify = function (window, path) {
- var head = window.document.getElementsByTagName('head')[0],
+exports.jQueryify = function (window, /* path [optional], callback */) {
+ var args = Array.prototype.slice.call(arguments),
+ callback = (typeof(args[args.length - 1]) === 'function') && args.pop(),
+ path = undefined,
+ head = window.document.getElementsByTagName('head')[0],
jQueryTag = window.document.createElement("script");
+
+ if (args.length > 1 && typeof(args[1] === 'string')) {
+ path = args[1];
+ }
path = path ? "file://" + path : 'http://code.jquery.com/jquery-1.4.2.js';
+
jQueryTag.src = path;
-
- head.appendChild(jQueryTag);
- return window;
+ jQueryTag.onload = function() {
+ if (this.readyState === 'complete') {
+ if (typeof fn === "function") {
+ fn();
+ }
+ }
+ };
};
View
@@ -2,8 +2,18 @@ var sys = require("sys");
exports.tests = {
build_window : function() {
- sys.debug(sys.inspect(jsdom));
var window = jsdom.jsdom().createWindow();
assertNotNull("window must be a new object!", window);
- }
+ },
+
+ /*
+ Async tests and mjsunit dont play well..
+ jquerify : function() {
+ var window = jsdom.jQueryify("<html><head></head><body><p>it works</p></body></html>", false, false,
+ function() {
+ console.dir(window);
+ assertEqual("jquery selectors should work at this point", window.jQuery("p").text(), "it works");
+ });
+ }*/
+
};

0 comments on commit 6b4fa23

Please sign in to comment.