-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
57 lines (44 loc) · 959 Bytes
/
main.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
package main
import (
"fmt"
"log"
"strings"
"github.com/JohannesKaufmann/dom"
"golang.org/x/net/html"
)
func main() {
input := `
<p>
<i>Javascript</i>
<i class="lang__old">PHP</i>
<i>Golang</i>
<p>`
doc, err := html.Parse(strings.NewReader(input))
if err != nil {
log.Fatal(err)
}
// - - - - - //
italicNodes := dom.FindAllNodes(doc, func(node *html.Node) bool {
return dom.NodeName(node) == "i"
})
for _, node := range italicNodes {
if dom.HasClass(node, "lang__old") {
newNode := &html.Node{
Type: html.TextNode,
Data: "🪦",
}
dom.ReplaceNode(node, newNode)
}
}
// - - - - - //
emptyTextNodes := dom.FindAllNodes(doc, func(node *html.Node) bool {
name := dom.NodeName(node)
text := dom.CollectText(node)
return name == "#text" && strings.TrimSpace(text) == ""
})
for _, node := range emptyTextNodes {
dom.RemoveNode(node)
}
// - - - - - //
fmt.Println(dom.RenderRepresentation(doc))
}