fix(click): do not retarget <a> to the parent <label> (#31368)

Fixes #31359.
This commit is contained in:
Dmitry Gozman 2024-06-18 12:12:19 -07:00 committed by GitHub
parent f6972c1e23
commit ac90a47b73
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 16 additions and 1 deletions

View File

@ -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;

View File

@ -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);
});