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 ¶
- func CSSToXPath(selector string) (string, error)
- func DocIndex(doc *Node) map[*Node]int
- func EscapeAttr(s string) string
- func Serialize(n *Node) string
- type Attr
- type Nav
- func (n *Nav) Copy() xpath.NodeNavigator
- func (n *Nav) LocalName() string
- func (n *Nav) MoveTo(other xpath.NodeNavigator) bool
- func (n *Nav) MoveToChild() bool
- func (n *Nav) MoveToFirst() bool
- func (n *Nav) MoveToNext() bool
- func (n *Nav) MoveToNextAttribute() bool
- func (n *Nav) MoveToParent() bool
- func (n *Nav) MoveToPrevious() bool
- func (n *Nav) MoveToRoot()
- func (n *Nav) NodeType() xpath.NodeType
- func (n *Nav) Prefix() string
- func (n *Nav) Value() string
- type Node
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CSSToXPath ¶
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 EscapeAttr ¶
EscapeAttr mirrors xmldom's addSerializedAttribute: replace [<>&"\t\n\r].
Types ¶
type Nav ¶
type Nav struct {
// 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 ¶
NewNav returns a cursor positioned at root. cdataAsText selects whether CDATA counts as text, which the two callers disagree about.
func (*Nav) Copy ¶
func (n *Nav) Copy() xpath.NodeNavigator
Copy returns an independent cursor at the same position.
func (*Nav) LocalName ¶
LocalName returns the current element or attribute name without its prefix, lowercased so name tests are case-insensitive.
func (*Nav) MoveTo ¶
func (n *Nav) MoveTo(other xpath.NodeNavigator) bool
MoveTo jumps to another cursor's position, which must be over the same document.
func (*Nav) MoveToChild ¶
MoveToChild steps to the first child, reporting whether there was one. An attribute has no children.
func (*Nav) MoveToFirst ¶
MoveToFirst steps to the first sibling, reporting whether that moved the cursor.
func (*Nav) MoveToNext ¶
MoveToNext steps to the following sibling, reporting whether there was one.
func (*Nav) MoveToNextAttribute ¶
MoveToNextAttribute steps to the next attribute of the current element, reporting whether there was one.
func (*Nav) MoveToParent ¶
MoveToParent steps to the owning element of an attribute, or to the parent node, reporting whether there was one.
func (*Nav) MoveToPrevious ¶
MoveToPrevious steps to the preceding sibling, reporting whether there was one.
func (*Nav) MoveToRoot ¶
func (n *Nav) MoveToRoot()
MoveToRoot returns the cursor to the document node.
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 ¶
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).