xmldom

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package xmldom is the XML document model the CSS-selector and XPath operations run against.

It covers the subset of @xmldom/xmldom those operations rely on: Parse builds a tree that preserves element case and attribute order, Serialize writes it back, NewNav adapts it to the XPath evaluator, and CSSToXPath translates a CSS selector into the XPath that selects the same nodes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CSSToXPath

func CSSToXPath(selector string) (string, error)

CSSToXPath translates a CSS selector (the nwmatcher subset CyberChef exercises) into an equivalent XPath 1.0 expression evaluable by antchfx/xpath over an xmlNode tree. Element type and attribute names are matched case-insensitively (the navigator lowercases LocalName, and names are lowered here to match); class, id and attribute values are case-sensitive except for the HTML enumerated attributes in ciAttrValues, mirroring nwmatcher's HTML_TABLE.

func DocIndex

func DocIndex(doc *Node) map[*Node]int

DocIndex assigns each node a pre-order document position.

func EscapeAttr

func EscapeAttr(s string) string

EscapeAttr mirrors xmldom's addSerializedAttribute: replace [<>&"\t\n\r].

func Serialize

func Serialize(n *Node) string

Serialize reproduces @xmldom/xmldom node.toString() with isHTML=false.

Types

type Attr

type Attr struct {
	Name  string
	Value string
}

Attr is a single element attribute, preserving source order and case.

type Nav struct {
	Cur *Node

	AttrIndex int // -1 when positioned on the node itself, else index into cur.attrs
	// contains filtered or unexported fields
}

Nav is an xpath.NodeNavigator cursor over an xmlNode tree, letting the antchfx/xpath engine evaluate XPath (and CSS-translated-to-XPath) expressions against our xmldom-faithful DOM. LocalName is lowercased so element type and attribute name matching are case-insensitive, matching nwmatcher's HTML mode; serialization is unaffected as it reads xmlNode fields directly.

func NewNav

func NewNav(root *Node, cdataAsText bool) *Nav

NewNav returns a cursor positioned at root. cdataAsText selects whether CDATA counts as text, which the two callers disagree about.

func (n *Nav) Copy() xpath.NodeNavigator

Copy returns an independent cursor at the same position.

func (n *Nav) LocalName() string

LocalName returns the current element or attribute name without its prefix, lowercased so name tests are case-insensitive.

func (n *Nav) MoveTo(other xpath.NodeNavigator) bool

MoveTo jumps to another cursor's position, which must be over the same document.

func (n *Nav) MoveToChild() bool

MoveToChild steps to the first child, reporting whether there was one. An attribute has no children.

func (n *Nav) MoveToFirst() bool

MoveToFirst steps to the first sibling, reporting whether that moved the cursor.

func (n *Nav) MoveToNext() bool

MoveToNext steps to the following sibling, reporting whether there was one.

func (n *Nav) MoveToNextAttribute() bool

MoveToNextAttribute steps to the next attribute of the current element, reporting whether there was one.

func (n *Nav) MoveToParent() bool

MoveToParent steps to the owning element of an attribute, or to the parent node, reporting whether there was one.

func (n *Nav) MoveToPrevious() bool

MoveToPrevious steps to the preceding sibling, reporting whether there was one.

func (n *Nav) MoveToRoot()

MoveToRoot returns the cursor to the document node.

func (n *Nav) NodeType() xpath.NodeType

NodeType reports the current node's kind in the evaluator's vocabulary.

func (n *Nav) Prefix() string

Prefix returns the namespace prefix of the current element or attribute, or "" when it has none.

func (n *Nav) Value() string

Value returns the string value of the current position: an attribute's value, a text or comment node's content, or an element's text descendants concatenated.

type Node

type Node struct {
	Attrs []Attr
	// contains filtered or unexported fields
}

Node is a minimal DOM node reproducing the subset of @xmldom/xmldom that CSS selector / XPath expression rely on. Element case and attribute order are preserved exactly, matching xmldom's XML-mode parsing.

func Parse

func Parse(src string) *Node

Parse reproduces @xmldom/xmldom's DOMParser.parseFromString(source) called with no mimeType: XML mode (isHTML=false), decoding only the five XML named entities plus numeric character references. It is deliberately lenient about unclosed tags and stray markup, mirroring how xmldom recovers, and enforces a single documentElement (content after the root element closes is discarded).

Jump to

Keyboard shortcuts

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