dom

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2025 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Overview

Package dom provides virtual DOM structures and operations for HTML parsing and manipulation.

Package dom provides virtual DOM structures and operations for HTML parsing and manipulation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetAttribute

func GetAttribute(element *VElement, name string) string

GetAttribute gets the value of an attribute on an element. Returns an empty string if the attribute doesn't exist.

func GetInnerText

func GetInnerText(node VNode, normalizeSpaces bool) string

GetInnerText returns the inner text of an element or text node. If normalizeSpaces is true, consecutive whitespace is normalized to a single space.

func GetLinkDensity

func GetLinkDensity(element *VElement) float64

GetLinkDensity calculates the ratio of link text to all text in an element. Returns a value between 0 and 1, where higher values indicate more links.

func GetTextDensity

func GetTextDensity(element *VElement) float64

GetTextDensity calculates the ratio of text to child elements in an element. Returns a value where higher values indicate more text-dense content.

func HasAncestorTag

func HasAncestorTag(node VNode, tagName string, maxDepth int) bool

HasAncestorTag checks if a node has an ancestor with the specified tag name. If maxDepth is less than or equal to 0, all ancestors are checked.

func IsProbablyVisible

func IsProbablyVisible(node *VElement) bool

IsProbablyVisible checks if an element is likely to be visible based on its attributes.

func IsVElement

func IsVElement(node VNode) bool

IsVElement checks if a node is a VElement.

func IsVText

func IsVText(node VNode) bool

IsVText checks if a node is a VText.

Types

type ReadabilityData

type ReadabilityData struct {
	ContentScore float64
}

ReadabilityData stores readability-specific information for a node.

type VDocument

type VDocument struct {
	DocumentElement *VElement
	Body            *VElement
	BaseURI         string
	DocumentURI     string
}

VDocument represents a virtual DOM document.

func NewVDocument

func NewVDocument(documentElement, body *VElement) *VDocument

NewVDocument creates a new virtual DOM document with the given document element and body.

type VElement

type VElement struct {
	TagName    string
	Attributes map[string]string
	Children   []VNode
	// contains filtered or unexported fields
}

VElement represents an element node in the virtual DOM.

func AsVElement

func AsVElement(node VNode) (*VElement, bool)

AsVElement attempts to convert a VNode to a VElement. Returns the VElement and true if successful, otherwise nil and false.

func CreateElement

func CreateElement(tagName string) *VElement

CreateElement creates a new element with the given tag name.

func GetElementsByTagName

func GetElementsByTagName(element *VElement, tagName string) []*VElement

GetElementsByTagName returns all elements with the specified tag name(s) in the element tree. If tagName is "*", it returns all elements.

func GetElementsByTagNames

func GetElementsByTagNames(element *VElement, tagNames []string) []*VElement

GetElementsByTagNames returns all elements with any of the specified tag names in the element tree.

func GetNodeAncestors

func GetNodeAncestors(node *VElement, maxDepth int) []*VElement

GetNodeAncestors returns the ancestor elements of a node up to a specified depth. If maxDepth is less than or equal to 0, all ancestors are returned.

func NewVElement

func NewVElement(tagName string) *VElement

NewVElement creates a new element node with the given tag name.

func (*VElement) AppendChild

func (e *VElement) AppendChild(child VNode)

AppendChild adds a child node to this element.

func (*VElement) ClassName

func (e *VElement) ClassName() string

ClassName returns the class attribute of this element, or an empty string if it has no class.

func (*VElement) GetAttribute

func (e *VElement) GetAttribute(name string) string

GetAttribute gets the value of an attribute on this element.

func (*VElement) GetReadabilityData

func (n *VElement) GetReadabilityData() *ReadabilityData

GetReadabilityData returns the readability-specific data for this node.

func (*VElement) HasAttribute

func (e *VElement) HasAttribute(name string) bool

HasAttribute checks if this element has the specified attribute.

func (*VElement) ID

func (e *VElement) ID() string

ID returns the id attribute of this element, or an empty string if it has no id.

func (*VElement) Parent

func (n *VElement) Parent() *VElement

Parent returns the parent element of this node.

func (*VElement) SetAttribute

func (e *VElement) SetAttribute(name, value string)

SetAttribute sets an attribute on this element.

func (*VElement) SetParent

func (n *VElement) SetParent(parent *VElement)

SetParent sets the parent element of this node.

func (*VElement) SetReadabilityData

func (n *VElement) SetReadabilityData(data *ReadabilityData)

SetReadabilityData sets the readability-specific data for this node.

func (*VElement) Type

func (n *VElement) Type() VNodeType

Type returns the node type.

type VNode

type VNode interface {
	// Type returns the node type.
	Type() VNodeType
	// Parent returns the parent element of this node, or nil if it has no parent.
	Parent() *VElement
	// SetParent sets the parent element of this node.
	SetParent(parent *VElement)
	// GetReadabilityData returns the readability-specific data for this node.
	GetReadabilityData() *ReadabilityData
	// SetReadabilityData sets the readability-specific data for this node.
	SetReadabilityData(data *ReadabilityData)
}

VNode is the interface for all virtual DOM nodes.

type VNodeType

type VNodeType string

VNodeType represents the type of a virtual DOM node.

const (
	// ElementNode represents an HTML element node.
	ElementNode VNodeType = "element"
	// TextNode represents a text node.
	TextNode VNodeType = "text"
)

type VText

type VText struct {
	TextContent string
	// contains filtered or unexported fields
}

VText represents a text node in the virtual DOM.

func AsVText

func AsVText(node VNode) (*VText, bool)

AsVText attempts to convert a VNode to a VText. Returns the VText and true if successful, otherwise nil and false.

func CreateTextNode

func CreateTextNode(content string) *VText

CreateTextNode creates a new text node with the given content.

func NewVText

func NewVText(textContent string) *VText

NewVText creates a new text node with the given text content.

func (*VText) GetReadabilityData

func (n *VText) GetReadabilityData() *ReadabilityData

GetReadabilityData returns the readability-specific data for this node.

func (*VText) Parent

func (n *VText) Parent() *VElement

Parent returns the parent element of this node.

func (*VText) SetParent

func (n *VText) SetParent(parent *VElement)

SetParent sets the parent element of this node.

func (*VText) SetReadabilityData

func (n *VText) SetReadabilityData(data *ReadabilityData)

SetReadabilityData sets the readability-specific data for this node.

func (*VText) Type

func (n *VText) Type() VNodeType

Type returns the node type.

Jump to

Keyboard shortcuts

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