Summary#
Implement focus tracking, tab-order navigation, and focus-related events and pseudo-classes.
Details#
Focus tracking#
Document.activeElementtracks the currently focused element- Only focusable elements can receive focus (form controls, elements with
tabindex,<a>with href) - Clicking a focusable element moves focus to it
element.focus()andelement.blur()methods
Tab navigation#
- Tab key advances focus to next focusable element in tab order
- Shift+Tab moves focus backwards
- Tab order: elements with positive
tabindexfirst (ascending), then DOM order fortabindex=0and naturally focusable elements - Elements with
tabindex=-1are focusable via script but skipped in tab order
Events#
- `focus` / `blur` events (don't bubble)
- `focusin` / `focusout` events (bubble)
CSS pseudo-classes#
- `:focus` matches the focused element
- `:focus-visible` matches when focus should be visually indicated (keyboard navigation)
- `:focus-within` matches ancestors of the focused element
Acceptance criteria#
- `document.activeElement` correctly tracks focus
- Tab/Shift+Tab cycles through focusable elements in correct order
- `tabindex` attribute is respected for ordering and focusability
- Focus/blur events fire correctly with proper bubbling behavior
- `:focus`, `:focus-visible`, `:focus-within` pseudo-classes match correctly in selector matching
- Focus ring renders around the focused element
- Tests cover tab order, event dispatch, and pseudo-class matching
Dependencies#
- Form element parsing and DOM interfaces