Skip to content

Commit

Permalink
Fix jashkenas#1366 - Do not prepend root to history.fragment.
Browse files Browse the repository at this point in the history
  • Loading branch information
braddunbar committed Jun 2, 2012
1 parent c924e05 commit 2b0ad77
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
15 changes: 7 additions & 8 deletions backbone.js
Original file line number Diff line number Diff line change
Expand Up @@ -1101,25 +1101,24 @@
navigate: function(fragment, options) {
if (!History.started) return false;
if (!options || options === true) options = {trigger: options};
var frag = (fragment || '').replace(routeStripper, '');
if (this.fragment == frag) return;
var fullFrag = (frag.indexOf(this.options.root) != 0 ? this.options.root : '') + frag;
fragment = (fragment || '').replace(routeStripper, '');
if (this.fragment === fragment) return;
this.fragment = fragment;
var fullFrag = (fragment.indexOf(this.options.root) != 0 ? this.options.root : '') + fragment;

// If pushState is available, we use it to set the fragment as a real URL.
if (this._hasPushState) {
this.fragment = fullFrag;
window.history[options.replace ? 'replaceState' : 'pushState']({}, document.title, fullFrag);

// If hash changes haven't been explicitly disabled, update the hash
// fragment to store history.
} else if (this._wantsHashChange) {
this.fragment = frag;
this._updateHash(window.location, frag, options.replace);
if (this.iframe && (frag != this.getFragment(this.getHash(this.iframe)))) {
this._updateHash(window.location, fragment, options.replace);
if (this.iframe && (fragment != this.getFragment(this.getHash(this.iframe)))) {
// Opening and closing the iframe tricks IE7 and earlier to push a history entry on hash-tag change.
// When replace is true, we don't want this.
if(!options.replace) this.iframe.document.open().close();
this._updateHash(this.iframe.location, frag, options.replace);
this._updateHash(this.iframe.location, fragment, options.replace);
}

// If you've told us that you explicitly don't want fallback hashchange-
Expand Down
14 changes: 14 additions & 0 deletions test/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ $(document).ready(function() {
var lastRoute = null;
var lastArgs = [];

var pushState = window.history && window.history.pushState;
var pathname = window.location.pathname;

function onRoute(router, route, args) {
lastRoute = route;
lastArgs = args;
Expand All @@ -24,6 +27,7 @@ $(document).ready(function() {
teardown: function() {
Backbone.history.stop();
Backbone.history.off('route', onRoute);
if (pushState) window.history.pushState({}, document.title, pathname);
}

});
Expand Down Expand Up @@ -278,4 +282,14 @@ $(document).ready(function() {
}, 50);
});

if (pushState) {
test('History does not prepend root to fragment.', 1, function() {
Backbone.History.started = false;
var history = new Backbone.History();
history.start({pushState: true});
history.navigate('x');
strictEqual(history.fragment, 'x');
});
}

});

0 comments on commit 2b0ad77

Please sign in to comment.