Skip to content
This repository has been archived by the owner on Dec 16, 2023. It is now read-only.

Commit

Permalink
Fixed issue 13: Zombie now shares global variables between scripts.
Browse files Browse the repository at this point in the history
  • Loading branch information
assaf committed Dec 30, 2010
1 parent 71ad9f8 commit 3916a43
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
zombie.js-changelog(7) -- Changelog
===================================

### Version 0.8.3 2010-12-30

Zombie now shares global variables between scripts.

199 Tests
2.4 sec to complete


### Version 0.8.2 2010-12-30

Expand Down
12 changes: 12 additions & 0 deletions spec/browser-spec.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ brains.get "/script/append", (req, res)-> res.send """

brains.get "/useragent", (req, res)-> res.send "<body>#{req.headers["user-agent"]}</body>"

brains.get "/context", (req, res)-> res.send """
<script>var foo = 1;</script>
<script>foo = foo + 1;</script>
<script>document.title = foo;</script>
"""


vows.describe("Browser").addBatch(
"open page":
Expand Down Expand Up @@ -228,4 +234,10 @@ vows.describe("Browser").addBatch(
"should resolve URL": (browser)-> assert.equal browser.location.href, "http://localhost:3003"
"should load page": (browser)-> assert.equal browser.text("title"), "Tap, Tap"


"script context":
zombie.wants "http://localhost:3003/context"
"should be shared by all scripts": (browser)-> assert.equal browser.text("title"), "2"


).export(module)
3 changes: 3 additions & 0 deletions src/zombie/history.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class History
resource = (url, method, data, enctype)=>
method = (method || "GET").toUpperCase()
throw new Error("Cannot load resource: #{URL.format(url)}") unless url.protocol && url.hostname
# If the browser has a new window, use it. If a document was already
# loaded into that window it would have state information we don't want
# (e.g. window.$) so open a new window.
window = browser.window
window = browser.open() if browser.window.document
# Create new DOM Level 3 document, add features (load external
Expand Down
15 changes: 11 additions & 4 deletions src/zombie/jsdom_patches.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,24 @@ core.resourceLoader.download = (url, callback)->
# 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
window.browser.log -> "Running script from #{filename}" if filename
if window
ctx = vm.Script.createContext(window)
# Unfortunately, using the same context in multiple scripts
# doesn't agree with jQuery, Sammy and other scripts I tested,
# so each script gets a new context.
context = vm.Script.createContext(window)
# But we need to carry global variables from one script to the
# next, so we're going to store them in window._vars and add them
# back to the new context.
if window._vars
context[v[0]] = v[1] for v in window._vars
script = new vm.Script(code, filename)
script.runInContext ctx
script.runInContext context
window._vars = ([n,v] for n, v of context).filter((v)-> !window[v[0]])

# DOMCharacterDataModified event fired when text is added to a
# TextNode. This is a crappy implementation, a good one would old and
Expand Down

0 comments on commit 3916a43

Please sign in to comment.