Skip to content

Commit e1df166

Browse files
committed
Add docs and cleanup matchingLabels
Remove broken `while` loop, as that case is already handled by the mouseCancellor code
1 parent 70edf1f commit e1df166

2 files changed

Lines changed: 13 additions & 14 deletions

File tree

lib/utils/gestures.html

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -128,17 +128,13 @@
128128
function matchingLabels(el) {
129129
/** @type {!Array<!HTMLLabelElement>} */
130130
let labels = el.labels;
131-
// IE doesn't have `labels` and Safari doesn't populate `labels` if element is in a shadowroot.
131+
// IE doesn't have `labels` and Safari doesn't populate `labels`
132+
// if element is in a shadowroot.
133+
// In this instance, finding the non-ancestor labels is enough,
134+
// as the mouseCancellor code will handle ancstor labels
132135
if (!labels || !labels.length) {
133136
labels = [];
134137
let root = el.getRootNode();
135-
// find all labels that are ancestors
136-
let cur = el.parentNode;
137-
while (cur !== root) {
138-
if (cur.localName === 'label') {
139-
labels.push(/** @type {!HTMLLabelElement} */(cur));
140-
}
141-
}
142138
// if there is an id on `el`, check for all labels with a matching `for` attribute
143139
if (el.id) {
144140
let matching = root.querySelectorAll(`label[for = ${el.id}]`);
@@ -168,28 +164,28 @@
168164
mouseEvent[HANDLED_OBJ] = {skip: true};
169165
// disable "ghost clicks"
170166
if (mouseEvent.type === 'click') {
171-
let labels = [];
172167
let clickFromLabel = false;
173168
let path = mouseEvent.composedPath && mouseEvent.composedPath();
174169
if (path) {
175170
for (let i = 0; i < path.length; i++) {
176171
if (path[i].nodeType === Node.ELEMENT_NODE) {
177172
if (path[i].localName === 'label') {
178-
labels.push(path[i]);
173+
clickedLabels.push(path[i]);
179174
} else if (canBeLabelled(path[i])) {
180175
let ownerLabels = matchingLabels(path[i]);
176+
// check if one of the clicked labels is labelling this element
181177
for (let j = 0; j < ownerLabels.length; j++) {
182178
clickFromLabel = clickFromLabel || clickedLabels.indexOf(ownerLabels[j]) > -1;
183179
}
184180
}
185181
}
186182
if (path[i] === POINTERSTATE.mouse.target) {
187-
// commit tracked labels to "clickedLabels"
188-
clickedLabels.push(...labels);
189183
return;
190184
}
191185
}
192186
}
187+
// if one of the clicked labels was labelling the target element,
188+
// this is not a ghost click
193189
if (clickFromLabel) {
194190
return;
195191
}

test/unit/gestures.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,11 +564,12 @@
564564
let el = document.createElement('x-native-label');
565565
document.body.appendChild(el);
566566
let target = el.$.label;
567+
// simulate the event sequence of a touch on the screen
567568
let touches = [{
568569
clientX: 0,
569570
clientY: 0,
570571
identifier: 1,
571-
// target is set to the element with `addEventListener`, which is app
572+
// target is set to the element with `addEventListener`, which is `target`
572573
target
573574
}];
574575
let touchstart = new CustomEvent('touchstart', {bubbles: true, composed: true});
@@ -577,9 +578,11 @@
577578
let touchend = new CustomEvent('touchend', {bubbles: true, composed: true});
578579
touchend.touches = touchend.changedTouches = touches;
579580
target.dispatchEvent(touchend);
581+
// simulate a mouse click on the label
580582
let click = new MouseEvent('click', {bubbles: true, composed: true});
581583
target.dispatchEvent(click);
582-
assert.equal(el.$.check.checked, true);
584+
// check that the mouse click on the label will activate the checkbox
585+
assert.equal(el.$.check.checked, true, 'checkbox should be checked');
583586
document.body.removeChild(el);
584587
});
585588
});

0 commit comments

Comments
 (0)