Permalink
Browse files

Navigation: Do not trigger 'navigate' if popstate is default-prevented

Fixes gh-8045
Re gh-8050
  • Loading branch information...
gabrielschulhof committed Mar 28, 2015
1 parent 1d3247b commit cc81c159b99ef951ffd6f814455f2e98e02ac1a2
View
@@ -47,9 +47,15 @@ define([
// TODO a lot of duplication between popstate and hashchange
popstate: function( event ) {
var newEvent = new $.Event( "navigate" ),
beforeNavigate = new $.Event( "beforenavigate" ),
state = event.originalEvent.state || {};
var newEvent, beforeNavigate, state;
if ( event.isDefaultPrevented() ) {
return;
}
newEvent = new $.Event( "navigate" );
beforeNavigate = new $.Event( "beforenavigate" );
state = event.originalEvent.state || {};
beforeNavigate.originalEvent = event;
$win.trigger( beforeNavigate );
@@ -0,0 +1,13 @@
define( [ "jquery" ], function( $ ) {
// TODO: Attaching early to popstate like this becomes unnecessary once the navigate event has been
// properly implemented in that it removes its popstate handler upon teardown and attaches it upon
// setup, rather than attaching it upon setup and then simply making sure upon subsequent calls to
// setup that the handlers are attached.
$( window ).on( "popstate", function( event ) {
if ( window.preventDefaultForNextPopstate ) {
event.preventDefault();
}
} );
} );
@@ -105,6 +105,41 @@ $.testHelper.setPushState();
}
]);
});
// relies on having the early popstate handler defined in early_popstate_handler.js
asyncTest( "Default-prevented popstate does not trigger a navigate event",
function( assert ) {
var eventNs = ".defaultPreventedPopstate";
assert.expect( 2 );
$.testHelper.detailedEventCascade( [
function() {
window.history.replaceState( { foo: "bar" }, document.title,
location.href.replace(/#.*/, "" ) + "#foo" );
location.hash = "#foo2";
},
{
navigate: { src: $( window ), event: "navigate" + eventNs + "1" }
},
function( result ) {
assert.deepEqual( result.navigate.timedOut, false,
"Received navigate event going forward" );
window.preventDefaultForNextPopstate = true;
window.history.back();
},
{
navigate: { src: $( window ), event: "navigate" + eventNs + "2" }
},
function( result ) {
assert.deepEqual( result.navigate.timedOut, true,
"Received no navigate event from a default-prevented popstate" );
delete window.preventDefaultForNextPopstate;
start();
}
] );
} );
} else {
asyncTest( "hashchange navigation provides for data added in a later binding", function() {
$( window ).one( "beforenavigate", function( event, data ) {
@@ -120,4 +155,4 @@ $.testHelper.setPushState();
location.hash = "#foo2";
});
}
})( jQuery );
})( jQuery );
@@ -12,7 +12,8 @@
<link rel="stylesheet" href="../../../../external/qunit/qunit.css"/>
<link rel="stylesheet" href="../../../jqm-tests.css"/>
<script>
$.testHelper.asyncLoad([
$.testHelper.asyncLoad( [
[ "early_popstate_handler.js" ],
[ "events/navigate" ],
[ "event_core.js" ]
], "../../../../js" );

0 comments on commit cc81c15

Please sign in to comment.