Skip to content

Commit

Permalink
Fix event path for tap event on touch
Browse files Browse the repository at this point in the history
Always use findOriginalTarget with a real event!

Fixes #4670
  • Loading branch information
dfreedm committed Jul 18, 2017
1 parent 466624a commit 50bf45c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/utils/gestures.html
Expand Up @@ -919,7 +919,8 @@
forward: function(e, preventer) {
let dx = Math.abs(e.clientX - this.info.x);
let dy = Math.abs(e.clientY - this.info.y);
let t = Gestures._findOriginalTarget(e);
// find original target from `preventer` for TouchEvents, or `e` for MouseEvents
let t = Gestures._findOriginalTarget(preventer || e);
// dx,dy can be NaN if `click` has been simulated and there was no `down` for `start`
if (isNaN(dx) || isNaN(dy) || (dx <= TAP_DISTANCE && dy <= TAP_DISTANCE) || isSyntheticClick(e)) {
// prevent taps from being generated if an event has canceled them
Expand Down
39 changes: 37 additions & 2 deletions test/unit/gestures.html
Expand Up @@ -35,13 +35,29 @@

teardown(function() {
document.body.removeChild(app);
Polymer.Gestures.resetMouseCanceller();
});

test('tap on x-foo and check localTarget and rootTarget', function() {
var foo = app.$.foo;
foo.dispatchEvent(new CustomEvent('click', {bubbles: true, composed: true}));
assert.equal(app._testLocalTarget, app, 'local target');
assert.equal(app._testRootTarget, foo, 'root target');
let touches = [{
clientX: 0,
clientY: 0,
identifier: 1,
// target is set to the element with `addEventListener`, which is app
target: app
}];
let touchstart = new CustomEvent('touchstart', {bubbles: true, composed: true});
touchstart.changedTouches = touchstart.touches = touches;
foo.dispatchEvent(touchstart);
let touchend = new CustomEvent('touchend', {bubbles: true, composed: true});
touchend.touches = touchend.changedTouches = touches;
foo.dispatchEvent(touchend);
assert.equal(app._testLocalTarget, app, 'local target touch');
assert.equal(app._testRootTarget, foo, 'root target touch');
});

test('tap on x-foo.div and check target info', function() {
Expand All @@ -52,6 +68,23 @@
assert.equal(app._testRootTarget, div, 'app root target');
assert.equal(foo._testLocalTarget, foo, 'foo local target');
assert.equal(foo._testRootTarget, div, 'foo root target');
let touches = [{
clientX: 0,
clientY: 0,
identifier: 1,
// target is set to the element with `addEventListener`, which is app
target: app
}]
let touchstart = new CustomEvent('touchstart', {bubbles: true, composed: true});
touchstart.touches = touchstart.changedTouches = touches;
let touchend = new CustomEvent('touchend', {composed: true, bubbles: true});
touchend.touches = touchend.changedTouches = touches;
div.dispatchEvent(touchstart);
div.dispatchEvent(touchend);
assert.equal(app._testLocalTarget, app, 'app local target touch');
assert.equal(app._testRootTarget, div, 'app root target touch');
assert.equal(foo._testLocalTarget, foo, 'foo local target touch');
assert.equal(foo._testRootTarget, div, 'foo root target touch');
});

test('HTMLElement.click triggers tap', function() {
Expand Down Expand Up @@ -268,7 +301,8 @@
clientX: clientX,
clientY: clientY,
identifier: 1,
target: child
// target is set to the element with `addEventListener`, which is el
target: el
}
];
ev.clientX = clientX;
Expand All @@ -283,7 +317,8 @@
clientX: clientX,
clientY: clientY,
identifier: 1,
target: child
// target is set to the element with `addEventListener`, which is el
target: el
}
];
ev.clientX = clientX;
Expand Down

0 comments on commit 50bf45c

Please sign in to comment.