hck

package module
v0.0.0-...-72c6ee3 Latest Latest
Warning

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

Go to latest
Published: May 18, 2016 License: BSD-3-Clause Imports: 4 Imported by: 0

README

HTML construction kit

NOTE
The library is still in flux - the api is not yet stable and it is not tested, just written.

HTML construction kit (hck) builds upon golang.org/x/net/html (html) and simpifies tree construction and modification.

While html is great for parsing and rendering, its api is rather unfriendly when one wants to build or modify a html document.
The nodes carry references to their parents, both siblings and the first and last children. It's easy to forget to update one and all referenced nodes potentially have to be updated, too. Modification and querying of attributes is also unwieldly.

hck stores a minimal representation of a node and relies on a Cursor to provide context on navigation. Each node only references its children and can easily be moved around.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Classes

func Classes(val string) []string

Classes splits the value of the class attribute into the individual classes.

Types

type Attributes

type Attributes []html.Attribute

func (*Attributes) AddClass

func (as *Attributes) AddClass(class string)

func (Attributes) Class

func (as Attributes) Class() string

Class retrieves the value of the "class" attribute

func (*Attributes) DelClass

func (as *Attributes) DelClass(class string)

func (*Attributes) Delete

func (as *Attributes) Delete(a *html.Attribute)

func (Attributes) Get

func (as Attributes) Get(key, namespace string) string

func (Attributes) ID

func (as Attributes) ID() string

func (Attributes) Len

func (as Attributes) Len() int

func (Attributes) Less

func (as Attributes) Less(i, j int) bool

func (*Attributes) Set

func (as *Attributes) Set(key, namespace, value string) (was string)

func (*Attributes) SetClass

func (as *Attributes) SetClass(val string) (was string)

func (*Attributes) SetID

func (as *Attributes) SetID(val string) (was string)

func (Attributes) Swap

func (as Attributes) Swap(i, j int)

type Builder

type Builder interface {
	// set namespace on current tag
	Namespace(namespace string) Builder

	// add attribute
	Attr(key, value string) Builder

	// add attribute with namespace
	AttrNS(key, namespace, value string) Builder

	// add tag as child, return builder inside child
	Tag(tag string) Builder

	// add textnode as child
	Text(text string) Builder

	// add multiple child nodes
	Children(children ...*Node) Builder

	// return builder inside parent
	Leave() Builder

	// retrieve the current node
	Node() *Node

	// retrieve the root node
	Root() *Node
}

func Build

func Build(n *Node) Builder

func Tag

func Tag(tag string) Builder

type Cursor

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

Cursor points to a node and stores the path to it. It simplifies navigation on nodes.

You must not modify the path to a cursor and the sibling nodes.

func (*Cursor) Cursor

func (c *Cursor) Cursor() *Cursor

Cursor creates a new cursor pointing to the same node.

func (*Cursor) Depth

func (c *Cursor) Depth() int

Depth retrieves the number of ancestor nodes.

func (*Cursor) FirstChild

func (c *Cursor) FirstChild() *Node

Parent moves to and retrieves the first child node. If no children exist, the cursor does not move and nil is returned.

func (*Cursor) LastChild

func (c *Cursor) LastChild() *Node

Parent moves to and retrieves the first child node. If no children exist, the cursor does not move and nil is returned.

func (*Cursor) Next

func (c *Cursor) Next() *Node

Next moves to and retrieves the depth-first next node. If no next node exists, the cursor does not move and nil is returned.

func (*Cursor) NextSibling

func (c *Cursor) NextSibling() *Node

NextSibling moves to and retrieves the next sibling node. If no next sibling exists, the cursor does not move and nil is returned.

func (*Cursor) Node

func (c *Cursor) Node() *Node

Node retrieves the current node.

func (*Cursor) Parent

func (c *Cursor) Parent() *Node

Parent moves to and retrieves the parent node. If no parent exists, the cursor does not move and nil is returned.

func (*Cursor) Path

func (c *Cursor) Path() Path

func (*Cursor) Prev

func (c *Cursor) Prev() *Node

Prev moves to and retrieves the depth-first previous node. If no previous node exists, the cursor does not move and nil is returned.

func (*Cursor) PrevSibling

func (c *Cursor) PrevSibling() *Node

PrevSibling moves to and retrieves the previous sibling node. If no previous sibling exists, the cursor does not move and nil is returned.

func (*Cursor) Seek

func (c *Cursor) Seek(m Matcher) bool

func (*Cursor) Swap

func (c *Cursor) Swap(n *Node) (prev *Node, ok bool)

type Finder

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

func (*Finder) All

