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

Commit b17c814

Browse files
author
John Messerly
committed
Merge pull request #497 from Polymer/platform_issue_83
restore isLoadLikeEvent, fixes unload and beforeunload event
2 parents 5eb83ac + bd8b9c2 commit b17c814

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

src/wrappers/events.js

+15-3
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,19 @@
237237
}
238238
}
239239

240+
241+
function isLoadLikeEvent(event) {
242+
switch (event.type) {
243+
// http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#events-and-the-window-object
244+
case 'load':
245+
// http://www.whatwg.org/specs/web-apps/current-work/multipage/browsers.html#unloading-documents
246+
case 'beforeunload':
247+
case 'unload':
248+
return true;
249+
}
250+
return false;
251+
}
252+
240253
function dispatchEvent(event, originalWrapperTarget) {
241254
if (currentlyDispatchingEvents.get(event))
242255
throw new Error('InvalidStateError');
@@ -254,12 +267,11 @@
254267
// http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html#the-end
255268
var overrideTarget;
256269
var win;
257-
var type = event.type;
258270

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

277-
if (event.type !== 'load') {
289+
if (!isLoadLikeEvent(event)) {
278290
var doc = eventPath[eventPath.length - 1];
279291
if (doc instanceof wrappers.Document)
280292
win = doc.defaultView;

test/html/on-unload-test.html

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<!DOCTYPE html>
2+
<script src="../../../tools/test/chai/chai.js"></script>
3+
<script src="../../../tools/test/htmltest.js"></script>
4+
<script src="../../shadowdom.js"></script>
5+
<script>
6+
7+
var assert = chai.assert;
8+
var doc = ShadowDOMPolyfill.wrap(document);
9+
10+
var beforeunloadCalled = 0;
11+
window.addEventListener('beforeunload', function(e) {
12+
assert.equal(e.target, doc);
13+
beforeunloadCalled++;
14+
});
15+
16+
window.addEventListener('unload', function(e) {
17+
assert.equal(beforeunloadCalled, 1);
18+
assert.equal(e.target, doc);
19+
done();
20+
});
21+
22+
window.addEventListener('load', function(e) {
23+
window.location.href = 'about:blank';
24+
});
25+
26+
</script>

test/js/events.js

+1
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,7 @@ test('retarget order (multiple shadow roots)', function() {
928928
});
929929

930930
htmlTest('html/on-load-test.html');
931+
htmlTest('html/on-unload-test.html');
931932

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

0 commit comments

Comments
 (0)