|
14 | 14 | * limitations under the License. |
15 | 15 | */ |
16 | 16 |
|
| 17 | +import { hasPointerCursor } from '@isomorphic/ariaSnapshot'; |
17 | 18 | import { normalizeWhiteSpace } from '@isomorphic/stringUtils'; |
18 | 19 |
|
19 | 20 | import type * as aria from '@isomorphic/ariaSnapshot'; |
@@ -102,6 +103,11 @@ function isLeafGeneric(node: aria.AriaNode): boolean { |
102 | 103 | return node.role === 'generic' && node.children.every(child => typeof child === 'string'); |
103 | 104 | } |
104 | 105 |
|
| 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 | + |
105 | 111 | // The tree builder emits raw text tokens - text nodes, CSS content, block spacing markers - as |
106 | 112 | // string children. Coalesce the adjacent ones, normalize whitespace and drop the empties, then |
107 | 113 | // 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 = { |
137 | 143 | // Only unwrap a generic that encloses at most one element, logical grouping still makes sense, |
138 | 144 | // even if it is not ref-able. The decision is made on `exit` - whether the node encloses a single |
139 | 145 | // 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. |
141 | 148 | const unwrapSingleChildGenerics: DistillerPlugin = { |
142 | 149 | 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'; |
146 | 156 | }, |
147 | 157 | }; |
148 | 158 |
|
149 | 159 | // A decorative image - role `img` with no accessible name and no content - carries no |
150 | 160 | // 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. |
152 | 163 | const removeNamelessImages: DistillerPlugin = { |
153 | 164 | 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)) |
156 | 167 | return 'remove'; |
157 | 168 | }, |
158 | 169 | }; |
|
0 commit comments