parser

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 30, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package parser provides HTML parsing with rich node traversal. It wraps golang.org/x/net/html nodes into Adaptable structs with parent, children, sibling navigation, text extraction, and attribute access.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Adaptable

type Adaptable struct {
	// contains filtered or unexported fields
}

Adaptable wraps an html.Node with rich traversal and extraction methods.

func Body

func Body(root *Adaptable) *Adaptable

Body returns the <body> element from a parsed document, or nil.

func Head(root *Adaptable) *Adaptable

Head returns the <head> element from a parsed document, or nil.

func NewAdaptable

func NewAdaptable(n *html.Node) *Adaptable

NewAdaptable wraps an html.Node into an Adaptable.

func Parse

func Parse(htmlStr string) (*Adaptable, error)

Parse parses an HTML string and returns the root Adaptable node.

func ParseFragment

func ParseFragment(htmlStr string) ([]*Adaptable, error)

ParseFragment parses an HTML fragment within a body context.

func ParseReader

func ParseReader(r io.Reader) (*Adaptable, error)

ParseReader parses HTML from a reader and returns the root Adaptable node.

func (*Adaptable) AllChildren

func (a *Adaptable) AllChildren() []*Adaptable

AllChildren returns all direct child nodes including text nodes.

func (*Adaptable) AllElements

func (a *Adaptable) AllElements() []*Adaptable

AllElements returns all descendant elements in document order.

func (*Adaptable) AllText

func (a *Adaptable) AllText() string

AllText returns all text content recursively from this element and descendants.

func (*Adaptable) Ancestors

func (a *Adaptable) Ancestors() []*Adaptable

Ancestors returns all ancestor elements from parent to root.

func (*Adaptable) Attr

func (a *Adaptable) Attr(name string) string

Attr returns the value of the named attribute, or empty string if not found.

func (*Adaptable) AttrKeys

func (a *Adaptable) AttrKeys() []string

AttrKeys returns all attribute key names.

func (*Adaptable) AttrValues

func (a *Adaptable) AttrValues() []string

AttrValues returns all attribute values.

func (*Adaptable) Attrs

func (a *Adaptable) Attrs() map[string]string

Attrs returns all attributes as a map.

func (*Adaptable) ChildTags

func (a *Adaptable) ChildTags() []string

ChildTags returns tag names of all direct child elements.

func (*Adaptable) Children

func (a *Adaptable) Children() []*Adaptable

Children returns all direct child elements (skipping text/comment nodes).

func (*Adaptable) Depth

func (a *Adaptable) Depth() int

Depth returns the nesting depth of this element (root = 0).

func (*Adaptable) Find

func (a *Adaptable) Find(tag string) *Adaptable

Find finds the first descendant element matching the given tag name.

func (*Adaptable) FindAll

func (a *Adaptable) FindAll(tag string) []*Adaptable

FindAll finds all descendant elements matching the given tag name.

func (*Adaptable) FindByAttr

func (a *Adaptable) FindByAttr(key, value string) []*Adaptable

FindByAttr finds all descendant elements with the given attribute key=value.

func (*Adaptable) FindByText

func (a *Adaptable) FindByText(text string) []*Adaptable

FindByText finds all descendant elements containing the given text.

func (*Adaptable) HTML

func (a *Adaptable) HTML() string

HTML returns the outer HTML of this element.

func (*Adaptable) HasAttr

func (a *Adaptable) HasAttr(name string) bool

HasAttr checks if the element has the named attribute.

func (*Adaptable) InnerHTML

func (a *Adaptable) InnerHTML() string

InnerHTML returns the inner HTML of this element.

func (*Adaptable) NextSibling

func (a *Adaptable) NextSibling() *Adaptable

NextSibling returns the next sibling element, or nil.

func (*Adaptable) Node

func (a *Adaptable) Node() *html.Node

Node returns the underlying html.Node.

func (*Adaptable) Parent

func (a *Adaptable) Parent() *Adaptable

Parent returns the parent element, or nil.

func (*Adaptable) Path

func (a *Adaptable) Path() []string

Path returns the DOM path as a slice of tag names from root to this element.

func (*Adaptable) PathString

func (a *Adaptable) PathString() string

PathString returns the DOM path as a slash-separated string.

func (*Adaptable) PrevSibling

func (a *Adaptable) PrevSibling() *Adaptable

PrevSibling returns the previous sibling element, or nil.

func (*Adaptable) SiblingTags

func (a *Adaptable) SiblingTags() []string

SiblingTags returns the tag names of all sibling elements.

func (*Adaptable) Siblings

func (a *Adaptable) Siblings() []*Adaptable

Siblings returns all sibling elements (excluding self).

func (*Adaptable) Tag

func (a *Adaptable) Tag() string

Tag returns the element's tag name. Returns empty string for non-element nodes.

func (*Adaptable) Text

func (a *Adaptable) Text() string

Text returns the direct text content of this element (not recursive).

Jump to

Keyboard shortcuts

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