From a77d64e5c291132ab971b0d1d1dd237e3d1a5f85 Mon Sep 17 00:00:00 2001 From: Daniel Freedman Date: Thu, 11 Jan 2018 17:54:59 -0800 Subject: [PATCH] Fix label tap by checking matched label pairs Fixes #4717 --- lib/utils/gestures.html | 23 +++++++++++++++++++++++ test/smoke/label-click.html | 25 +++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 test/smoke/label-click.html diff --git a/lib/utils/gestures.html b/lib/utils/gestures.html index 738940f406..b29a7a4d4e 100644 --- a/lib/utils/gestures.html +++ b/lib/utils/gestures.html @@ -97,6 +97,10 @@ /** @type {(function(MouseEvent): void | undefined)} */ GestureRecognizer.prototype.click; + // keep track of any labels hit by tghe mouseCanceller + /** @type {!Array} */ + const clickedLabels = []; + // touch will make synthetic mouse events // `preventDefault` on touchend will cancel them, // but this breaks `` focus and link clicks @@ -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(); } @@ -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); diff --git a/test/smoke/label-click.html b/test/smoke/label-click.html new file mode 100644 index 0000000000..c2ca4d3fa1 --- /dev/null +++ b/test/smoke/label-click.html @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file