Skip to content

Commit

Permalink
Always trigger tap for synthetic click events
Browse files Browse the repository at this point in the history
Fixes #2212
  • Loading branch information
dfreedm committed Aug 5, 2015
1 parent eafa3e5 commit 1eef1a7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/standard/gestures.html
Expand Up @@ -111,6 +111,25 @@
}
}

function isSyntheticClick(ev) {
if (ev.type === 'click') {
// ev.detail is 0 for HTMLElement.click in most browsers
if (ev.detail === 0) {
return true;
}
// in the worst case, check that the x/y position of the click is within
// the bounding box of the target of the event
// Thanks IE 10 >:(
var t = Gestures.findOriginalTarget(ev);
var bcr = t.getBoundingClientRect();
// use page x/y to account for scrolling
var x = ev.pageX, y = ev.pageY;
return (x >= bcr.left && x <= bcr.right) &&
(y >= bcr.top && y <= bcr.bottom);
}
return false;
}

var POINTERSTATE = {
mouse: {
target: null,
Expand Down Expand Up @@ -624,7 +643,7 @@
var dy = Math.abs(e.clientY - this.info.y);
var t = Gestures.findOriginalTarget(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)) {
if (isNaN(dx) || isNaN(dy) || (dx <= TAP_DISTANCE && dy <= TAP_DISTANCE) || isSyntheticClick(e)) {
// prevent taps from being generated if an event has canceled them
if (!this.info.prevent) {
Gestures.fire(t, 'tap', {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/gestures.html
Expand Up @@ -53,6 +53,16 @@
assert.equal(foo._testRootTarget, div, 'foo root target');
});

test('HTMLElement.click triggers tap', function() {
// make a mousedown *very* far away to tickle the distance check
var ev = new CustomEvent('mousedown');
ev.clientX = 1e8;
ev.clientY = 1e8;
app.dispatchEvent(ev);
app.click();
assert.equal(app._testLocalTarget, app, 'app local target');
assert.equal(app._testRootTarget, app, 'app root target');
});
});

suite('Event Setup and Teardown', function() {
Expand Down

0 comments on commit 1eef1a7

Please sign in to comment.