helpers

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Dec 22, 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(want, got []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 EqualHTML added in v0.4.0

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

EqualHTML parses two HTML strings and returns true if their DOMs match. It ignores pure-whitespace text nodes and compares attributes order-insensitively. If DOMs don't match, it logs template and data context for debugging.

func FilterAttrs added in v0.2.2

func FilterAttrs(attrs []html.Attribute, excludeKey string) []html.Attribute

FilterAttrs returns a new attribute slice excluding the specified key. This is useful for creating a copy of attributes without a specific key.

func FormatAttr added in v0.3.0

func FormatAttr(val string) string

FormatAttr formats an attribute value by trimming whitespace, replacing newlines with spaces, and reducing repeated spaces to single spaces.

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 reports whether s is a valid ASCII identifier. An identifier must be non-empty, start with a letter (A–Z, a–z) or underscore, and may contain only letters, digits, or underscores thereafter.

func IsIdentifierChar

func IsIdentifierChar(ch rune, first bool) bool

IsIdentifierChar reports whether ch is valid in an identifier. If first is true, digits are not allowed.

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.

Types

This section is empty.

Jump to

Keyboard shortcuts

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