-
Notifications
You must be signed in to change notification settings - Fork 34
feat(perception): add action affordance markers #1064
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /** | ||
| * Compact action-affordance classification for perception output. | ||
| * | ||
| * The returned marker is display metadata only. It must be rendered outside | ||
| * canonical refs/backendNodeIds so existing ref parsers keep working. | ||
| */ | ||
| export type ElementAffordance = | ||
| | 'text-input' | ||
| | 'link' | ||
| | 'control' | ||
| | 'visual' | ||
| | 'text'; | ||
|
|
||
| export type AffordanceMarker = '#' | '@' | '$' | '%' | ''; | ||
|
|
||
| export interface ElementAffordanceInput { | ||
| tagName?: string | null; | ||
| role?: string | null; | ||
| type?: string | null; | ||
| href?: string | null; | ||
| contentEditable?: boolean | string | null; | ||
| } | ||
|
|
||
| const TEXT_INPUT_TYPES = new Set([ | ||
| 'text', | ||
| 'password', | ||
| 'email', | ||
| 'search', | ||
| 'url', | ||
| 'tel', | ||
| 'number', | ||
| ]); | ||
|
|
||
| const TEXT_INPUT_ROLES = new Set([ | ||
| 'textbox', | ||
| 'searchbox', | ||
| ]); | ||
|
|
||
| const LINK_ROLES = new Set([ | ||
| 'link', | ||
| ]); | ||
|
|
||
| const CONTROL_ROLES = new Set([ | ||
| 'button', | ||
| 'checkbox', | ||
| 'radio', | ||
| 'combobox', | ||
| 'listbox', | ||
| 'menu', | ||
| 'menuitem', | ||
| 'menuitemcheckbox', | ||
| 'menuitemradio', | ||
| 'option', | ||
| 'tab', | ||
| 'switch', | ||
| 'slider', | ||
| 'spinbutton', | ||
| 'treeitem', | ||
| ]); | ||
|
|
||
| const VISUAL_ROLES = new Set([ | ||
| 'image', | ||
| 'img', | ||
| 'graphics-symbol', | ||
| ]); | ||
|
|
||
| function normalize(value: string | null | undefined): string { | ||
| return (value ?? '').trim().toLowerCase(); | ||
| } | ||
|
|
||
| function isContentEditable(value: ElementAffordanceInput['contentEditable']): boolean { | ||
| return value === true || normalize(String(value ?? '')) === 'true' || normalize(String(value ?? '')) === 'plaintext-only'; | ||
| } | ||
|
Comment on lines
+71
to
+73
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. |
||
|
|
||
| export function classifyElementAffordance(input: ElementAffordanceInput): ElementAffordance { | ||
| const tagName = normalize(input.tagName); | ||
| const role = normalize(input.role); | ||
| const type = normalize(input.type); | ||
|
|
||
| if (isContentEditable(input.contentEditable) || TEXT_INPUT_ROLES.has(role)) { | ||
| return 'text-input'; | ||
| } | ||
|
|
||
| if (tagName === 'textarea') { | ||
| return 'text-input'; | ||
| } | ||
|
|
||
| if (tagName === 'input') { | ||
| if (!type || TEXT_INPUT_TYPES.has(type)) return 'text-input'; | ||
| if (type === 'hidden') return 'text'; | ||
| return 'control'; | ||
| } | ||
|
|
||
| if (tagName === 'a' || LINK_ROLES.has(role)) { | ||
| return 'link'; | ||
|
Comment on lines
+94
to
+95
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Useful? React with 👍 / 👎. |
||
| } | ||
|
|
||
| if (tagName === 'button' || tagName === 'select' || tagName === 'details' || CONTROL_ROLES.has(role)) { | ||
| return 'control'; | ||
| } | ||
|
|
||
| if (tagName === 'img' || tagName === 'canvas' || tagName === 'video' || tagName === 'svg' || VISUAL_ROLES.has(role)) { | ||
| return 'visual'; | ||
| } | ||
|
|
||
| return 'text'; | ||
| } | ||
|
|
||
| export function affordanceMarkerFor(kind: ElementAffordance): AffordanceMarker { | ||
| switch (kind) { | ||
| case 'text-input': return '#'; | ||
| case 'link': return '@'; | ||
| case 'control': return '$'; | ||
| case 'visual': return '%'; | ||
| case 'text': return ''; | ||
| } | ||
| } | ||
|
|
||
| export function getAffordanceMarker(input: ElementAffordanceInput): AffordanceMarker { | ||
| return affordanceMarkerFor(classifyElementAffordance(input)); | ||
| } | ||
|
|
||
| export function formatAffordancePrefix(input: ElementAffordanceInput): string { | ||
| const marker = getAffordanceMarker(input); | ||
| return marker ? `${marker} ` : ''; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /// <reference types="jest" /> | ||
|
|
||
| import { classifyElementAffordance, formatAffordancePrefix, getAffordanceMarker } from '../../src/utils/element-affordance'; | ||
|
|
||
| describe('element affordance classifier', () => { | ||
| test.each([ | ||
| [{ tagName: 'input', type: 'text' }, 'text-input', '# '], | ||
| [{ tagName: 'input', type: 'search' }, 'text-input', '# '], | ||
| [{ tagName: 'textarea' }, 'text-input', '# '], | ||
| [{ role: 'textbox' }, 'text-input', '# '], | ||
| [{ tagName: 'div', contentEditable: 'true' }, 'text-input', '# '], | ||
| [{ tagName: 'a', href: '/home' }, 'link', '@ '], | ||
| [{ role: 'link' }, 'link', '@ '], | ||
| [{ tagName: 'button' }, 'control', '$ '], | ||
| [{ tagName: 'input', type: 'checkbox' }, 'control', '$ '], | ||
| [{ role: 'combobox' }, 'control', '$ '], | ||
| [{ tagName: 'img' }, 'visual', '% '], | ||
| [{ role: 'image' }, 'visual', '% '], | ||
| [{ tagName: 'p' }, 'text', ''], | ||
| ])('classifies %o as %s', (input, expectedKind, expectedPrefix) => { | ||
| expect(classifyElementAffordance(input)).toBe(expectedKind); | ||
| expect(formatAffordancePrefix(input)).toBe(expectedPrefix); | ||
| }); | ||
|
|
||
| test('does not mark hidden inputs as actionable', () => { | ||
| expect(getAffordanceMarker({ tagName: 'input', type: 'hidden' })).toBe(''); | ||
| }); | ||
|
|
||
| test('treats password fields as text-insertable markers without exposing values', () => { | ||
| expect(getAffordanceMarker({ tagName: 'input', type: 'password' })).toBe('#'); | ||
| }); | ||
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isContentEditablecurrently recognizes onlytrueandplaintext-only, but in HTMLcontenteditablewithout a value is represented as an empty string and is also editable. In DOM mode this means elements like<div contenteditable>are misclassified as plain text and lose the#affordance marker, which weakens the new action-hinting behavior specifically on rich-text editors and similar inputs.Useful? React with 👍 / 👎.