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
|
package html
import (
//"fmt"
"io"
x "golang.org/x/net/html"
)
func Parse(r io.Reader) (*Node, error) {
xnode, err := x.Parse(r)
if err != nil {
return nil, err
}
/*
if len(xnodes) > 1 {
return nil, fmt.Errorf("found multiple HTML roots: %d", len(xnodes))
}
*/
root := wrapNodes(xnode, 0)
return root, nil
}
func wrapNodes(root *x.Node, l int) *Node {
out := &Node{Node: root, level: l}
for c := root.FirstChild; c != nil; c = c.NextSibling {
child := wrapNodes(c, l+1)
out.appendChild(child)
}
/*
if root.Parent != nil {
out.parent = &Node{Node: root.Parent}
if l > 0 {
out.parent.level = l - 1
}
}
if root.FirstChild != nil {
out.firstChild = wrapNodes(root.FirstChild, l+1)
}
if root.NextSibling != nil {
out.nextSibling = wrapNodes(root.NextSibling, l)
}
if root.LastChild != nil {
out.lastChild = wrapNodes(root.LastChild, l+1)
}
if root.PrevSibling != nil {
//out.prevSibling = wrapNodes(root.prevSibling, l)
out.prevSibling = &Node{
Node: root.PrevSibling,
level: l,
parent: out.parent,
}
}
*/
return out
}
|