Skip to content

Fix label tap by checking matched label pairs #5039

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 22, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions lib/utils/gestures.html
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@
/** @type {(function(MouseEvent): void | undefined)} */
GestureRecognizer.prototype.click;

// keep track of any labels hit by tghe mouseCanceller
/** @type {!Array<!HTMLLabelElement>} */
const clickedLabels = [];

// touch will make synthetic mouse events
// `preventDefault` on touchend will cancel them,
// but this breaks `<input>` focus and link clicks
Expand All @@ -115,14 +119,31 @@
mouseEvent[HANDLED_OBJ] = {skip: true};
// disable "ghost clicks"
if (mouseEvent.type === 'click') {
let labels = [];
let clickFromLabel = false;
let path = mouseEvent.composedPath && mouseEvent.composedPath();
if (path) {
for (let i = 0; i < path.length; i++) {
if (path[i].nodeType === Node.ELEMENT_NODE) {
if (path[i].localName === 'label') {
labels.push(path[i]);
} else if (path[i].labels) {
let ownerLabels = path[i].labels;
for (let j = 0; j < ownerLabels.length; j++) {
clickFromLabel = clickFromLabel || clickedLabels.indexOf(ownerLabels[j]) > -1;
}
}
}
if (path[i] === POINTERSTATE.mouse.target) {
// commit tracked labels to "clickedLabels"
clickedLabels.push(...labels);
return;
}
}
}
if (clickFromLabel) {
return;
}
mouseEvent.preventDefault();
mouseEvent.stopPropagation();
}
Expand All @@ -137,6 +158,8 @@
for (let i = 0, en; i < events.length; i++) {
en = events[i];
if (setup) {
// reset clickLabels array
clickedLabels.length = 0;
document.addEventListener(en, mouseCanceller, true);
} else {
document.removeEventListener(en, mouseCanceller, true);
Expand Down
25 changes: 25 additions & 0 deletions test/smoke/label-click.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!--
@license
Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<script src="../../../webcomponentsjs/webcomponents-lite.js"></script>
<link rel="import" href="../../polymer.html">
</head>

<body>
<label id="label" for="checkme">Native checkbox</label>
<input type="checkbox" id="checkme">
</body>

</html>