Skip to content

Commit

Permalink
fix(click): do not retarget <a> to the parent <label> (#31368)
Browse files Browse the repository at this point in the history
Fixes #31359.
  • Loading branch information
dgozman committed Jun 18, 2024
1 parent f6972c1 commit ac90a47
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -478,7 +478,7 @@ export class InjectedScript {
element = element.closest('button, [role=button], [role=checkbox], [role=radio]') || element;
}
if (behavior === 'follow-label') {
if (!element.matches('input, textarea, button, select, [role=button], [role=checkbox], [role=radio]') &&
if (!element.matches('a, input, textarea, button, select, [role=link], [role=button], [role=checkbox], [role=radio]') &&
!(element as any).isContentEditable) {
// Go up to the label that might be connected to the input/textarea.
element = element.closest('label') || element;
Expand Down
15 changes: 15 additions & 0 deletions tests/page/retarget.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,18 @@ it('check retargeting', async ({ page, asset }) => {
});
}
});

it('should not retarget anchor into parent label', async ({ page }) => {
await page.setContent(`
<label disabled>Text<a href='#' onclick='window.__clicked=1'>Target</a></label>
`);
await page.locator('a').click();
expect(await page.evaluate('window.__clicked')).toBe(1);

await page.setContent(`
<input type="radio" id="input-id" checked disabled />
<label for="input-id">Text<a href='#' onclick='window.__clicked=2'>Target</a></label>
`);
await page.locator('a').click();
expect(await page.evaluate('window.__clicked')).toBe(2);
});

0 comments on commit ac90a47

Please sign in to comment.