Skip to content

Commit

Permalink
History should work as expected
Browse files Browse the repository at this point in the history
  • Loading branch information
assaf committed Sep 19, 2012
1 parent 61e8da9 commit 97f2621
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 30 deletions.
2 changes: 0 additions & 2 deletions lib/zombie/browser.coffee
Expand Up @@ -75,7 +75,6 @@ class Browser extends EventEmitter
onfocus = window.document.createEvent("HTMLEvents")
onfocus.initEvent("focus", false, false)
element.dispatchEvent(onfocus)
@_eventLoop.setActiveWindow(window)

# Window becomes inactive
@on "inactive", (window)->
Expand All @@ -88,7 +87,6 @@ class Browser extends EventEmitter
onblur = window.document.createEvent("HTMLEvents")
onblur.initEvent("blur", false, false)
window.dispatchEvent(onblur)
@_eventLoop.setActiveWindow(null)

# Window has been closed
@on "closed", (window)->
Expand Down
50 changes: 30 additions & 20 deletions lib/zombie/history.coffee
Expand Up @@ -67,22 +67,22 @@ class Entry
# of the entry, but must keep the entry intact
# - The current entry uses the same window as the new entry, also need to
# keep window intact
dispose: (keep)->
dispose: (options)->
if @next
@next.dispose()
@next.dispose(options)
# Do not close window if replacing entry with same window
if keep == @window
if options && options.keepAlive == @window
return
# Do not close window if used by previous entry in history
if @prev && @prev.window == @window
return
@window._cleanup()

append: (entry)->
append: (entry, options)->
if @next
@next.dispose()
@next = entry
@next.dispose(options)
entry.prev = this
@next = entry


class History
Expand All @@ -92,7 +92,7 @@ class History
# Opens the first window and returns it.
open: ({ name, opener, parent, url })->
window = createWindow(browser: @browser, history: this, name: name, opener: opener, parent: parent, url: url)
@current = @first = new Entry(window, url || window.location)
@addEntry(window, url)
return window

# Dispose of all windows in history
Expand All @@ -105,21 +105,25 @@ class History
url ||= window.location
entry = new Entry(window, url, pushState)
@updateLocation(window, url)
@current.append(entry)
@current = entry
@focus(window)
if @current
@current.append(entry)
@current = entry
else
@current = @first = entry

# Replace current entry with a new one.
replaceEntry: (window, url, pushState)->
url ||= window.location
entry = new Entry(window, url, pushState)
@updateLocation(window, url)
@focus(window)
if @current == @first
@current.dispose(window)
if @current
@current.dispose(keepAlive: window)
@current = @first = entry
else
@current.prev.append(entry, window)
@focus(window)
@current.prev.append(entry, keepAlive: window)

# Update window location (navigating to new URL, same window, e.g pushState or hash change)
updateLocation: (window, url)->
Expand All @@ -131,14 +135,10 @@ class History
history.assign(url)
enumerable: true

# Returns current URL.
@prototype.__defineGetter__ "url", ->
return @current?.url

# Form submission
submit: ({ url, method, encoding, data })->
window = @current.window
url = HTML.resourceLoader.resolve(window.document, url || "/")
url = HTML.resourceLoader.resolve(window.document, url)
params =
browser: @browser
history: this
Expand All @@ -150,6 +150,10 @@ class History
data: data
newWindow = createWindow(params)
@addEntry(newWindow, url)

# Returns current URL.
@prototype.__defineGetter__ "url", ->
return @current?.url


# -- Implementation of window.history --
Expand Down Expand Up @@ -198,6 +202,12 @@ class History
@replaceEntry(window, url)
return

reload: ->
if window = @current.window
url = window.location.href
newWindow = createWindow(browser: @browser, history: this, name: window.name, url: url, parent: window.parent)
@replaceEntry(newWindow, url)

# This method is available from Location.
go: (amount)->
was = @current
Expand Down Expand Up @@ -278,9 +288,9 @@ createLocation = (history, url)->
value: (url)->
history.replace(url)

reload: (force)->
value:
history.replace(@url)
reload:
value: (force)->
history.reload()

toString:
value: ->
Expand Down
2 changes: 2 additions & 0 deletions lib/zombie/index.coffee
Expand Up @@ -3,11 +3,13 @@ Path = require("path")


# Make sure Contextify is available to JSDOM
###
try
contextify = Path.resolve(require.resolve("jsdom"), "../../node_modules/contextify")
require contextify
catch ex
throw new Error("To use Zombie, Contextify must be installed as a dependency of JSDOM (not Zombie itself)")
###


