-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhtmlg_test.go
76 lines (63 loc) · 1.61 KB
/
htmlg_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
package htmlg_test
import (
"fmt"
"os"
"github.com/shurcooL/component"
"github.com/shurcooL/htmlg"
"golang.org/x/net/html"
"golang.org/x/net/html/atom"
)
func Example() {
// Context-aware escaping is done just like in html/template.
html := htmlg.Render(
htmlg.Text("Hi & how are you, "),
htmlg.A("Gophers", "https://golang.org/"),
htmlg.Text("? <script> is a cool gopher."),
)
fmt.Fprintln(os.Stdout, html)
// Output:
// Hi & how are you, <a href="https://golang.org/">Gophers</a>? <script> is a cool gopher.
}
func ExampleAppendChildren() {
div := htmlg.Div(
htmlg.Text("Go "),
)
htmlg.AppendChildren(div, component.Link{
Text: "there",
URL: "https://golang.org",
NewTab: true,
}.Render()...)
div.AppendChild(
htmlg.Text("for more!"),
)
err := html.Render(os.Stdout, div)
if err != nil {
panic(err)
}
// Output:
// <div>Go <a href="https://golang.org" target="_blank">there</a>for more!</div>
}
func ExampleNodes() {
err := htmlg.RenderComponents(os.Stdout, htmlg.Nodes{
htmlg.Text("Hi & how are you, "),
htmlg.A("Gophers", "https://golang.org/"),
htmlg.Text("? <script> is a cool gopher."),
})
if err != nil {
panic(err)
}
// Output:
// Hi & how are you, <a href="https://golang.org/">Gophers</a>? <script> is a cool gopher.
}
func ExampleNodeComponent() {
heading := htmlg.NodeComponent{
Type: html.ElementNode, Data: atom.H2.String(),
FirstChild: htmlg.Text("This heading is an htmlg.Component"),
}
err := htmlg.RenderComponents(os.Stdout, heading)
if err != nil {
panic(err)
}
// Output:
// <h2>This heading is an htmlg.Component</h2>
}