-
Notifications
You must be signed in to change notification settings - Fork 18
/
example_test.go
105 lines (84 loc) · 2.51 KB
/
example_test.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package svgparser_test
import (
"fmt"
"strings"
"github.com/JoshVarga/svgparser"
"github.com/JoshVarga/svgparser/utils"
)
func ExampleParse() {
svg := `
<svg width="100" height="100">
<circle cx="50" cy="50" r="40" fill="red" />
</svg>
`
reader := strings.NewReader(svg)
element, _ := svgparser.Parse(reader, false)
fmt.Printf("SVG width: %s\n", element.Attributes["width"])
fmt.Printf("Circle fill: %s", element.Children[0].Attributes["fill"])
// Output:
// SVG width: 100
// Circle fill: red
}
func ExampleValidate() {
svg := `
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" id="svg-root" width="100%" height="100%">
<desc id="test-desc">Test validation</desc>
</svg>
`
reader := strings.NewReader(svg)
element, _ := svgparser.Parse(reader, true)
fmt.Printf("Desc content: %+v\n", element.FindAll("desc")[0].Content)
// Output:
// Desc content: Test validation
}
func ExampleElement_FindAll() {
svg := `
<svg width="1000" height="600">
<rect fill="#000" width="5" height="3"/>
<rect fill="#444" width="5" height="2" y="1"/>
<rect fill="#888" width="5" height="1" y="2"/>
</svg>
`
reader := strings.NewReader(svg)
element, _ := svgparser.Parse(reader, false)
rectangles := element.FindAll("rect")
fmt.Printf("First child fill: %s\n", rectangles[0].Attributes["fill"])
fmt.Printf("Second child height: %s", rectangles[1].Attributes["height"])
// Output:
// First child fill: #000
// Second child height: 2
}
func ExampleElement_FindID() {
svg := `
<svg width="1000" height="600">
<rect id="black" fill="#000" width="5" height="3"/>
<rect id="gray" fill="#888" width="5" height="2" y="1"/>
<rect id="white" fill="#fff" width="5" height="1" y="2"/>
</svg>
`
reader := strings.NewReader(svg)
element, _ := svgparser.Parse(reader, false)
white := element.FindID("white")
fmt.Printf("White rect fill: %s", white.Attributes["fill"])
// Output:
// White rect fill: #fff
}
func ExamplePathParser() {
d := "M50,50 A30,30 0 0,1 35,20 L100,100 M110,110 L100,0"
path, _ := utils.PathParser(d)
fmt.Printf("Number of subpaths: %d\n", len(path.Subpaths))
for i, subpath := range path.Subpaths {
fmt.Printf("Path %d: ", i)
for j, command := range subpath.Commands {
if j+1 == len(subpath.Commands) {
fmt.Printf("%v\n", command)
} else {
fmt.Printf("%v -> ", command)
}
}
}
// Output:
// Number of subpaths: 2
// Path 0: &{M [50 50]} -> &{A [30 30 0 0 1 35 20]} -> &{L [100 100]}
// Path 1: &{M [110 110]} -> &{L [100 0]}
}