Skip to content

Commit

Permalink
Fix some things that broke accidentally
Browse files Browse the repository at this point in the history
  • Loading branch information
assaf committed May 4, 2012
1 parent 06a7fd6 commit 9023ba7
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 38 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Expand Up @@ -17,6 +17,9 @@ Fixed IFrame document and window to include Zombie enhancements.

Zombie can now show FB Connect form.

453 tests
10.0 sec to complete


## Version 0.13.6 2012-05-02

Expand Down
3 changes: 2 additions & 1 deletion lib/zombie/history.coffee
Expand Up @@ -125,8 +125,9 @@ class History
referer = @_stack[@_index-1]?.url?.href || @_browser.referer
headers["referer"] = referer if referer

Path = require("path")
if url.protocol == "file:"
url.pathname = "/#{url.host}/#{url.pathname}"
url = URL.format(protocol: "file:", host: "", pathname: "/#{url.hostname}#{url.pathname}")

if credentials = @_browser.credentials
switch credentials.scheme.toLowerCase()
Expand Down
12 changes: 6 additions & 6 deletions lib/zombie/jsdom_patches.coffee
Expand Up @@ -40,15 +40,15 @@ HTML.resourceLoader.load = (element, href, callback)->
window.browser.resources.get url, @enqueue(element, loaded, url.pathname)
else
url = URL.parse(@resolve(document, href))
if url.hostname
loaded = (response, filename)->
callback.call this, response.body, URL.parse(response.url).pathname
window.browser.resources.get url, @enqueue(element, loaded, url.pathname)
else
if url.protocol == "file:"
loaded = (data, filename)->
callback.call this, data, filename
file = @resolve(document, url.pathname)
file = "/#{url.hostname}#{url.pathname}"
@readFile file, @enqueue(element, loaded, file)
else
loaded = (response, filename)->
callback.call this, response.body, URL.parse(response.url).pathname
window.browser.resources.get url, @enqueue(element, loaded, url.pathname)


# Support for iframes that load content when you set the src attribute.
Expand Down
26 changes: 14 additions & 12 deletions lib/zombie/windows.coffee
Expand Up @@ -78,6 +78,7 @@ class Windows
# This actually handles creation of a new window.
_create: ({ name, parent, opener })->
window = JSDOM.createWindow(HTML)
global = window.getGlobal()
@_stack.push window

Object.defineProperty window, "browser", value: @_browser
Expand All @@ -90,15 +91,15 @@ class Windows
Object.defineProperty window, "name", value: name || ""
# If this is an iframe within a parent window
if parent
Object.defineProperty window, "parent", value: parent
Object.defineProperty window, "top", value: parent.top
Object.defineProperty window, "parent", value: parent.getGlobal()
Object.defineProperty window, "top", value: parent.top.getGlobal()
else
Object.defineProperty window, "parent", value: window
Object.defineProperty window, "top", value: window
Object.defineProperty window, "parent", value: window.getGlobal()
Object.defineProperty window, "top", value: window.getGlobal()
# Each window maintains its own history
Object.defineProperty window, "history", value: new History(window)
# If this was opened from another window
Object.defineProperty window, "opener", value: opener
Object.defineProperty window, "opener", value: opener?.getGlobal()

# Window title is same as document title
window.__defineGetter__ "title", ->
Expand All @@ -108,7 +109,8 @@ class Windows

# Present in browsers, not in spec Used by Google Analytics see
# https://developer.mozilla.org/en/DOM/window.navigator.javaEnabled
Object.defineProperty window.navigator, "javaEnabled", value: false
window.navigator.javaEnabled = ->
return false

# Add cookies, storage, alerts/confirm, XHR, WebSockets, JSON, Screen, etc
@_browser._cookies.extend window
Expand Down Expand Up @@ -145,8 +147,8 @@ class Windows
event = document.createEvent("MessageEvent")
event.initEvent "message", false, false
event.data = data
event.source = global.window
origin = global.window.location
event.source = Windows.inContext
origin = event.source.location
event.origin = URL.format(protocol: origin.protocol, host: origin.host)
process.nextTick ->
eventloop.dispatch window, event
Expand All @@ -156,13 +158,13 @@ class Windows
# Evaulate in context of window. This can be called with a script (String) or a function.
window._evaluate = (code, filename)->
try
global.window = window # the current window, postMessage needs this
Windows.inContext = window # the current window, postMessage needs this
if typeof code == "string" || code instanceof String
window.run code, filename
global.run code, filename
else
code.call window
code.call global
finally
global.window = null
Windows.inContext = null

