dom

package
v0.0.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: MIT Imports: 5 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README ΒΆ

Package dom

Package dom implements the logical node tree for Kite. It is responsible for modeling the structure of the document and its semantic state.

πŸ› Architecture

The dom package is strictly separated from layout and rendering logic:

  1. Logical Tree Only: dom nodes model the hierarchy and semantic state (Focusable(), Disabled()). They do not contain layout algorithms or drawing logic.
  2. Synchronization Flags: Changes to the DOM structure trigger synchronization flags (NeedsSync, ChildNeedsSync) that the engine uses to update the render tree during the Sync Phase.
  3. Element Identity (ADR-0036): Every element carries an outer back-pointer (exposed as self in some implementations) to ensure that identity remains stable even when elements are wrapped by widgets.

πŸ”‘ Key Interfaces

Node

The base interface for every node in the tree. It provides methods for tree traversal (Parent, FirstChild, NextSibling, etc.) and management of the associated render.Object.

Element

Extends Node with identity attributes like TagName and ID.

Selection & Range (ADR-022)

Represents text selection state within the document.

  • Range: Defines a segment of text between a start and end point.
  • Selection: Managed by the Document, holds the active ranges and dispatches selectionchange events.
SpatialCaret & Direction (ADR-045)

Governs hybrid caret-level navigation and boundary-crossing focus transitions.

  • Direction: Enum for the four cardinal directions (DirectionUp, DirectionDown, DirectionLeft, DirectionRight).
  • SpatialCaret: Implemented by elements that support character-level caret navigation:
    type SpatialCaret interface {
        MoveCaret(dir Direction) (boundaryReached bool)
        ResetCaret(dir Direction)
    }
    
Disableable

Indicates that an element can be semantically disabled.

type Disableable interface {
    IsDisabled() bool
    SetDisabled(bool)
}
Focusable

Indicates that an element can receive keyboard focus.

type Focusable interface {
    IsFocusable() bool
    Focus()
    Blur()
}
FormControl

Indicates that an element acts as a form control exposing a name and value for form collection.

type FormControl interface {
    Node
    Name() string
    Value() any
}

πŸ”„ Lifecycle Hooks

Nodes can implement the Lifecycle interface to receive notifications when they enter or leave the live tree:

  • OnConnected: Fired in pre-order during the attach walk.
  • OnDisconnected: Fired in post-order during the detach walk.

πŸ”— Connection and Adoption

  • Connected State: A node is "connected" when it is reachable from the Document root.
  • Adoption: When a widget embeds an element, it must respect the adoption registry to ensure that event targets and lookups return the user-visible wrapper.

Documentation ΒΆ

Overview ΒΆ

Package dom defines the interfaces for Kite's logical document tree. A Document owns Element and TextNode nodes and tracks selection and overlays. Tree traversal visits application children, not the private nodes that implement built-in controls. Lifecycle provides callbacks for nodes entering or leaving a connected tree.

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func DefaultScroller ΒΆ

func DefaultScroller(host Element) event.Scrollable

DefaultScroller returns a new Scrollable implementation for the given host.

func IsUANode ΒΆ

func IsUANode(n Node) bool

IsUANode reports whether n is part of a UA shadow subtree.

func LayoutChildren ΒΆ

func LayoutChildren(n Node) iter.Seq[Node]

LayoutChildren returns an iterator that yields the engine-visible children of n.

func RegisterImplementation ΒΆ

func RegisterImplementation(i Implementation)

RegisterImplementation registers the concrete implementation of the DOM. This is called by internal/dom's init() function.

Types ΒΆ

type Direction ΒΆ

type Direction uint8

Direction represents the four cardinal directions for spatial navigation.

const (
	// DirectionUp navigates toward the top of the screen.
	DirectionUp Direction = iota
	// DirectionDown navigates toward the bottom of the screen.
	DirectionDown
	// DirectionLeft navigates toward the left side of the screen.
	DirectionLeft
	// DirectionRight navigates toward the right side of the screen.
	DirectionRight
)

type Disableable ΒΆ

type Disableable interface {
	IsDisabled() bool
	SetDisabled(bool)
}

Disableable indicates that an element can be semantically disabled.

type Document ΒΆ

