-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtransformer.go
82 lines (67 loc) · 1.51 KB
/
transformer.go
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
package gsoup
import (
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
// XNode is a wrapper around an *html.Node to enforce safe transformations
type XNode interface {
FirstChild() XNode
LastChild() XNode
Type() html.NodeType
Atom() atom.Atom
Data() string
Attr() []html.Attribute
SetType(html.NodeType)
SetAtom(atom.Atom)
SetData(string)
SetAttrs([]html.Attribute)
}
// TransformFunc describes the signature of a transform function
type TransformFunc func(XNode) XNode
func newXNode(n *html.Node) XNode {
return &tnode{node: n}
}
type tnode struct {
node *html.Node
}
func (t *tnode) FirstChild() XNode {
if t.node.FirstChild != nil {
return newXNode(t.node.FirstChild)
}
return nil
}
func (t *tnode) LastChild() XNode {
if t.node.LastChild != nil {
return newXNode(t.node.LastChild)
}
return nil
}
func (t *tnode) Type() html.NodeType {
return t.node.Type
}
func (t *tnode) Atom() atom.Atom {
return t.node.DataAtom
}
func (t *tnode) Data() string {
return t.node.Data
}
func (t *tnode) Attr() []html.Attribute {
result := make([]html.Attribute, len(t.node.Attr), len(t.node.Attr))
copy(result, t.node.Attr)
return result
}
func (t *tnode) SetType(newType html.NodeType) {
t.node.Type = newType
}
func (t *tnode) SetAtom(newAtom atom.Atom) {
t.node.DataAtom = newAtom
if t.node.Type == html.ElementNode {
t.node.Data = newAtom.String()
}
}
func (t *tnode) SetData(newData string) {
t.node.Data = newData
}
func (t *tnode) SetAttrs(newAttrs []html.Attribute) {
t.node.Attr = newAttrs
}