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