-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgax_example_test.go
164 lines (137 loc) · 2.9 KB
/
gax_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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package gax_test
import (
"fmt"
x "github.com/mitranim/gax"
)
func ExampleBui() {
var (
E = x.E
AP = x.AP
)
type Dat struct {
Title string
Posts []string
}
dat := Dat{
Title: `Posts`,
Posts: []string{`Post0`, `Post1`},
}
bui := x.F(
x.Str(x.Doctype),
E(`html`, AP(`lang`, `en`),
E(`head`, nil,
E(`meta`, AP(`charset`, `utf-8`)),
E(`link`, AP(`rel`, `icon`, `href`, `data:;base64,=`)),
// Use normal Go conditionals.
func(b *x.Bui) {
if dat.Title != "" {
b.E(`title`, nil, dat.Title)
} else {
b.E(`title`, nil, `test markup`)
}
},
),
E(`body`, nil,
E(`h1`, AP(`class`, `title`), `Posts`),
// Use normal Go loops.
func(b *x.Bui) {
for _, post := range dat.Posts {
b.E(`h2`, nil, post)
}
},
),
),
)
fmt.Println(bui)
// Output:
// <!doctype html><html lang="en"><head><meta charset="utf-8"><link rel="icon" href="data:;base64,="><title>Posts</title></head><body><h1 class="title">Posts</h1><h2>Post0</h2><h2>Post1</h2></body></html>
}
func ExampleE() {
var (
E = x.E
AP = x.AP
)
fmt.Println(
E(`span`, AP(`aria-hidden`, `true`), `🔥`),
)
// Output:
// <span aria-hidden="true">🔥</span>
}
func ExampleF() {
var doc = x.F(
x.Str(x.Doctype),
x.E(`html`, nil),
)
fmt.Println(doc)
// Output:
// <!doctype html><html></html>
}
func ExampleDoctype() {
bui := x.Bui(x.Doctype)
bui.E(`html`, nil)
fmt.Println(bui)
// Output:
// <!doctype html><html></html>
}
func ExampleAP() {
fmt.Println(
x.AP(
`href`, `/`,
`aria-current`, `page`,
`class`, `some-class`,
),
)
// Output:
// href="/" aria-current="page" class="some-class"
}
func ExampleA() {
attrs := x.A(
x.Attr{`class`, `some-class`},
x.Attr{`style`, `some: style`},
)
fmt.Println(attrs)
// Output:
// class="some-class" style="some: style"
}
func ExampleAttrs() {
attrs := x.Attrs{
{`class`, `some-class`},
{`style`, `some: style`},
}
fmt.Println(attrs)
// Output:
// class="some-class" style="some: style"
}
func ExampleAttrs_A() {
cur := func() x.Attr { return x.Attr{`aria-current`, `page`} }
bg := func() x.Attr { return x.Attr{`style`, `background-image: url(...)`} }
fmt.Println(
x.AP(`class`, `some-class`).A(cur(), bg()),
)
// class="some-class" aria-current="page" style="background-image: url(...)"
}
func ExampleAttrs_AP() {
fmt.Println(
x.AP(`class`, `some-class`).AP(`href`, `/`),
)
// class="some-class" href="/"
}
func ExampleAttr() {
fmt.Println(x.Attr{`class`, `some-class`})
// Output:
// class="some-class"
}
func ExampleStr_Render() {
var bui x.Bui
bui.E(`div`, nil, x.Str(`<script>alert('hacked!')</script>`))
fmt.Println(bui)
// Output:
// <div><script>alert('hacked!')</script></div>
}
func ExampleBui_Render() {
var bui x.Bui
bui.E(`div`, nil, x.Bui(`<script>alert('hacked!')</script>`))
fmt.Println(bui)
// Output:
// <div><script>alert('hacked!')</script></div>
}