-
-
Save brianmcd/1115903 to your computer and use it in GitHub Desktop.
JSDOM with Contextify
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/lib/jsdom/browser/index.js b/lib/jsdom/browser/index.js | |
index ed0eb80..c9c4501 100644 | |
--- a/lib/jsdom/browser/index.js | |
+++ b/lib/jsdom/browser/index.js | |
@@ -7,7 +7,7 @@ var sys = require('sys'), | |
HTMLEncode = htmlencoding.HTMLEncode, | |
HTMLDecode = htmlencoding.HTMLDecode, | |
jsdom = require('../../jsdom'), | |
- vm = require('vm'); | |
+ Contextify = require('contextify'); | |
function NOT_IMPLEMENTED(target) { | |
return function() { | |
@@ -93,10 +93,6 @@ exports.createWindow = function(dom, options) { | |
} | |
function DOMWindow(options) { | |
- this.frames = [this]; | |
- this.contentWindow = this; | |
- this.window = this.self = this.parent = this.top = this; | |
- | |
var href = (options || {}).url || 'file://' + __filename; | |
this.location = URL.parse(href); | |
this.location.reload = NOT_IMPLEMENTED(this); | |
@@ -217,9 +213,11 @@ exports.createWindow = function(dom, options) { | |
var window = new DOMWindow(options); | |
- // This is the last chance we have to properly update the window | |
- // so turn it into a context now. | |
- window = vm.createContext(window); | |
+ Contextify(window); | |
+ var global = window.getGlobal(); | |
+ window.frames = [global]; | |
+ window.window = window.contentWindow = window.self = window.parent = window.top = global; | |
+ | |
return window; | |
}; | |
@@ -498,13 +496,14 @@ var browserAugmentation = exports.browserAugmentation = function(dom, options) { | |
dom.Document.prototype.__defineGetter__('parentWindow', function() { | |
if (!this._parentWindow) { | |
- this._parentWindow = exports.windowAugmentation(dom, {document: this, url: this.URL}); | |
+ var window = exports.windowAugmentation(dom, {document: this, url: this.URL}); | |
+ this._parentWindow = window.getGlobal(); | |
} | |
return this._parentWindow; | |
}); | |
dom.Document.prototype.__defineSetter__('parentWindow', function(window) { | |
- this._parentWindow = window; | |
+ this._parentWindow = window.getGlobal(); | |
}); | |
dom.Document.prototype.__defineGetter__('defaultView', function() { | |
diff --git a/lib/jsdom/level2/languages/javascript.js b/lib/jsdom/level2/languages/javascript.js | |
index 1595918..4c683f1 100644 | |
--- a/lib/jsdom/level2/languages/javascript.js | |
+++ b/lib/jsdom/level2/languages/javascript.js | |
@@ -1,24 +1,13 @@ | |
-var vm = require('vm'); | |
- | |
exports.javascript = function(element, code, filename) { | |
var doc = element.ownerDocument, window = doc && doc.parentWindow; | |
if (window) { | |
- //document.parentWindow will be set to the global scrop within the context (so | |
- //document.parentWindow === window evaluates to true in the context), however, | |
- //when frame/iframe is created, the parent has to be set to the real context, | |
- //so remember the real context in doc.__scriptContext | |
- doc.__scriptContext = window; | |
- | |
try { | |
- vm.createScript('document.parentWindow=this;\n'+code, filename).runInContext(window); | |
+ window.run(code, filename); | |
} catch (e) { | |
element.trigger( | |
'error', 'Running ' + filename + ' failed.', | |
{error: e, filename: filename} | |
); | |
} | |
- //reset the parentWindow back to the context | |
- window.document.parentWindow = window; | |
- delete window.document.__scriptContext; | |
} | |
}; | |
diff --git a/test/jsdom/index.js b/test/jsdom/index.js | |
index d93d46c..4d527f2 100644 | |
--- a/test/jsdom/index.js | |
+++ b/test/jsdom/index.js | |
@@ -874,6 +874,23 @@ document.body.appendChild(iframe);</script></head>\ | |
test.ok(window.exposed === 42, 'read local var from window and exposed it'); | |
test.done(); | |
}); | |
- } | |
+ }, | |
+ timer_executes_in_context : function(test) { | |
+ jsdom.env('<a />', [__dirname + '/files/timer_in_context.js'],function (errors, window) { | |
+ setTimeout(function () { | |
+ test.ok(window.x == 1); | |
+ test.done(); | |
+ }, 1); | |
+ }); | |
+ }, | |
+ | |
+ // see: https://github.com/tmpvar/jsdom/issues/250 | |
+ issue_250 : function(test) { | |
+ jsdom.env('<a />', [__dirname + '/files/250.js'], function(errors, window) { | |
+ test.ok(window.checkIdentity); | |
+ test.ok(window.checkIdentity2); | |
+ test.done(); | |
+ }); | |
+ } | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
level1/core 529/529 100% | |
level1/html 238/238 100% | |
level1/svg 527/527 100% | |
level2/core 283/283 100% | |
level2/html 687/687 100% | |
level2/style 3/3 100% | |
level2/extra 4/4 100% | |
level3/xpath 93/93 100% | |
window 2/2 100% | |
sizzle/index 12/15 80% | |
jsdom/index 54/54 100% | |
------------------------------------- | |
TOTALS: 3/2435 failed; 99% success | |
TIME: 12417ms |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x = 0; | |
setTimeout(function () { | |
x = 1; | |
}, 0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment