|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | +import type {TextSnapshot, TextSnapshotNode} from '../McpContext.js'; |
| 7 | + |
| 8 | +export class SnapshotFormatter { |
| 9 | + #snapshot: TextSnapshot; |
| 10 | + |
| 11 | + constructor(snapshot: TextSnapshot) { |
| 12 | + this.#snapshot = snapshot; |
| 13 | + } |
| 14 | + |
| 15 | + toString(): string { |
| 16 | + const chunks: string[] = []; |
| 17 | + const root = this.#snapshot.root; |
| 18 | + |
| 19 | + // Top-level content of the snapshot. |
| 20 | + if ( |
| 21 | + this.#snapshot.verbose && |
| 22 | + this.#snapshot.hasSelectedElement && |
| 23 | + !this.#snapshot.selectedElementUid |
| 24 | + ) { |
| 25 | + chunks.push(`Note: there is a selected element in the DevTools Elements panel but it is not included into the current a11y tree snapshot. |
| 26 | +Get a verbose snapshot to include all elements if you are interested in the selected element.\n\n`); |
| 27 | + } |
| 28 | + |
| 29 | + chunks.push(this.#formatNode(root, 0)); |
| 30 | + return chunks.join(''); |
| 31 | + } |
| 32 | + |
| 33 | + toJSON(): object { |
| 34 | + return this.#nodeToJSON(this.#snapshot.root); |
| 35 | + } |
| 36 | + |
| 37 | + #formatNode(node: TextSnapshotNode, depth = 0): string { |
| 38 | + const chunks: string[] = []; |
| 39 | + const attributes = this.#getAttributes(node); |
| 40 | + const line = |
| 41 | + ' '.repeat(depth * 2) + |
| 42 | + attributes.join(' ') + |
| 43 | + (node.id === this.#snapshot.selectedElementUid |
| 44 | + ? ' [selected in the DevTools Elements panel]' |
| 45 | + : '') + |
| 46 | + '\n'; |
| 47 | + chunks.push(line); |
| 48 | + |
| 49 | + for (const child of node.children) { |
| 50 | + chunks.push(this.#formatNode(child, depth + 1)); |
| 51 | + } |
| 52 | + return chunks.join(''); |
| 53 | + } |
| 54 | + |
| 55 | + #nodeToJSON(node: TextSnapshotNode): object { |
| 56 | + const rawAttrs = this.#getAttributesMap(node); |
| 57 | + const children = node.children.map(child => this.#nodeToJSON(child)); |
| 58 | + const result: Record<string, unknown> = structuredClone(rawAttrs); |
| 59 | + if (children.length > 0) { |
| 60 | + result.children = children; |
| 61 | + } |
| 62 | + return result; |
| 63 | + } |
| 64 | + |
| 65 | + #getAttributes(serializedAXNodeRoot: TextSnapshotNode): string[] { |
| 66 | + const attributes = [`uid=${serializedAXNodeRoot.id}`]; |
| 67 | + |
| 68 | + if (serializedAXNodeRoot.role) { |
| 69 | + attributes.push( |
| 70 | + serializedAXNodeRoot.role === 'none' |
| 71 | + ? 'ignored' |
| 72 | + : serializedAXNodeRoot.role, |
| 73 | + ); |
| 74 | + } |
| 75 | + if (serializedAXNodeRoot.name) { |
| 76 | + attributes.push(`"${serializedAXNodeRoot.name}"`); |
| 77 | + } |
| 78 | + |
| 79 | + const simpleAttrs = this.#getAttributesMap( |
| 80 | + serializedAXNodeRoot, |
| 81 | + /* excludeSpecial */ true, |
| 82 | + ); |
| 83 | + |
| 84 | + for (const attr of Object.keys(serializedAXNodeRoot).sort()) { |
| 85 | + if (excludedAttributes.has(attr)) { |
| 86 | + continue; |
| 87 | + } |
| 88 | + |
| 89 | + const mapped = booleanPropertyMap[attr]; |
| 90 | + if (mapped && simpleAttrs[mapped]) { |
| 91 | + attributes.push(mapped); |
| 92 | + } |
| 93 | + |
| 94 | + const val = simpleAttrs[attr]; |
| 95 | + if (val === true) { |
| 96 | + attributes.push(attr); |
| 97 | + } else if (typeof val === 'string' || typeof val === 'number') { |
| 98 | + attributes.push(`${attr}="${val}"`); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + return attributes; |
| 103 | + } |
| 104 | + |
| 105 | + #getAttributesMap( |
| 106 | + node: TextSnapshotNode, |
| 107 | + excludeSpecial = false, |
| 108 | + ): Record<string, unknown> { |
| 109 | + const result: Record<string, unknown> = {}; |
| 110 | + if (!excludeSpecial) { |
| 111 | + result.id = node.id; |
| 112 | + if (node.role) { |
| 113 | + result.role = node.role; |
| 114 | + } |
| 115 | + if (node.name) { |
| 116 | + result.name = node.name; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + // Re-implementing the exact logic from original function for #getAttributes to be safe: |
| 121 | + return { |
| 122 | + ...result, |
| 123 | + ...this.#extractedAttributes(node), |
| 124 | + }; |
| 125 | + } |
| 126 | + |
| 127 | + #extractedAttributes(node: TextSnapshotNode): Record<string, unknown> { |
| 128 | + const result: Record<string, unknown> = {}; |
| 129 | + |
| 130 | + for (const attr of Object.keys(node).sort()) { |
| 131 | + if (excludedAttributes.has(attr)) { |
| 132 | + continue; |
| 133 | + } |
| 134 | + const value = (node as unknown as Record<string, unknown>)[attr]; |
| 135 | + if (typeof value === 'boolean') { |
| 136 | + if (booleanPropertyMap[attr]) { |
| 137 | + result[booleanPropertyMap[attr]] = true; |
| 138 | + } |
| 139 | + if (value) { |
| 140 | + result[attr] = true; |
| 141 | + } |
| 142 | + } else if (typeof value === 'string' || typeof value === 'number') { |
| 143 | + result[attr] = value; |
| 144 | + } |
| 145 | + } |
| 146 | + return result; |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +const booleanPropertyMap: Record<string, string> = { |
| 151 | + disabled: 'disableable', |
| 152 | + expanded: 'expandable', |
| 153 | + focused: 'focusable', |
| 154 | + selected: 'selectable', |
| 155 | +}; |
| 156 | + |
| 157 | +const excludedAttributes = new Set([ |
| 158 | + 'id', |
| 159 | + 'role', |
| 160 | + 'name', |
| 161 | + 'elementHandle', |
| 162 | + 'children', |
| 163 | + 'backendNodeId', |
| 164 | +]); |
0 commit comments