|
128 | 128 | function matchingLabels(el) { |
129 | 129 | /** @type {!Array<!HTMLLabelElement>} */ |
130 | 130 | 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 |
132 | 135 | if (!labels || !labels.length) { |
133 | 136 | labels = []; |
134 | 137 | 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 | | - } |
142 | 138 | // if there is an id on `el`, check for all labels with a matching `for` attribute |
143 | 139 | if (el.id) { |
144 | 140 | let matching = root.querySelectorAll(`label[for = ${el.id}]`); |
|
168 | 164 | mouseEvent[HANDLED_OBJ] = {skip: true}; |
169 | 165 | // disable "ghost clicks" |
170 | 166 | if (mouseEvent.type === 'click') { |
171 | | - let labels = []; |
172 | 167 | let clickFromLabel = false; |
173 | 168 | let path = mouseEvent.composedPath && mouseEvent.composedPath(); |
174 | 169 | if (path) { |
175 | 170 | for (let i = 0; i < path.length; i++) { |
176 | 171 | if (path[i].nodeType === Node.ELEMENT_NODE) { |
177 | 172 | if (path[i].localName === 'label') { |
178 | | - labels.push(path[i]); |
| 173 | + clickedLabels.push(path[i]); |
179 | 174 | } else if (canBeLabelled(path[i])) { |
180 | 175 | let ownerLabels = matchingLabels(path[i]); |
| 176 | + // check if one of the clicked labels is labelling this element |
181 | 177 | for (let j = 0; j < ownerLabels.length; j++) { |
182 | 178 | clickFromLabel = clickFromLabel || clickedLabels.indexOf(ownerLabels[j]) > -1; |
183 | 179 | } |
184 | 180 | } |
185 | 181 | } |
186 | 182 | if (path[i] === POINTERSTATE.mouse.target) { |
187 | | - // commit tracked labels to "clickedLabels" |
188 | | - clickedLabels.push(...labels); |
189 | 183 | return; |
190 | 184 | } |
191 | 185 | } |
192 | 186 | } |
| 187 | + // if one of the clicked labels was labelling the target element, |
| 188 | + // this is not a ghost click |
193 | 189 | if (clickFromLabel) { |
194 | 190 | return; |
195 | 191 | } |
|
0 commit comments