| 4 | import { extractNodeText, normalizeType } from '../snapshot/snapshot-processing.ts'; |
| 5 | |
| 6 | export function buildSelectorChainForNode( |
| 7 | node: SnapshotNode, |
| 8 | _platform: Platform | PublicPlatform, |
| 9 | options: { action?: 'click' | 'fill' | 'get' } = {}, |
| 10 | ): string[] { |
| 11 | const chain: string[] = []; |
| 12 | const role = normalizeType(node.type ?? ''); |
| 13 | const id = normalizeSelectorText(node.identifier); |
| 14 | const label = normalizeSelectorText(node.label); |
| 15 | const value = normalizeSelectorText(node.value); |
| 16 | const text = normalizeSelectorText(extractNodeText(node)); |
| 17 | const requireEditable = options.action === 'fill'; |
| 18 | |
| 19 | if (id) { |
| 20 | chain.push(`id=${quoteSelectorValue(id)}`); |
| 21 | } |
| 22 | if (role && label) { |
| 23 | chain.push( |
| 24 | requireEditable |
| 25 | ? `role=${quoteSelectorValue(role)} label=${quoteSelectorValue(label)} editable=true` |
| 26 | : `role=${quoteSelectorValue(role)} label=${quoteSelectorValue(label)}`, |
| 27 | ); |
| 28 | } |
| 29 | if (label) { |
| 30 | chain.push( |
| 31 | requireEditable |
| 32 | ? `label=${quoteSelectorValue(label)} editable=true` |
| 33 | : `label=${quoteSelectorValue(label)}`, |
| 34 | ); |
| 35 | } |
| 36 | if (value) { |
| 37 | chain.push( |
| 38 | requireEditable |
| 39 | ? `value=${quoteSelectorValue(value)} editable=true` |
| 40 | : `value=${quoteSelectorValue(value)}`, |
| 41 | ); |
| 42 | } |
| 43 | if (text && text !== label && text !== value) { |
| 44 | chain.push( |
| 45 | requireEditable |
| 46 | ? `text=${quoteSelectorValue(text)} editable=true` |
| 47 | : `text=${quoteSelectorValue(text)}`, |
| 48 | ); |
| 49 | } |
| 50 | if (role && requireEditable && !chain.some((entry) => entry.includes('editable=true'))) { |
| 51 | chain.push(`role=${quoteSelectorValue(role)} editable=true`); |
| 52 | } |
| 53 | |
| 54 | const deduped = uniqueStrings(chain); |
| 55 | if (deduped.length === 0 && role) { |
| 56 | deduped.push( |
| 57 | requireEditable |
| 58 | ? `role=${quoteSelectorValue(role)} editable=true` |
| 59 | : `role=${quoteSelectorValue(role)}`, |
| 60 | ); |
| 61 | } |
| 62 | if (deduped.length === 0) { |
| 63 | const visible = isNodeVisible(node); |