summaryrefslogtreecommitdiff
path: root/html/node.go
blob: 831a7a191b6f06ff1c7828f4310b6eb939cf3a57 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package html

import (
	"fmt"

	x "golang.org/x/net/html"
	base "src.userspace.com.au/felix/query"
)

// A Node consists of a NodeType and some data (tag name for
// element nodes, content for text) and are part of a tree of Nodes.
type Node struct {
	parent, prevSibling, nextSibling, firstChild, lastChild *Node

	level int

	*x.Node
}

func (n *Node) Parent() base.Node {
	if n.parent == nil {
		return nil
	}
	return &Node{Node: n.Node.Parent}
}
func (n *Node) NextSibling() base.Node {
	if n.nextSibling == nil {
		return nil
	}
	return &Node{Node: n.Node.NextSibling}
}
func (n *Node) FirstChild() base.Node {
	if n.firstChild == nil {
		return nil
	}
	return &Node{Node: n.Node.FirstChild}
}
func (n *Node) PrevSibling() base.Node { return &Node{Node: n.Node.PrevSibling} }
func (n *Node) LastChild() base.Node   { return &Node{Node: n.Node.LastChild} }
func (n *Node) Type() base.NodeType    { return base.NodeType(n.Node.Type) }
func (n *Node) DataType() string       { return "string" }
func (n *Node) Attr() []base.Attribute {
	out := make([]base.Attribute, len(n.Node.Attr))
	for _, a := range n.Node.Attr {
		out = append(out, base.Attribute(a))
	}
	return out
}

// Data gets the value of the node and all its child nodes.
func (n *Node) Data() string { return n.Node.Data }

// InnerText gets the value of the node and all its child nodes.
func (n *Node) InnerText() string {
	// FIXME
	return n.Node.Data
}

func (n Node) String() string {
	return fmt.Sprintf("[%s] %s(%s)", base.NodeNames[n.Type()], n.DataType(), n.Data())
}

func (n Node) PrintTree(level int) {
	for i := 1; i <= level; i++ {
		fmt.Printf("  ")
	}
	fmt.Println(n)
	for c := n.firstChild; c != nil; c = c.nextSibling {
		c.PrintTree(level + 1)
	}
}

func (n *Node) appendChild(c *Node) {
	if c.parent != nil || c.prevSibling != nil || c.nextSibling != nil {
		panic("html: appendChild called for an attached child Node")
	}
	last := n.lastChild
	if last != nil {
		last.nextSibling = c
	} else {
		n.firstChild = c
	}
	n.lastChild = c
	c.parent = n
	c.prevSibling = last
}