Permalink
Browse files

Merge branch 'master' of github.com:tmpvar/jsdom

  • Loading branch information...
2 parents 3f9410f + 2264e2e commit e5728a8edf60126855b63147eb60edbf6f5cc33e @tmpvar committed Aug 12, 2010
Showing with 60 additions and 24 deletions.
  1. +1 −0 .gitignore
  2. +6 −17 example/jquery/run.js
  3. +36 −4 lib/jsdom.js
  4. +5 −1 package.json
  5. +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().makeWindow(),
- 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
@@ -4,13 +4,45 @@ exports.defaultLevel = dom.level1.core;
exports.browserAugmentation = require("./jsdom/browser").browserAugmentation;
exports.windowAugmentation = require("./jsdom/browser").windowAugmentation;
-exports.jsdom = function(level) {
+exports.jsdom = function (level) {
level = level || exports.defaultLevel;
var doc = new (level.Document)();
- doc.makeWindow = function() {
- var window = exports.windowAugmentation(level, {document: doc});
- delete window.document.makeWindow;
+ doc.createWindow = function() {
+ var window = exports.windowAugmentation(level, { document: doc });
+ delete window.document.createWindow;
return window;
}
return doc;
}
+
+exports.createWindow = function (html, level) {
+ var window = exports.jsdom(level).createWindow();
+ window.document.innerHTML = html;
+ return window;
+};
+
+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;
+
+ jQueryTag.onload = function() {
+ if (this.readyState === 'complete') {
+ if (callback) {
+ callback(window, window.jQuery);
+ }
+ }
+ };
+
+ return window;
+};
View
@@ -1,6 +1,6 @@
{
"name": "jsdom",
- "version": "0.1.5",
+ "version": "0.1.6",
"description": "CommonJS implementation of the DOM intended to be platform independent and as minimal/light as possible while completely adhering to the w3c DOM specifications.",
"keywords": [
"dom",
@@ -21,6 +21,10 @@
{
"name": "Dav Glass",
"email": "davglass@gmail.com"
+ },
+ {
+ "name" : "Charlie Robins",
+ "email": "charlie.robbins@gmail.com"
}
],
"bugs": {
View
@@ -2,8 +2,18 @@ var sys = require("sys");
exports.tests = {
build_window : function() {
- sys.debug(sys.inspect(jsdom));
- var window = jsdom.jsdom().makeWindow();
+ var window = jsdom.jsdom().createWindow();
assertNotNull("window must be a new object!", window);
+ },
+
+ jquerify : function() {
+ jsdom.jQueryify(jsdom.jsdom().createWindow(),
+ __dirname + "/../../example/jquery/jquery.js",
+ function(window, jQuery)
+ {
+ assertNotNull("jQuery should be attached to the window", window.jQuery.find);
+ assertNotNull("jQuery should be attached to the window", jQuery.find);
+ });
}
+
};

0 comments on commit e5728a8

Please sign in to comment.