Skip to content

Commit 8b58f98

Browse files
fix(controller)!: fix label activation utility on slotted elements
PiperOrigin-RevId: 511523410
1 parent c63a1d9 commit 8b58f98

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

controller/events.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,10 @@ export function dispatchActivationClick(element: HTMLElement) {
9797
*/
9898
export function isActivationClick(event: Event) {
9999
// Event must start at the event target.
100+
if (event.currentTarget !== event.target) {
101+
return false;
102+
}
103+
// Event must not be retargeted from shadowRoot.
100104
if (event.composedPath()[0] !== event.target) {
101105
return false;
102106
}

controller/events_test.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ describe('events', () => {
1313

1414
beforeEach(() => {
1515
instance = document.createElement('div');
16-
instance.attachShadow({mode: 'open'});
16+
instance.attachShadow({mode: 'open'})
17+
.append(document.createElement('slot'));
1718
// To have event.target set correctly, the EventTarget instance must be
1819
// attached to the DOM.
1920
document.body.appendChild(instance);
@@ -113,20 +114,38 @@ describe('events', () => {
113114
});
114115

115116
describe('isActivationClick()', () => {
116-
it('should return true only if the event originated from target', () => {
117+
it('returns true for click on listener', () => {
117118
const listener = jasmine.createSpy('listener', isActivationClick);
118119
listener.and.callThrough();
119120
instance.addEventListener('click', listener);
120121
instance.dispatchEvent(
121122
new MouseEvent('click', {bubbles: true, composed: true}));
122123
expect(listener).toHaveBeenCalledTimes(1);
123124
expect(listener.calls.mostRecent().returnValue).toBe(true);
125+
});
126+
127+
it('returns false for click on element listener shadowRoot', () => {
128+
const listener = jasmine.createSpy('listener', isActivationClick);
129+
listener.and.callThrough();
130+
instance.addEventListener('click', listener);
124131
const innerEl = document.createElement('div');
125132
instance.shadowRoot!.append(innerEl);
126-
127133
innerEl.dispatchEvent(
128134
new MouseEvent('click', {bubbles: true, composed: true}));
129-
expect(listener).toHaveBeenCalledTimes(2);
135+
expect(listener).toHaveBeenCalledTimes(1);
136+
expect(listener.calls.mostRecent().returnValue).toBe(false);
137+
});
138+
139+
it('returns false for click on element listener child', () => {
140+
const listener = jasmine.createSpy('listener', isActivationClick);
141+
listener.and.callThrough();
142+
instance.addEventListener('click', listener);
143+
const slottedEl = document.createElement('div');
144+
instance.append(slottedEl);
145+
146+
slottedEl.dispatchEvent(
147+
new MouseEvent('click', {bubbles: true, composed: true}));
148+
expect(listener).toHaveBeenCalledTimes(1);
130149
expect(listener.calls.mostRecent().returnValue).toBe(false);
131150
});
132151
});

0 commit comments

Comments
 (0)