Skip to content

Commit 8376826

Browse files
committed
cherry-pick(#42034): fix(aria): keep icon-only clickable elements in ai snapshots
1 parent 66c5cc9 commit 8376826

2 files changed

Lines changed: 43 additions & 11 deletions

File tree

‎packages/injected/src/ariaSnapshotDistiller.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17+
import { hasPointerCursor } from '@isomorphic/ariaSnapshot';
1718
import { normalizeWhiteSpace } from '@isomorphic/stringUtils';
1819

1920
import type * as aria from '@isomorphic/ariaSnapshot';
@@ -102,6 +103,11 @@ function isLeafGeneric(node: aria.AriaNode): boolean {
102103
return node.role === 'generic' && node.children.every(child => typeof child === 'string');
103104
}
104105

106+
// Removing the click target root would hide an actionable element from the snapshot.
107+
function isClickTargetRoot(node: aria.AriaNode, ctx: DistillerContext): boolean {
108+
return !!node.ref && hasPointerCursor(node) && !ctx.ancestors.some(ancestor => !!ancestor.ref && hasPointerCursor(ancestor));
109+
}
110+
105111
// The tree builder emits raw text tokens - text nodes, CSS content, block spacing markers - as
106112
// string children. Coalesce the adjacent ones, normalize whitespace and drop the empties, then
107113
// drop a lone text child that merely repeats the node's accessible name. Runs on `exit`, so the
@@ -137,22 +143,27 @@ const mergeStringChildren: DistillerPlugin = {
137143
// Only unwrap a generic that encloses at most one element, logical grouping still makes sense,
138144
// even if it is not ref-able. The decision is made on `exit` - whether the node encloses a single
139145
// ref-bearing child is only known after its own descendants were unwrapped - so nested wrappers
140-
// collapse bottom-up.
146+
// collapse bottom-up. A generic emptied by the other plugins is dropped, unless it is the
147+
// click target root, for example an icon-only button.
141148
const unwrapSingleChildGenerics: DistillerPlugin = {
142149
name: 'unwrapSingleChildGenerics',
143-
exit(node: aria.AriaNode): 'unwrap' | void {
144-
if (node.role === 'generic' && !node.name && node.children.length <= 1 && node.children.every(child => typeof child !== 'string' && !!child.ref))
145-
return 'unwrap';
150+
exit(node: aria.AriaNode, ctx: DistillerContext): 'unwrap' | void {
151+
if (node.role !== 'generic' || node.name || node.children.length > 1 || !node.children.every(child => typeof child !== 'string' && !!child.ref))
152+
return;
153+
if (!node.children.length && isClickTargetRoot(node, ctx))
154+
return;
155+
return 'unwrap';
146156
},
147157
};
148158

149159
// A decorative image - role `img` with no accessible name and no content - carries no
150160
// information. The decision is made on `exit` - whether the node has content is only known after
151-
// `mergeStringChildren` dropped the empty text tokens.
161+
// `mergeStringChildren` dropped the empty text tokens. A clickable image outside of any clickable
162+
// container is not decorative though - e.g. a bare svg icon acting as a button - and is kept.
152163
const removeNamelessImages: DistillerPlugin = {
153164
name: 'removeNamelessImages',
154-
exit(node: aria.AriaNode): 'remove' | void {
155-
if (node.role === 'img' && !node.name && !node.children.length)
165+
exit(node: aria.AriaNode, ctx: DistillerContext): 'remove' | void {
166+
if (node.role === 'img' && !node.name && !node.children.length && !isClickTargetRoot(node, ctx))
156167
return 'remove';
157168
},
158169
};

‎tests/page/page-aria-snapshot-ai.spec.ts‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -440,13 +440,13 @@ it('should omit images without an accessible name', async ({ page }) => {
440440
`);
441441

442442
const snapshot = await snapshotForAI(page);
443-
// A nameless image carries no information and is omitted, whether or not it is clickable. Only
444-
// the named image is kept - and the body wrapper, left with a single child, is unwrapped.
443+
// A nameless image that cannot be clicked carries no information and is omitted.
445444
expect(snapshot).toContainYaml(`
446-
- img "A cat" [ref=e3]
445+
- generic [active] [ref=e1]:
446+
- img "A cat" [ref=e3]
447+
- img [ref=e4] [cursor=pointer]
447448
`);
448449
expect(snapshot).not.toContain('[ref=e2]');
449-
expect(snapshot).not.toContain('[ref=e4]');
450450
});
451451

452452
it('should omit a nameless image nested inside a link', async ({ page }) => {
@@ -463,6 +463,27 @@ it('should omit a nameless image nested inside a link', async ({ page }) => {
463463
expect(snapshot).not.toContain('img');
464464
});
465465

466+
it('should keep icon-only clickable elements', {
467+
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/42013' },
468+
}, async ({ page }) => {
469+
const icon = `<svg viewBox="0 0 1024 1024"><use xlink:href="#icon"></use></svg>`;
470+
await page.setContent(`
471+
<div style="cursor: pointer" aria-haspopup="true"><i>${icon}</i></div>
472+
<img style="cursor: pointer" src="data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA=">
473+
<a style="cursor: pointer" href="/target">${icon}</a>
474+
<div onclick="void 0">${icon}</div>
475+
`);
476+
477+
const snapshot = await snapshotForAI(page);
478+
expect(snapshot).toContainYaml(`
479+
- generic [active] [ref=e1]:
480+
- generic [ref=e2] [cursor=pointer]
481+
- img [ref=e5] [cursor=pointer]
482+
- link [ref=e6] [cursor=pointer]:
483+
- /url: /target
484+
`);
485+
});
486+
466487
it('should omit leaf generic whose text is already in an ancestor name', async ({ page }) => {
467488
// The inner element is block so it survives as its own generic node (an inline single-text span
468489
// would be collapsed into the link instead). It inherits the link's pointer cursor.

0 commit comments

Comments
 (0)