type Document interface {
	Node

	// CreateElement returns a new Element with the given tag name, owned by
	// this document. If self is nil, the element's identity is itself;
	// if self is not nil, it is set as the element's identity (Task 02).
	// The returned node is detached (no parent).
	CreateElement(tag string, self Node) Element

	// CreateTextNode returns a new TextNode with the given data, owned by
	// this document. If self is nil, the node's identity is itself.
	// The returned node is detached (no parent).
	CreateTextNode(data string, self Node) TextNode

	// GetElementByID returns the Element whose ID equals id, or nil if no
	// such element exists in this document. The lookup is O(1); the registry
	// is maintained by SetID and RemoveChild.
	GetElementByID(id string) Element

	// FindAnchor returns the Element registered under name in the anchor
	// registry, or nil if no anchor with that name is known. The anchor
	// registry is separate from the ID registry so that anchor names and
	// element IDs do not shadow each other.
	FindAnchor(name string) Element

	// RegisterAnchor adds el to the anchor registry under name. Called by
	// Anchor elements (Task 18) when their Name property is set.
	RegisterAnchor(name string, el Element)

	// UnregisterAnchor removes the entry for name from the anchor registry.
	// Called by Anchor elements when they are destroyed or their name changes.
	UnregisterAnchor(name string)

	// Body returns the root element of the document (similar to <body>).
	// If no body is mounted, it returns nil.
	Body() Element

	// Focus acquisition and state.
	Focus(el Element)
	IsFocused(el Element) bool

	// Focus scoping.
	PushScope(scope *FocusScope)
	PopScope()
	ActiveScope() *FocusScope

	// Focus navigation.
	CurrentFocus() Element
	NextFocus() bool
	PreviousFocus() bool
	NavigateFocus(dir Direction) bool
	MoveCaret(dir Direction) bool

	// QuerySelector returns the first element matching the selector in this document's subtree.
	// It pierces UA shadow subtrees.
	QuerySelector(selector string) Element

	// ShowOverlay adds el to the top layer at the specified z-index.
	// Elements in the top layer are rendered above the document body and
	// ignore the normal layout flow. If el is already an overlay, its
	// z-index is updated.
	ShowOverlay(el Element, zIndex int)

	// HideOverlay removes el from the top layer. If el is not an overlay,
	// the call is a no-op.
	HideOverlay(el Element)

	// Overlays returns an iterator over all elements currently in the top
	// layer, sorted by z-index (ascending) and then by insertion order.
	Overlays() iter.Seq[Element]

	// Selection returns the selection object for this document, which
	// represents the range of text selected by the user or current caret
	// position.
	Selection() Selection

	// CreateRange creates a new Range for this document.
	CreateRange() Range

	// SetFocusHandle injects the focus management implementation into the document.
	SetFocusHandle(handle FocusHandle)

	// Terminal returns the host environment, or nil if no host is attached.
	Terminal() terminal.Terminal
	// SetTerminal sets the terminal object for this document.
	SetTerminal(t terminal.Terminal)

	// DefaultView returns the layout/style view for this document.
	DefaultView() View

	// FindNodeAtByteOffset performs a structural walk to find the text node
	// and rune offset corresponding to a flat byte offset in the document.
	FindNodeAtByteOffset(root Node, targetOffset int) (Node, int)
}

Document is the root of a DOM tree and the factory for all new nodes. Tree-global concerns such as focus management, and the task scheduler are out of scope here and will be added in later tasks.

func NewDocument ΒΆ

func NewDocument() Document

NewDocument creates and returns a new, empty Document.

type Element ΒΆ