# Default onerror handler.
window.onerror = (event)=>
Expand Down
2 changes: 1 addition & 1 deletion test/browser_test.coffee
Expand Up @@ -469,7 +469,7 @@ describe "Browser", ->
assert.equal browser.window, browser.windows.get(1)

it "should reference opener from opened window", ->
assert.equal browser.window.opener, browser.windows.get(0)
assert.equal browser.window.opener, browser.windows.get(0).top


describe "and close it", ->
Expand Down
3 changes: 2 additions & 1 deletion test/facebook_connect_test.coffee
Expand Up @@ -2,7 +2,7 @@


describe "Facebook Connect", ->

###
browser = new Browser()
before (done)->
Expand Down Expand Up @@ -49,3 +49,4 @@ describe "Facebook Connect", ->
it "should show FB Connect login form", ->
assert browser.query(".login_form_container #loginform")
###
25 changes: 10 additions & 15 deletions test/history_test.coffee
Expand Up @@ -4,7 +4,9 @@ JSDOM = require("jsdom")

describe "History", ->

file_url = "file://#{__dirname}/data/index.html"
# On OS X path probably starts with /Users, but as URL the first component
# ends up as the hostname (stupid), which gets lowered case to /user.
file_url = "file://#{__dirname.toLowerCase()}/data/index.html"


before (done)->
Expand Down Expand Up @@ -39,26 +41,25 @@ describe "History", ->
describe "new window", ->
window = new Browser().window

it "should start out empty", ->
assert.equal window.history.length, 0
it "should start out with no location", ->
assert.equal window.location.href, undefined
it "should start out with one location", ->
assert.equal window.history.length, 1
assert.equal window.location.href, "about:blank"

describe "go forward", ->
before ->
window.history.forward()

it "should have no effect", ->
assert.equal window.history.length, 0
assert.equal window.location.href, undefined
assert.equal window.history.length, 1
assert.equal window.location.href, "about:blank"

describe "go backwards", ->
before ->
window.history.back()

it "should have no effect", ->
assert.equal window.history.length, 0
assert.equal window.location.href, undefined
assert.equal window.history.length, 1
assert.equal window.location.href, "about:blank"


describe "history", ->
Expand Down Expand Up @@ -282,8 +283,6 @@ describe "History", ->
assert.equal browser.location, "http://localhost:3003/history/boo"
it "should load document", ->
assert /Eeek!/.test(browser.html())
it "should load document in new window", ->
assert browser.window != window

describe "replace", ->
browser = new Browser()
Expand All @@ -302,8 +301,6 @@ describe "History", ->
assert.equal browser.location, "http://localhost:3003/history/boo"
it "should load document", ->
assert /Eeek!/.test(browser.html())
it "should load document in new window", ->
assert browser.window != window

describe "reload", ->
browser = new Browser()
Expand All @@ -323,8 +320,6 @@ describe "History", ->
assert.equal browser.location, "http://localhost:3003/"
it "should reload document", ->
assert /Tap, Tap/.test(browser.html())
it "should reload document in new window", ->
assert browser.window != window

describe "components", ->
location = null
Expand Down
2 changes: 1 addition & 1 deletion test/iframe_test.coffee
Expand Up @@ -52,7 +52,7 @@ describe "IFrame", ->
assert /Hello World/.test(document.innerHTML)
assert.equal document.location, "http://localhost:3003/iframe/static"
it "should reference parent window from iframe", ->
assert.equal iframe.contentWindow.parent, browser.window
assert.equal iframe.contentWindow.parent, browser.window.top
it "should not alter the parent", ->
assert.equal "http://localhost:3003/iframe", browser.window.location

Expand Down
7 changes: 6 additions & 1 deletion test/script_test.coffee
Expand Up @@ -127,7 +127,12 @@ describe "Scripts", ->
brains.get "/script/window", (req, res)->
res.send """
<html>
<script>document.title = [window == this, this == window.window, this == top, top == window.top, this == parent, top == window.parent].join(',')</script>
<script>document.title = [window == this,
this == window.window,
this == top,
top == window.top,
this == parent,
top == parent].join(',')</script>
</html>
"""
browser.visit "http://localhost:3003/script/window", done
Expand Down

0 comments on commit 9023ba7

Please sign in to comment.