-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
49 lines (44 loc) · 1.2 KB
/
node.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
package gohtml
import (
"golang.org/x/net/html"
"log"
)
func AppendChildNodes(parent *html.Node, children []*html.Node) {
for _, c := range children {
parent.AppendChild(c)
}
}
func DetachNodes(nodes []*html.Node) {
for _, n := range nodes {
n.Parent.RemoveChild(n)
}
}
func CompactNode(n *html.Node) {
var appendNodes []*html.Node
for c := n.FirstChild; c != nil; {
CompactNode(c)
if _mergeTextElements[c.Data] {
appendNodes = append(appendNodes, GetChildNodes(c)...)
log.Println("delete", c.Data)
c = RemoveNode(c)
} else if c.Type == html.ElementNode && c.FirstChild == nil && !_voidElements[c.Data] {
log.Println("delete", c.Data)
c = RemoveNode(c)
} else {
c = c.NextSibling
}
}
DetachNodes(appendNodes)
AppendChildNodes(n, appendNodes)
if n.FirstChild != nil && n.FirstChild.NextSibling == nil {
if n.FirstChild.Data == n.Data || (n.FirstChild.Data == "br" && (n.Data == "p" || n.Data == "div")) {
childNodes := GetChildNodes(n.FirstChild)
log.Println("delete", n.FirstChild.Data)
n.RemoveChild(n.FirstChild)
DetachNodes(childNodes)
AppendChildNodes(n, childNodes)
} else if n.FirstChild.Data == "img" && n.Data == "a" {
*n = *n.FirstChild
}
}
}