type Element interface {
	Node

	// TagName returns the tag name used to create this element.
	TagName() string

	// ID returns the element's identifier attribute value.
	ID() string

	// SetID sets the element's identifier attribute value.
	SetID(id string)

	// Class returns the element's classification tag.
	Class() string

	// SetClass sets the element's classification tag.
	SetClass(class string)

	// Attribute returns the value of the attribute with the given name,
	// and a boolean indicating if the attribute is present.
	Attribute(name string) (string, bool)

	// SetAttribute sets the attribute with the given name to value.
	SetAttribute(name, value string)

	// RemoveAttribute removes the attribute with the given name.
	RemoveAttribute(name string)

	// HasAttribute reports whether the element has the attribute with the given name.
	HasAttribute(name string) bool

	// EachAttribute calls fn for each attribute set on the element.
	// If fn returns false, iteration stops immediately.
	EachAttribute(fn func(name, value string) bool)

	// QuerySelector returns the first element matching the selector in this element's subtree.
	// It pierces UA shadow subtrees.
	QuerySelector(selector string) Element

	// ReplaceWith replaces this element with the given nodes and returns this
	// element. The new nodes are appended in the order they are given. If this
	// element has no parent, the call is a no-op and this element is returned.
	ReplaceWith(nodes ...Node) Element

	// AttachUARoot attaches root as the host element's closed UA shadow subtree.
	// The UA subtree is invisible to all public traversal APIs (Children(),
	// FirstChild(), LastChild(), ChildNodes(), GetElementByID()) but is walked
	// by the engine during Sync, Style, Layout, and Paint phases via
	// dom.LayoutChildren().
	//
	// AttachUARoot recursively sets the outer back-pointer on every node in
	// root's subtree to this host element, so event.Target() and similar
	// identity queries collapse to the host (ADR-0036). It also marks the host
	// as NeedsSync so the engine picks up the new subtree on the next Sync pass.
	//
	// AttachUARoot must be called eagerly in the host's constructor, before the
	// host is connected to a document. The ua root must be created against the
	// same Document as the host. Calling AttachUARoot more than once panics.
	AttachUARoot(root Node)

	// Scroll returns the current raw scroll offset (x, y) in terminal cells.
	Scroll() (x, y int)

	// ScrollTo sets the raw scroll offset to (x, y) and marks the element
	// for a paint update (DirtyScroll). Dispatches event.EventScroll.
	ScrollTo(x, y int)

	// ScrollBy shifts the raw scroll offset by (dx, dy).
	ScrollBy(dx, dy int)

	// ScrollCursorIntoView scrolls the element so that the cursor (caret) is
	// visible within the content box. It is typically called by the engine
	// after layout if the element is focused.
	ScrollCursorIntoView()

	// ProvidesCursor reports whether this element provides a text cursor.
	// Used by the layout and paint engines to adjust scroll clamping.
	ProvidesCursor() bool

	// GetBoundingClientRect returns the physical terminal rectangle occupied
	// by this element. It returns (rect, true) if the element is connected
	// and has been laid out; otherwise it returns (Rect{}, false).
	GetBoundingClientRect() (geom.Rect, bool)

	// TabIndex returns the tab index of the element.
	TabIndex() int
	// SetTabIndex sets the tab index of the element.
	SetTabIndex(index int)
	// Focus attempts to move focus to this element.
	Focus()
	// Blur removes focus from this element.
	Blur()
	// IsFocusable reports whether this element is currently focusable.
	IsFocusable() bool

	// RawStyle returns the author-set sparse style for this element.
	RawStyle() style.Style
	// DefaultStyle returns the element-type default style.
	DefaultStyle() style.Style
	// IntrinsicStyle returns the UA-mandated sparse style for this element.
	IntrinsicStyle() style.Style
}

Element extends Node with identity (tag name, id).

func NewElement ΒΆ

func NewElement(doc Document, tag string, self Node) Element

NewElement allocates an element with the given tag name and owner document.

type FocusHandle ΒΆ

type FocusHandle interface {
	Focus(el Element)
	IsFocused(el Element) bool
	PushScope(scope *FocusScope)
	PopScope()
	ActiveScope() *FocusScope
	Current() Element
	Next() bool
	Previous() bool

	NavigateFocus(dir Direction) bool
	MoveCaret(dir Direction) bool
}

FocusHandle is the interface for the focus management implementation injected by the engine. It decouples the logical DOM from the focus package.

type FocusScope ΒΆ

type FocusScope struct {
	// Root is the logical node that acts as the boundary for tab navigation
	// and focus queries while this scope is active. Must not be nil.
	Root Node

	// Autofocus is the initial focus target when the scope is pushed.
	// If nil no autofocus is applied; focus stays on the previous element
	// until a navigation or programmatic Focus call.
	Autofocus Element

	// PreviousFocus is captured automatically on PushScope. It is restored
	// with ReasonRestore when PopScope is called. Do not set this field
	// manually; Manager writes it on PushScope.
	PreviousFocus Element
}

FocusScope is a focus containment region. While a FocusScope is active, tab navigation and focusable-filter queries are restricted to the subtree rooted at Root.

Lifecycle:

  • PushScope captures the current focus into PreviousFocus.
  • PopScope restores PreviousFocus with ReasonRestore, or blurs if the previous node is no longer focusable.

type Focusable ΒΆ

type Focusable interface {
	IsFocusable() bool
	Focus()
	Blur()
}

Focusable indicates that an element can be focused.

type FormControl ΒΆ

type FormControl interface {
	Node
	Name() string
	Value() any
}

FormControl represents a DOM element that carries a name and a value for form submission.

type Implementation ΒΆ

