Skip to content
This repository has been archived by the owner on Mar 13, 2018. It is now read-only.

Commit

Permalink
Merge pull request #497 from Polymer/platform_issue_83
Browse files Browse the repository at this point in the history
restore isLoadLikeEvent, fixes unload and beforeunload event
  • Loading branch information
John Messerly committed Aug 27, 2014
2 parents 5eb83ac + bd8b9c2 commit b17c814
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/wrappers/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,19 @@
}
}


function isLoadLikeEvent(event) {
switch (event.type) {
// http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#events-and-the-window-object
case 'load':
// http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html#unloading-documents
case 'beforeunload':
case 'unload':
return true;
}
return false;
}

function dispatchEvent(event, originalWrapperTarget) {
if (currentlyDispatchingEvents.get(event))
throw new Error('InvalidStateError');
Expand All @@ -254,12 +267,11 @@
// http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html#the-end
var overrideTarget;
var win;
var type = event.type;

// Should really be not cancelable too but since Firefox has a bug there
// we skip that check.
// https://bugzilla.mozilla.org/show_bug.cgi?id=999456
if (type === 'load' && !event.bubbles) {
if (isLoadLikeEvent(event) && !event.bubbles) {
var doc = originalWrapperTarget;
if (doc instanceof wrappers.Document && (win = doc.defaultView)) {
overrideTarget = doc;
Expand All @@ -274,7 +286,7 @@
} else {
eventPath = getEventPath(originalWrapperTarget, event);

if (event.type !== 'load') {
if (!isLoadLikeEvent(event)) {
var doc = eventPath[eventPath.length - 1];
if (doc instanceof wrappers.Document)
win = doc.defaultView;
Expand Down
26 changes: 26 additions & 0 deletions test/html/on-unload-test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<!DOCTYPE html>
<script src="../../../tools/test/chai/chai.js"></script>
<script src="../../../tools/test/htmltest.js"></script>
<script src="../../shadowdom.js"></script>
<script>

var assert = chai.assert;
var doc = ShadowDOMPolyfill.wrap(document);

var beforeunloadCalled = 0;
window.addEventListener('beforeunload', function(e) {
assert.equal(e.target, doc);
beforeunloadCalled++;
});

window.addEventListener('unload', function(e) {
assert.equal(beforeunloadCalled, 1);
assert.equal(e.target, doc);
done();
});

window.addEventListener('load', function(e) {
window.location.href = 'about:blank';
});

</script>
1 change: 1 addition & 0 deletions test/js/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -928,6 +928,7 @@ test('retarget order (multiple shadow roots)', function() {
});

htmlTest('html/on-load-test.html');
htmlTest('html/on-unload-test.html');

test('event wrap round trip', function() {
var e = new Event('x');
Expand Down

0 comments on commit b17c814

Please sign in to comment.