dom

package
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package dom holds a raw-preserving SVG document tree. Every node keeps the exact input bytes that produced it; serialization emits those bytes verbatim unless the node was modified. Untouched markup therefore round-trips byte-for-byte, which is what makes "when unsure, leave it unchanged" safe and the output deterministic.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Serialize

func Serialize(doc *Node) []byte

Serialize emits the document. Nodes that were never modified are written from their verbatim input bytes; modified elements get a canonical start tag in which untouched attributes still keep their original spelling.

Types

type Attr

type Attr struct {
	// Name is the attribute name as written, including any namespace prefix.
	Name string
	// contains filtered or unexported fields
}

Attr is one attribute of an element. The raw form (leading whitespace, original quoting, undecoded entities) is kept so that untouched attributes serialize verbatim even when a sibling attribute changed.

func (*Attr) Value

func (a *Attr) Value() (string, bool)

Value returns the decoded attribute value. ok is false when the value could not be cleanly decoded (unknown entity); such values must be treated as opaque and never rewritten.

type Kind

type Kind uint8

Kind discriminates node types in the tree.

const (
	KindDocument Kind = iota
	KindElement
	KindText
	KindComment
	KindCDATA
	KindDoctype
	KindProcInst
)

type Node

type Node struct {
	Kind     Kind
	Name     string // element or PI name as written, including prefix
	Attrs    []Attr
	Children []*Node
	Parent   *Node

	// SelfClosing records whether the element used <name/> form.
	SelfClosing bool
	// contains filtered or unexported fields
}

Node is a document, element, or leaf (text, comment, CDATA, doctype, PI).

func Parse

func Parse(svg []byte) (*Node, error)

Parse builds a document tree from svg. It fails on malformed markup (mismatched or unclosed tags, no root element) so the caller can fall back to the untouched input.

func (*Node) AttrValue

func (n *Node) AttrValue(name string) (string, bool)

AttrValue returns the decoded value of the named attribute. ok is false when the attribute is absent or its value is opaque.

func (*Node) CanonicalizeStartTag added in v0.2.0

func (n *Node) CanonicalizeStartTag()

CanonicalizeStartTag re-serializes the start tag with single-space attribute separation when the original spelling wastes bytes (editor indentation inside tags). Inter-attribute whitespace is not significant in XML, so the document is equivalent.

func (*Node) HasAttr

func (n *Node) HasAttr(name string) bool

HasAttr reports whether the named attribute is present.

func (*Node) Raw

func (n *Node) Raw() []byte

Raw returns the verbatim input bytes of a leaf node (text, comment, CDATA, doctype, processing instruction). It is nil for elements and documents.

func (*Node) RemoveAttr

func (n *Node) RemoveAttr(name string)

RemoveAttr removes the named attribute if present.

func (*Node) RemoveChild

func (n *Node) RemoveChild(c *Node)

RemoveChild removes c from n's children. Surrounding markup is unaffected.

func (*Node) Rename added in v0.4.0

func (n *Node) Rename(name string)

Rename changes an element's tag name; both tags re-serialize.

func (*Node) ReplaceWithChildren

func (n *Node) ReplaceWithChildren()

ReplaceWithChildren splices n's children into its parent in n's place, dropping n's own tags. Used to unwrap groups.

func (*Node) SetAttr

func (n *Node) SetAttr(name, value string)

SetAttr sets the named attribute, appending it when absent, and marks the element for canonical re-serialization of its start tag.

func (*Node) SetText added in v0.3.0

func (n *Node) SetText(s string)

SetText replaces a text node's serialized content. The caller is responsible for keeping any markup-significant characters escaped.

func (*Node) Walk

func (n *Node) Walk(fn func(*Node) bool)

Walk visits n and its descendants in document order. Returning false from fn skips the node's children.

Jump to

Keyboard shortcuts

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