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

Commit

Permalink
Fix issue related to returnValue in BeforeUnloadEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
arv committed Mar 13, 2014
1 parent afd4552 commit d5e9378
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/wrappers/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,17 @@
if (handledEventsTable.get(originalEvent))
return;
handledEventsTable.set(originalEvent, true);
dispatchEvent(wrap(originalEvent), wrap(originalEvent.target));
}

return dispatchEvent(wrap(originalEvent), wrap(originalEvent.target));
function isLoadLikeEvent(event) {
switch (event.type) {
case 'beforeunload':
case 'load':
case 'unload':
return true;
}
return false;
}

function dispatchEvent(event, originalWrapperTarget) {
Expand All @@ -183,15 +192,15 @@
scope.renderAllPending();
var eventPath = retarget(originalWrapperTarget);

// For window load events the load event is dispatched at the window but
// For window "load" events the "load" event is dispatched at the window but
// the target is set to the document.
//
// http://www.whatwg.org/specs/web-apps/current-work/multipage/the-end.html#the-end
//
// TODO(arv): Find a less hacky way to do this.
if (event.type === 'load' &&
eventPath.length === 2 &&
eventPath[0].target instanceof wrappers.Document) {
if (eventPath.length === 2 &&
eventPath[0].target instanceof wrappers.Document &&
isLoadLikeEvent(event)) {
eventPath.shift();
}

Expand Down Expand Up @@ -542,7 +551,7 @@
}

function BeforeUnloadEvent(impl) {
Event.call(this);
Event.call(this, impl);
}
BeforeUnloadEvent.prototype = Object.create(Event.prototype);
mixin(BeforeUnloadEvent.prototype, {
Expand All @@ -554,6 +563,8 @@
}
});

registerWrapper(window.BeforeUnloadEvent, BeforeUnloadEvent);

function isValidListener(fun) {
if (typeof fun === 'function')
return true;
Expand Down
11 changes: 11 additions & 0 deletions test/manual/before-unload.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!doctype html>
<script src="../../shadowdom.js"></script>
<script>

window.addEventListener('beforeunload', function(e) {
e.returnValue = 'OK';
}, false);

</script>

<p>Reload this page and you should see a dialog saying "OK".

0 comments on commit d5e9378

Please sign in to comment.