Skip to content

Commit

Permalink
Gestures: fall back to event.target when composedPath is empty. (#5029)
Browse files Browse the repository at this point in the history
This came up in Safari, where a click event on window ended up crashing
gestures. No original target could be found, because
event.composedPath() returned an empty array, which we assumed couldn't
happen.
  • Loading branch information
aomarks authored Jan 10, 2018
1 parent 589684a commit 98b5aad
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/utils/gestures.html
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,9 @@
_findOriginalTarget: function(ev) {
// shadowdom
if (ev.composedPath) {
const target = /** @type {EventTarget} */(ev.composedPath()[0]);
return target;
const targets = /** @type {!Array<!EventTarget>} */(ev.composedPath());
// It shouldn't be, but sometimes targets is empty (window on Safari).
return targets.length > 0 ? targets[0] : ev.target;
}
// shadydom
return ev.target;
Expand Down
9 changes: 9 additions & 0 deletions test/unit/gestures.html
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,15 @@
document.dispatchEvent(new MouseEvent('click', { detail: 1, clientX: 100, bubbles: true, composed: true }));
Polymer.Gestures.remove(document, 'tap', null);
});

test('#5030', function() {
let count = 0;
const increment = function() { count++; };
Polymer.Gestures.add(window, 'tap', increment);
window.dispatchEvent(new MouseEvent('click', {bubbles: true}));
assert.equal(count, 1);
Polymer.Gestures.remove(window, 'tap', increment);
});
});
</script>

Expand Down

0 comments on commit 98b5aad

Please sign in to comment.