Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 16 additions & 53 deletions src/formatters/snapshotFormatter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,69 +28,32 @@ function getAttributes(serializedAXNodeRoot: TextSnapshotNode): string[] {
`"${serializedAXNodeRoot.name || ''}"`, // Corrected: Added quotes around name
];

// Value properties
const valueProperties = [
'value',
'valuetext',
'valuemin',
'valuemax',
'level',
'autocomplete',
'haspopup',
'invalid',
'orientation',
'description',
'keyshortcuts',
'roledescription',
] as const;
for (const property of valueProperties) {
if (
property in serializedAXNodeRoot &&
serializedAXNodeRoot[property] !== undefined
) {
attributes.push(`${property}="${serializedAXNodeRoot[property]}"`);
}
}
const excluded = new Set(['id', 'role', 'name', 'elementHandle', 'children']);

// Boolean properties that also have an 'able' attribute
const booleanPropertyMap = {
const booleanPropertyMap: Record<string, string> = {
disabled: 'disableable',
expanded: 'expandable',
focused: 'focusable',
selected: 'selectable',
};
for (const [property, ableAttribute] of Object.entries(booleanPropertyMap)) {
if (property in serializedAXNodeRoot) {
attributes.push(ableAttribute);
if (serializedAXNodeRoot[property as keyof typeof booleanPropertyMap]) {
attributes.push(property);
}
}
}

const booleanProperties = [
'modal',
'multiline',
'readonly',
'required',
'multiselectable',
] as const;

for (const property of booleanProperties) {
if (property in serializedAXNodeRoot && serializedAXNodeRoot[property]) {
attributes.push(property);
for (const attr of Object.keys(serializedAXNodeRoot).sort()) {
if (excluded.has(attr)) {
continue;
}
}

// Mixed boolean/string attributes
for (const property of ['pressed', 'checked'] as const) {
if (property in serializedAXNodeRoot) {
attributes.push(property);
if (serializedAXNodeRoot[property]) {
attributes.push(`${property}="${serializedAXNodeRoot[property]}"`);
const value = (serializedAXNodeRoot as unknown as Record<string, unknown>)[
attr
];
if (typeof value === 'boolean') {
if (booleanPropertyMap[attr]) {
attributes.push(booleanPropertyMap[attr]);
}
if (value) {
attributes.push(attr);
}
} else if (typeof value === 'string' || typeof value === 'number') {
attributes.push(`${attr}="${value}"`);
}
}

return attributes;
}
2 changes: 1 addition & 1 deletion tests/McpResponse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ uid=1_0 RootWebArea ""
## Page content
uid=1_0 RootWebArea "My test page"
uid=1_1 StaticText "username"
uid=1_2 textbox "username" value="mcp" focusable focused
uid=1_2 textbox "username" focusable focused value="mcp"
`,
);
});
Expand Down
2 changes: 1 addition & 1 deletion tests/formatters/snapshotFormatter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('snapshotFormatter', () => {
const formatted = formatA11ySnapshot(snapshot);
assert.strictEqual(
formatted,
`uid=1_1 checkbox "checkbox" checked checked="true"
`uid=1_1 checkbox "checkbox" checked
uid=1_2 statictext "text"
`,
);
Expand Down