Skip to content
This repository
Browse code

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

  • Loading branch information...
commit e5728a8edf60126855b63147eb60edbf6f5cc33e 2 parents 3f9410f + 2264e2e
Elijah Insua authored August 12, 2010
1  .gitignore
... ...
@@ -0,0 +1 @@
  1
+.DS_Store
23  example/jquery/run.js
... ...
@@ -1,21 +1,10 @@
1 1
 var sys    = require("sys"),
2  
-    jsdom  = require(__dirname + "/../../lib/jsdom").jsdom,
3  
-    window = jsdom().makeWindow(),
4  
-    head   = window.document.getElementsByTagName('head')[0],
5  
-    jQueryTag = window.document.createElement("script");
6  
-
7  
-jQueryTag.src = "file://" + __dirname + "/jquery.js";
  2
+    jsdom  = require(__dirname + "/../../lib/jsdom"),
  3
+    window = jsdom.jsdom().createWindow();
8 4
 
9 5
 // this also works:
10 6
 // jQueryTag.src = "http://code.jquery.com/jquery-1.4.2.js";
11  
-
12  
-head.appendChild(jQueryTag);
13  
-
14  
-jQueryTag.onload = function() {
15  
-  if (this.readyState === 'complete') {
16  
-    // jQuery is ready!
17  
-    window.jQuery('body').append("<div class='testing'>Hello World, It works!</div>");
18  
-    sys.puts(window.jQuery(".testing").text());
19  
-  }
20  
-};
21  
-
  7
+jsdom.jQueryify(window, __dirname + "/jquery.js", function() {
  8
+  window.jQuery('body').append("<div class='testing'>Hello World, It works!</div>");
  9
+  sys.puts(window.jQuery(".testing").text());
  10
+});
40  lib/jsdom.js
@@ -4,13 +4,45 @@ exports.defaultLevel = dom.level1.core;
4 4
 exports.browserAugmentation = require("./jsdom/browser").browserAugmentation;
5 5
 exports.windowAugmentation = require("./jsdom/browser").windowAugmentation;
6 6
 
7  
-exports.jsdom = function(level) {
  7
+exports.jsdom = function (level) {
8 8
   level = level || exports.defaultLevel;
9 9
   var doc = new (level.Document)();
10  
-  doc.makeWindow =  function() {
11  
-    var window = exports.windowAugmentation(level, {document: doc});
12  
-    delete window.document.makeWindow;
  10
+  doc.createWindow =  function() {
  11
+    var window = exports.windowAugmentation(level, { document: doc });
  12
+    delete window.document.createWindow;
13 13
     return window;
14 14
   }
15 15
   return doc;
16 16
 }
  17
+
  18
+exports.createWindow = function (html, level) {
  19
+  var window = exports.jsdom(level).createWindow();
  20
+  window.document.innerHTML = html;
  21
+  return window;
  22
+};
  23
+
  24
+exports.jQueryify = function (window /* path [optional], callback */) {
  25
+  var args = Array.prototype.slice.call(arguments),
  26
+      callback = (typeof(args[args.length - 1]) === 'function') && args.pop(),
  27
+      path = undefined,
  28
+      head   = window.document.getElementsByTagName('head')[0],
  29
+      jQueryTag = window.document.createElement("script");
  30
+      
  31
+  if (args.length > 1 && typeof(args[1] === 'string')) {
  32
+    path = args[1];
  33
+  }
  34
+  
  35
+  path = path ? "file://" + path : 'http://code.jquery.com/jquery-1.4.2.js';
  36
+  
  37
+  jQueryTag.src = path;
  38
+  
  39
+  jQueryTag.onload = function() {
  40
+    if (this.readyState === 'complete') {
  41
+      if (callback) {
  42
+        callback(window, window.jQuery);
  43
+      }
  44
+    }
  45
+  };
  46
+  
  47
+  return window;
  48
+};
6  package.json
... ...
@@ -1,6 +1,6 @@
1 1
 {
2 2
     "name": "jsdom",
3  
-    "version": "0.1.5",
  3
+    "version": "0.1.6",
4 4
     "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.",
5 5
     "keywords": [
6 6
         "dom",
@@ -21,6 +21,10 @@
21 21
         {
22 22
             "name": "Dav Glass",
23 23
             "email": "davglass@gmail.com"
  24
+        },
  25
+        {
  26
+            "name" : "Charlie Robins",
  27
+            "email": "charlie.robbins@gmail.com"
24 28
         }
25 29
     ],
26 30
     "bugs": {
14  test/jsdom/index.js
@@ -2,8 +2,18 @@ var sys = require("sys");
2 2
 exports.tests = {
3 3
 
4 4
   build_window : function() {
5  
-    sys.debug(sys.inspect(jsdom));
6  
-    var window = jsdom.jsdom().makeWindow();
  5
+    var window = jsdom.jsdom().createWindow();
7 6
     assertNotNull("window must be a new object!", window);
  7
+  },
  8
+  
  9
+  jquerify : function() {
  10
+    jsdom.jQueryify(jsdom.jsdom().createWindow(), 
  11
+                    __dirname + "/../../example/jquery/jquery.js", 
  12
+                    function(window, jQuery) 
  13
+    {
  14
+        assertNotNull("jQuery should be attached to the window", window.jQuery.find);
  15
+        assertNotNull("jQuery should be attached to the window", jQuery.find);
  16
+    });
8 17
   }
  18
+  
9 19
 };

0 notes on commit e5728a8

Please sign in to comment.
Something went wrong with that request. Please try again.