Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
9 changes: 9 additions & 0 deletions internal/events/dispatch-hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,15 @@ export function setupDispatchHooks(
// Re-dispatch the event. We can't reuse `redispatchEvent()` since we
// need to add the hooks to the copy before it's dispatched.
isRedispatching = true;
const composedPathIncludesAnchor = event
.composedPath()
.some((el) => (el as Partial<HTMLElement>)?.matches?.('a'));
if (event.type === 'click' && composedPathIncludesAnchor) {
// For legacy reasons, synthetic click events dispatching on
// HTMLAnchorElement will trigger link behavior. Prevent this since
// we will dispatch a copy of the same click event.
event.preventDefault();
}
const dispatched = event.composedPath()[0].dispatchEvent(eventCopy);
isRedispatching = false;
if (!dispatched) {
Expand Down
21 changes: 21 additions & 0 deletions internal/events/dispatch-hooks_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,27 @@ describe('dispatch hooks', () => {
.withContext('innerClickListener')
.toHaveBeenCalledTimes(1);
});

it('should not trigger activation behavior for clicks coming from inner <a> elements', () => {
const shadowRoot = element.attachShadow({mode: 'open'});
const anchorElement = document.createElement('a');
anchorElement.href = '#';
shadowRoot.appendChild(anchorElement);

setupDispatchHooks(element, 'click');

const clickEvent = new MouseEvent('click', {
bubbles: true,
cancelable: true,
composed: true,
});

anchorElement.dispatchEvent(clickEvent);

expect(clickEvent.defaultPrevented)
.withContext('clickEvent.defaultPrevented')
.toBeTrue();
});
});

describe('afterDispatch()', () => {
Expand Down
Loading