Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

70 lines (59 sloc) 2.626 kB
# Fix things that JSDOM doesn't do quite right.
core = require("jsdom").dom.level3.core
URL = require("url")
vm = process.binding("evals")
# Event Handling
# --------------
# Add default event behavior (click link to navigate, click button to submit
# form, etc). We start by wrapping dispatchEvent so we can forward events to
# the element's _eventDefault function (only events that did not incur
# preventDefault).
dispatchEvent = core.HTMLElement.prototype.dispatchEvent
core.HTMLElement.prototype.dispatchEvent = (event)->
outcome =, event) event unless event._preventDefault
return outcome
core.HTMLElement.prototype._eventDefault = (event)->
# Scripts
# -------
# Need to use the same context for all the scripts we load in the same document,
# otherwise simple things won't work (e.g $.xhr)
core.languageProcessors =
javascript: (element, code, filename)->
document = element.ownerDocument
window = document.parentWindow
#document._jsContext = vm.Script.createContext(window)
if window
ctx = vm.Script.createContext(window)
script = new vm.Script(code, filename)
script.runInContext ctx
catch ex
console.error "Loading #{filename}", ex.stack
# Links/Resources
# ---------------
# Default behavior for clicking on links: navigate to new URL is specified.
core.HTMLAnchorElement.prototype._eventDefault = (event)->
@ownerDocument.parentWindow.location = @href if event.type == "click" && @href
# Fix not-too-smart URL resolving in JSDOM.
core.resourceLoader.resolve = (document, path)->
path = URL.resolve(document.URL, path)
path.replace(/^file:/, '').replace(/^([\/]+)/, "/")
# Fix resource loading to keep track of in-progress requests. Need this to wait
# for all resources (mainly JavaScript) to complete loading before terminating
# browser.wait.
core.resourceLoader.load = (element, href, callback)->
document = element.ownerDocument
window = document.parentWindow
ownerImplementation = document.implementation
if ownerImplementation.hasFeature('FetchExternalResources', element.tagName.toLowerCase())
window.request { url: href, method: "GET", headers: {} }, (done)=>
url = URL.parse(@resolve(document, href))
loaded = (data, filename)->
done null, { status: 200, headers: {}, body: data.slice(0,100) } this, data, filename
if url.hostname
@download url, @enqueue(element, loaded, url.pathname)
file = @resolve(document, url.pathname)
@readFile file, @enqueue(element, loaded, file)
Jump to Line
Something went wrong with that request. Please try again.