type Implementation struct {
	NewDocument     func() Document
	NewElement      func(doc Document, tag string, self Node) Element
	NewTextNode     func(doc Document, data string, self Node) TextNode
	LayoutChildren  func(n Node) iter.Seq[Node]
	Outer           func(n Node) Node
	IsUANode        func(n Node) bool
	UARoot          func(el Element) Node
	DefaultScroller func(host Element) event.Scrollable
}

Implementation defines the set of factory functions and utilities provided by the internal/dom package.

type Kind ΒΆ

type Kind int

Kind identifies the type of a Node. It is used by Node.Kind() to allow callers to determine the concrete type of a Node without relying on Go's type system or incurring the cost of a type assertion. The set of possible values is fixed by the current DOM implementation and may not be extended by user code.

const (
	KindDocument Kind = iota
	KindElement
	KindText
)

func (Kind) String ΒΆ

func (k Kind) String() string

type Lifecycle ΒΆ

type Lifecycle interface {
	// OnConnected is called when the node becomes reachable from the Document
	// root. The node's IsConnected predicate is already true when this fires.
	OnConnected()

	// OnDisconnected is called when the node is about to leave the live tree.
	// The node's IsConnected predicate is still true when this fires; it
	// becomes false after the callback returns.
	OnDisconnected()
}

Lifecycle is an optional interface that DOM nodes may implement to receive notifications when they enter or leave the live tree (i.e. when they become connected to or disconnected from the Document root).

The attach walk fires OnConnected in pre-order (parent before children). The detach walk fires OnDisconnected in post-order (children before parent). Both callbacks run synchronously inside the mutation call that triggered the walk, before the mutation returns to the caller.

Self- and descendant-mutations are permitted inside either callback. Ancestor-mutations are forbidden and panic in development builds.

type Node ΒΆ

type Node interface {
	event.EventTarget

	// Kind returns the kind of this node (Document, Element, or Text).
	Kind() Kind

	// NodeName returns the name of this node. For Element nodes it is the tag
	// name; for Text nodes it is "#text"; for Document it is "#document".
	NodeName() string

	// Parent returns the parent Node, or nil if this node is the Document root.
	Parent() Node

	// ParentElement returns the parent Node if it is an Element, or nil if
	// this node has no parent or its parent is not an Element.
	ParentElement() Element

	// NextSibling returns the next sibling Node, or nil if there is none.
	NextSibling() Node

	// PreviousSibling returns the previous sibling Node, or nil if there is none.
	PreviousSibling() Node

	// OwnerDocument returns the Document that owns this node.
	OwnerDocument() Document

	// IsConnected reports whether this node is reachable from the Document
	// root. The document itself is always connected. The value is toggled by
	// the attach/detach walks run inside AppendChild, InsertBefore,
	// RemoveChild, and ReplaceChild.
	IsConnected() bool

	// AppendChild adds child as the last child of this node and returns child.
	AppendChild(child Node) Node

	// InsertBefore inserts newChild immediately before ref and returns newChild.
	// If ref is nil the call is equivalent to AppendChild.
	InsertBefore(newChild, ref Node) Node

	// RemoveChild removes child from this node and returns child.
	RemoveChild(child Node) Node

	// ReplaceChild inserts newChild in the position occupied by oldChild,
	// removes oldChild from the tree, and returns oldChild.
	ReplaceChild(newChild, oldChild Node) Node

	// FirstChild returns the first child Node, or nil if the node has no children.
	FirstChild() Node

	// LastChild returns the last child Node, or nil if the node has no children.
	LastChild() Node

	// HasChildNodes reports whether this node has any children.
	HasChildNodes() bool

	// Contains reports whether this node is an ancestor of descendant.
	Contains(descendant Node) bool

	// ChildNodes returns an iterator over direct children in document order.
	ChildNodes() iter.Seq[Node]

	// Unwrap returns the underlying Node being decorated by this wrapper, or nil
	// if this node is a base implementation.
	Unwrap() Node

	// TextContent returns the concatenation of all text content in this node's subtree. For Text
	// nodes it is the same as Data(). For Element and Document nodes it is the concatenation of
	// the TextContent of all descendant Text nodes in document order.
	TextContent() string

	// CloneNode returns a new Node that is a copy of this node. If deep is true, the clone also
	// includes clones of all descendant nodes; otherwise the clone has no children. The returned
	// node is detached (no parent).
	CloneNode(deep bool) Node

	// EventTarget returns the user-visible event target for this node. For
	// nodes in a UA shadow subtree, this returns the host element; otherwise
	// it returns the node itself (ADR-0036).
	EventTarget() event.EventTarget
}