# ### zombie.visit(url, callback)
Expand Down
4 changes: 3 additions & 1 deletion lib/zombie/tabs.coffee
Expand Up @@ -94,10 +94,12 @@ createTabs = (browser)->
focus = (window)->
if window && window != active
index = tabs.indexOf(active)
tabs[index] = window
if ~index
tabs[index] = window
if tabs.current == active
tabs.current = window
active = window
browser._eventLoop.setActiveWindow(window)

history = createHistory(browser, focus)
window = history(name: name, opener: opener, url: url)
Expand Down
4 changes: 2 additions & 2 deletions lib/zombie/window.coffee
Expand Up @@ -269,10 +269,10 @@ createWindow = ({ browser, data, encoding, history, method, name, opener, parent
history:
value: windowHistory

# Load the document associated with this window.
loadDocument document: document, history: history, url: url, method: method, encoding: encoding, data: data
# Form submission uses this
window._submit = history.submit.bind(history)
# Load the document associated with this window.
loadDocument document: document, history: history, url: url, method: method, encoding: encoding, data: data
return window


Expand Down
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -20,6 +20,7 @@
"node": ">= 0.6.0"
},
"dependencies": {
"contextify": "~0.1.3",
"eventsource": "~0.0.5",
"html5": "~0.3.8",
"jsdom": "~0.2.15",
Expand Down
2 changes: 1 addition & 1 deletion test/event_source_test.coffee
@@ -1,7 +1,7 @@
{ assert, brains, Browser } = require("./helpers")


describe "EventSource", ->
describe.skip "EventSource", ->

before (done)->
brains.get "/streaming", (req, res)->
Expand Down
4 changes: 2 additions & 2 deletions test/forms_test.coffee
Expand Up @@ -877,7 +877,7 @@ describe "Forms", ->


# File upload
describe "file upload", ->
describe.skip "file upload", ->
before ->
brains.get "/forms/upload", (req, res)->
res.send """
Expand Down Expand Up @@ -1046,7 +1046,7 @@ describe "Forms", ->
assert.equal @browser.text("#is_file"), "true"


describe "content length", ->
describe.skip "content length", ->
before ->
brains.post "/forms/urlencoded", (req, res)->
text = req.body.text
Expand Down
12 changes: 10 additions & 2 deletions test/history_test.coffee
Expand Up @@ -218,6 +218,7 @@ describe "History", ->
@browser.window.location.pathname = "/history/boo"
@browser.on "loaded", ->
done()
@browser.wait()

it "should add page to history", ->
assert.equal @browser.history.length, 2
Expand All @@ -233,6 +234,7 @@ describe "History", ->
@browser.window.location.href = "/history/boo"
@browser.on "loaded", ->
done()
@browser.wait()

it "should add page to history", ->
assert.equal @browser.history.length, 2
Expand All @@ -249,6 +251,7 @@ describe "History", ->
@browser.window.addEventListener "hashchange", ->
done()
@browser.window.location.hash = "boo"
@browser.wait()

it "should add page to history", ->
assert.equal @browser.history.length, 2
Expand All @@ -264,6 +267,7 @@ describe "History", ->
@browser.window.location.assign "http://localhost:3003/history/boo"
@browser.on "loaded", ->
done()
@browser.wait()

it "should add page to history", ->
assert.equal @browser.history.length, 2
Expand All @@ -279,6 +283,7 @@ describe "History", ->
@browser.window.location.replace "http://localhost:3003/history/boo"
@browser.on "loaded", ->
done()
@browser.wait()

it "should not add page to history", ->
assert.equal @browser.history.length, 1
Expand All @@ -287,14 +292,15 @@ describe "History", ->
it "should load document", ->
assert /Eeek!/.test(@browser.html())

describe.skip "reload", ->
describe "reload", ->
before (done)->
@browser = new Browser()
@browser.visit "http://localhost:3003/", =>
@browser.window.document.innerHTML = "Wolf"
@browser.reload()
@browser.on "loaded", ->
done()
@browser.wait()

it "should not add page to history", ->
assert.equal @browser.history.length, 1
Expand All @@ -321,7 +327,7 @@ describe "History", ->
it "should include pathname", ->
assert.equal @location.pathname, "/"
it "should include search", ->
console.dir @location.search
assert.equal @location.search, ""
it "should include hash", ->
assert.equal @location.hash, ""

Expand All @@ -332,6 +338,7 @@ describe "History", ->
@browser.window.location = "http://localhost:3003/history/boo"
@browser.on "loaded", ->
done()
@browser.wait()

it "should add page to history", ->
assert.equal @browser.history.length, 2
Expand All @@ -347,6 +354,7 @@ describe "History", ->
@browser.window.document.location = "http://localhost:3003/history/boo"
@browser.on "loaded", ->
done()
@browser.wait()

it "should add page to history", ->
assert.equal @browser.history.length, 2
Expand Down

0 comments on commit 97f2621

Please sign in to comment.