helpers

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package helpers provides HTML node manipulation utilities for vuego.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AppendAttr

func AppendAttr(n *html.Node, key, value string)

AppendAttr appends a value to an attribute on a node. If the attribute exists, the new value replaces it. If it doesn't exist, it is added.

func CloneNode

func CloneNode(n *html.Node) *html.Node

CloneNode creates a shallow copy of a node without sharing children or siblings. Attributes are shared (not copied) to avoid unnecessary allocations.

func CompareHTML

func CompareHTML(tb testing.TB, want, got, template, data []byte) bool

CompareHTML parses two HTML strings and returns true if their DOMs match. It ignores pure-whitespace text nodes and compares attributes order-insensitively.

func ContainsPipe

func ContainsPipe(expr string) bool

ContainsPipe checks if an expression contains a pipe operator.

func CountChildren

func CountChildren(n *html.Node) int

CountChildren counts the number of child nodes of the given node. This is useful for preallocating slices with the correct capacity.

func DeepCloneNode

func DeepCloneNode(n *html.Node) *html.Node

DeepCloneNode creates a deep copy of a node including all children.

func GetAttr

func GetAttr(n *html.Node, key string) string

GetAttr returns the value of an attribute by key, or empty string if not found.

func GetBodyNode

func GetBodyNode() *html.Node

GetBodyNode returns a cached body element suitable for html.ParseFragment.

func HasAttr

func HasAttr(n *html.Node, key string) bool

HasAttr checks if a node has an attribute by key (regardless of its value).

func IsComplexExpr

func IsComplexExpr(expr string) bool

IsComplexExpr checks if an expression contains operators like ==, ===, !=, <, >, etc.

func IsFunctionCall

func IsFunctionCall(expr string) bool

IsFunctionCall checks if an expression looks like a function call.

func IsIdentifier

func IsIdentifier(s string) bool

IsIdentifier checks if a string is a valid identifier (starts with letter or underscore, followed by letters, digits, or underscores).

func IsIdentifierChar

func IsIdentifierChar(ch rune) bool

IsIdentifierChar checks if a rune is valid in an identifier (letter, digit, or underscore).

func IsTruthy

func IsTruthy(val any) bool

IsTruthy converts a value to boolean following Vue semantics. For bound attributes, false values should not render the attribute.

func NeedsHTMLEscape

func NeedsHTMLEscape(s string) bool

NeedsHTMLEscape checks if a string contains characters that need HTML escaping. Returns true if the string contains &, <, >, ", or ' characters. This avoids calling html.EscapeString which always allocates a new string.

func NewNode

func NewNode() *html.Node

NewNode creates or reuses a node from the pool, clearing any previous state. This reduces memory allocations and GC pressure during template rendering.

func NormalizeComparisonOperators

func NormalizeComparisonOperators(expr string) string

NormalizeComparisonOperators coalesces strict comparison operators (=== and !==) to loose operators (== and !=). This is needed because the underlying expr evaluator supports == and != but not === and !==.

func RemoveAttr

func RemoveAttr(n *html.Node, key string)

RemoveAttr removes an attribute from a node by key.

func SetAttr

func SetAttr(n *html.Node, key, value string)

SetAttr sets or updates an attribute on a node. If the attribute exists, its value is replaced. If it doesn't exist, it is added.

func ShallowCloneWithAttrs

func ShallowCloneWithAttrs(n *html.Node) *html.Node

ShallowCloneWithAttrs creates a shallow copy of a node and copies its attributes. This is useful when you need a new node with the same attributes but will replace its children.

func SignificantChildren

func SignificantChildren(root *html.Node) []*html.Node

SignificantChildren returns a slice of child nodes of root that are not pure-whitespace text nodes. For document nodes it returns children, for element nodes it returns the node itself wrapped as single-item slice.

func SliceToAny

func SliceToAny[T any](s []T) []any

SliceToAny converts a typed slice to []any.

Types

This section is empty.

Jump to

Keyboard shortcuts

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