func (f *Finder) All() []*Node

All retrieves all remaining findable nodes.

func (*Finder) Each

func (f *Finder) Each(do func(Path) (stop bool))

Each iterates over all found nodes and calls do.

func (Finder) Find

func (f Finder) Find(m Matcher) *Finder

func (Finder) Next

func (f Finder) Next() *Node

func (Finder) Node

func (fs Finder) Node() *Node

func (Finder) Path

func (fs Finder) Path() Path

type Match

type Match func(*Node) bool

func (Match) Match

func (m Match) Match(n *Node) bool

type Matcher

type Matcher interface {
	Match(*Node) bool
}

func MatchAll

func MatchAll(ms ...Matcher) Matcher

func MatchAny

func MatchAny(ms ...Matcher) Matcher

func MatchAttribute

func MatchAttribute(key, namespace, value string) Matcher

func MatchChild

func MatchChild(m Matcher) Matcher

func MatchNamespace

func MatchNamespace(namespace string) Matcher

func MatchTag

func MatchTag(tag string) Matcher

func MatchTagNS

func MatchTagNS(tag, namespace string) Matcher

func MatchType

func MatchType(t html.NodeType) Matcher

func Matchers

func Matchers(ms ...Match) []Matcher

type Node

type Node struct {
	Children  Siblings
	Namespace string
	Data      string
	Attributes
	Type html.NodeType
}

Node is an alternative to golang.org/x/net/html.Node intended for dom mutation. It stores a minimal amount of references that have to be updated on transformations.

func Convert

func Convert(h *html.Node) *Node

Convert a /x/net/html.Node to a Node.

func Document

func Document(children ...*Node) *Node

func Parse

func Parse(r io.Reader) (*Node, error)

Parse a tree from r.

func Text

func Text(t string) *Node

func (*Node) Attr

func (n *Node) Attr(key string) string

Attr retrieves the value of an attribute.

func (*Node) AttrNS

func (n *Node) AttrNS(key, namespace string) string

Attr retrieves the value of an attribute with a namespace.

func (*Node) Attribute

func (n *Node) Attribute(key, namespace string) *html.Attribute

Attribute retrieves a pointer to the Attribute with the given key and namespace. If none exists, nil is returned.

func (*Node) Clone

func (n *Node) Clone() *Node

Clone retrieves a copy of the node.

func (*Node) Convert

func (n *Node) Convert() (*html.Node, error)

Convert a Node to a /x/net/html.Node. If a child node is an ancestor of its own parent, an error will be returned.

func (*Node) Cursor

func (n *Node) Cursor() *Cursor

func (*Node) Each

func (n *Node) Each(m Matcher, f func(n *Node))

func (*Node) Find

func (n *Node) Find(ms ...Matcher) *Finder

func (*Node) HasCycle

func (n *Node) HasCycle() bool

HasCycle reports whether any reachable node is the ancestor of its own parents.

func (*Node) Match

func (n *Node) Match(m *Node) bool

func (*Node) PathTo

func (r *Node) PathTo(n *Node) Path

func (*Node) Render

func (n *Node) Render(w io.Writer) error

func (*Node) SetAttr

func (n *Node) SetAttr(key, value string) (was string)

func (*Node) SetAttrNS

func (n *Node) SetAttrNS(key, namespace, value string) (was string)

func (*Node) Swap

func (n *Node) Swap(n2 *Node) *Node

Swap state with another node and retrieve that node.

type Path

type Path []*Node

Path contains all nodes traversed to get to its last node.

func (Path) Cursor

func (p Path) Cursor() *Cursor

func (Path) Index

func (p Path) Index(m Matcher) int

Index retrieves the index of the first matching node. It returns -1 if no match is found.

func (Path) Node

func (p Path) Node() *Node

Node retrieves the last node. If the path is empty, it returns nil.

func (Path) Prev

func (p Path) Prev() Path

type Siblings

type Siblings []*Node

func (Siblings) Index

func (s Siblings) Index(m Matcher) int

Index retrieves the index of the first matching node. It returns -1 if no match is found.

func (Siblings) Render

func (s Siblings) Render(w io.Writer) error

Render nodes to a writer. nil nodes are skipped.

func (Siblings) Splice

func (s Siblings) Splice(i, del int, ns ...*Node) Siblings

Splice copies the siblings and modifies it by deleting del nodes starting at i and inserting ns there.

func (Siblings) SplitBefore

func (s Siblings) SplitBefore(m Matcher) (front, back Siblings)

SplitBefore retrieves siblings up to and starting with the first node from which a match is reachable. If no match is found, back is empty.

Jump to

Keyboard shortcuts

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