GoHtml

package module
v0.0.0-beta.0 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2025 License: MIT Imports: 7 Imported by: 0

README

GoHTML logo

GoHTML

A powerful and comprehensive HTML parser and DOM manipulation library for Go, bringing JavaScript-like DOM operations to the Go ecosystem.

Note: Still on devolopment.

Documentation

Overview

A powerful and comprehensive HTML parser and DOM manipulation library for Go, bringing JavaScript-like DOM operations to the Go ecosystem.

Index

Constants

View Source
const (
	Area          = "area"
	Base   string = "base"
	Br     string = "br"
	Col    string = "col"
	Embed  string = "embed"
	Hr     string = "hr"
	Img    string = "img"
	Input  string = "input"
	Link   string = "link"
	Meta   string = "meta"
	Param  string = "param"
	Source string = "source"
	Track  string = "track"
	Wbr    string = "wbr"
)
View Source
const (
	//This is not a void el. but added it anyway.
	DOCTYPEDTD string = "!DOCTYPE"
)

A DTD defines the structure and the legal elements and attributes of an XML document.

Variables

View Source
var (
	SyntaxError error = fmt.Errorf("Syntax error")
)
View Source
var (
	VoidTags = map[string]bool{
		"area":     true,
		"base":     true,
		"br":       true,
		"col":      true,
		"embed":    true,
		"hr":       true,
		"img":      true,
		"input":    true,
		"link":     true,
		"meta":     true,
		"param":    true,
		"source":   true,
		"track":    true,
		"wbr":      true,
		"!doctype": true,
	}
)

Functions

func ApplySaveChanges

func ApplySaveChanges(node *Node)

ApplySaveChanges replaces the nodes previous and parent node with the given node.

func Encode

func Encode(w io.Writer, rootNode *Node)

Encode writes to w encoding of rootNode

func IsVoidTag

func IsVoidTag(tagName string) bool

IsVoidTag returns whether the tagName is a void tag or DTD

func NodeTreeToHTML

func NodeTreeToHTML(rootNode *Node) string

NodeTreeToHTML returns encoding of node-tree as a string.

Types

type Node

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

The DOM Node struct is an abstract base class upon which many other DOM API objects are based, thus letting those object types to be used similarly and often interchangeably.

func CloneNode

func CloneNode(node *Node) *Node

CloneNode copy the node.

func CreateNode

func CreateNode(tagName string) *Node

CreateNode returns a initialized new node.

func CreateTextNode

func CreateTextNode(text string) *Node

CreateTextNode returns a new node that represents the given text.

func Decode

func Decode(rd io.Reader) (*Node, error)

Decode reads from rd and create a node-tree. Then returns the root node and an error. If error were to occur it would be SyntaxError.

func DeepCloneNode

func DeepCloneNode(node *Node) *Node

DeepCloneNode clones the node without having references to it's original parent node, previous node and next node.

func HTMLToNodeTree

func HTMLToNodeTree(html string) (*Node, error)

HTMLToNodeTree return html code as a node-tree. If error were to occur it would be SyntaxError.

func (*Node) Append

func (node *Node) Append(newNode *Node)

Append inserts the newNode to end of the node chain.

func (*Node) AppendChild

func (node *Node) AppendChild(childNode *Node)

The AppendChild() method of the Node adds a node to the end of the list of children of a specified parent node.

func (*Node) AppendText

func (node *Node) AppendText(text string)

AppendText add text to the node.

func (*Node) GetAttribute

func (node *Node) GetAttribute(attributeName string) string

GetAttribute returns the specified attribute form the node.

func (*Node) GetChildNode

func (node *Node) GetChildNode() *Node

GetChildNode returns the first child elements of this node.

func (*Node) GetFirstNode

func (node *Node) GetFirstNode() *Node

GetFirstNode returns the first node of the node branch.

func (*Node) GetInnerText

func (node *Node) GetInnerText() string

GetInnerText returns all of the text inside the node.

func (*Node) GetLastNode

func (node *Node) GetLastNode() *Node

GetLastNode returns the last node in the node branch.

func (*Node) GetNextNode

func (node *Node) GetNextNode() *Node

GetNextNode returns node next to the node.

func (*Node) GetParent

func (node *Node) GetParent() *Node

GetParent returns a pointer to the parent node.

func (*Node) GetPreviousNode

func (node *Node) GetPreviousNode() *Node

GetPreviousNode returns the previous node.

func (*Node) GetTagName

func (node *Node) GetTagName() string

Returns a string with the name of the tag for the given node.

func (*Node) GetText

func (node *Node) GetText() string

GetText returns text on the node. This does not returns text on it's child nodes. If you also wants child nodes text use GetInnerText method on the node.

func (*Node) IterateAttributes

func (node *Node) IterateAttributes(callback func(attribute, value string))

IterateAttributes calls callback at every attribute in the node by passing attribute and value of the each node to the callback.

func (*Node) RemoveNode

func (node *Node) RemoveNode()

RemoveNode removes the node from the branch safely.

func (*Node) SetAttribute

func (node *Node) SetAttribute(attribute, value string)

SetAttribute add a attribute to the node.

func (*Node) SetNextNode

func (node *Node) SetNextNode(nextNode *Node)

SetNextNode make nodes next node as nextNode.

func (*Node) SetPreviousNode

func (node *Node) SetPreviousNode(previousNode *Node)

SetPreviousNode sets nodes previous node to previousNode.

func (*Node) SetTagName

func (node *Node) SetTagName(tagName string)

SetTagName changes the html tag name to the tagName.

func (*Node) SetText

func (node *Node) SetText(text string)

SetText add text to the node.

type Traverser

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

func GetTraverser

func GetTraverser(startingNode *Node) Traverser

GetTraverser returns a new traverser that can be used to navigate the node tree.

func (*Traverser) GetCurrentNode

func (t *Traverser) GetCurrentNode() *Node

GetCurrentNode returns the current node.

func (*Traverser) Next

func (t *Traverser) Next() *Node

Next returns the node next to current node and change CurrentNode to the new node.

func (*Traverser) Previous

func (t *Traverser) Previous() *Node

Previous returns the previous node and change CurrentNode to the new node.

func (*Traverser) SetCurrentNodeTo

func (t *Traverser) SetCurrentNodeTo(newNode *Node)

SetCurrentNodeTo changes the current node to the newNode.

func (*Traverser) Walkthrough

func (t *Traverser) Walkthrough(callback func(node *Node))

TODO: use a linked stack Walkthrough traverse the node tree from the current node to the end of the node tree by visiting every node.

Jump to

Keyboard shortcuts

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