Node is the base interface for every node in the logical DOM tree. It carries parent/sibling links and owner-document reference. DOM nodes do not own dirty flags, layout state, or computed style β€” those live on render.Object.

func Outer ΒΆ

func Outer(n Node) Node

Outer returns the self wrapper for el when one has been adopted.

func UARoot ΒΆ

func UARoot(el Element) Node

UARoot returns the UA shadow subtree root attached to el, or nil.

type Range ΒΆ

type Range interface {
	// StartContainer returns the Node within which the Range starts.
	StartContainer() Node
	// StartOffset returns a number representing where in the StartContainer
	// the Range starts. For Text nodes, this is the number of runes from
	// the start of the data.
	StartOffset() int
	// EndContainer returns the Node within which the Range ends.
	EndContainer() Node
	// EndOffset returns a number representing where in the EndContainer the
	// Range ends.
	EndOffset() int

	// SetStart sets the start position of a Range.
	SetStart(node Node, offset int)
	// SetEnd sets the end position of a Range.
	SetEnd(node Node, offset int)
	// Collapse collapses the Range to one of its boundary points.
	Collapse(toStart bool)
	// IsCollapsed reports whether the Range's start and end points are the same.
	IsCollapsed() bool

	// String returns the text content of the Range.
	String() string
}

Range represents a fragment of a document that can contain nodes and parts of text nodes.

type Selection ΒΆ

type Selection interface {
	// RangeCount returns the number of ranges in the selection.
	RangeCount() int
	// GetRangeAt returns the range at the specified index.
	GetRangeAt(index int) Range
	// AddRange adds a Range to a Selection.
	AddRange(r Range)
	// RemoveAllRanges removes all ranges from the selection.
	RemoveAllRanges()
	// String returns a string currently being represented by the selection
	// (the combined text of all its ranges).
	String() string
}

Selection represents the range of text selected by the user or the current caret position.

type SpatialCaret ΒΆ

type SpatialCaret interface {
	// MoveCaret moves the internal caret in the specified direction.
	// Returns boundaryReached=true if the cursor hits the edge and focus should jump.
	MoveCaret(dir Direction) (boundaryReached bool)

	// ResetCaret positions the caret at the entry boundary when focus shifts in.
	ResetCaret(dir Direction)
}

SpatialCaret is implemented by elements that manage an internal text cursor. It allows the engine's default spatial focus handler to seamlessly coordinate internal caret movements and global element-level focus jumps.

type TextNode ΒΆ

type TextNode interface {
	Node

	// Data returns the current text content.
	Data() string

	// SetData replaces the text content and notifies the parent's render object.
	SetData(data string)
}

TextNode is a leaf node that carries character data. It has no children.

func NewTextNode ΒΆ

func NewTextNode(doc Document, data string, self Node) TextNode

NewTextNode allocates a TextNode with the given data and owner document.

type View ΒΆ

type View interface {
	// GetBoundingClientRect returns the physical terminal rectangle occupied
	// by the node.
	GetBoundingClientRect(n Node) (geom.Rect, bool)

	// GetComputedStyle returns the fully-resolved computed style for the node.
	GetComputedStyle(n Node) *style.Computed

	// GetSize returns the physical size of the node's layout fragment.
	GetSize(n Node) (geom.Size, bool)

	// GetMaxScroll returns the maximum horizontal and vertical scroll offsets
	// for the node.
	GetMaxScroll(n Node) (x, y int)

	// GetCaretPosition returns the content-relative coordinates (relative to
	// the content box origin) for the given rune offset in the node.
	GetCaretPosition(n Node, offset int) (geom.Point, bool)

	// MoveCursorVertically returns the new rune offset after moving up (delta < 0)
	// or down (delta > 0) from the current offset. x and y are the current
	// content-relative coordinates used for vertical tracking.
	MoveCursorVertically(n Node, offset int, delta int, x, y int) int

	// ByteOffsetAtPoint returns the byte offset within the node at the given
	// content-relative coordinates.
	ByteOffsetAtPoint(n Node, x, y int) int

	// NodeAtPoint returns the leaf node and its rune offset at the given
	// screen coordinates.
	NodeAtPoint(x, y int) (Node, int)

	// ViewportSize returns the current viewport size (terminal dimensions).
	ViewportSize() geom.Size

	// LayoutVersion returns a monotonic version number of the layout state.
	// Incrementing this means the layout has been recomputed.
	LayoutVersion() uint64
}

View provides read-only access to computed style and layout information for a DOM tree without coupling the DOM to the render engine.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL