-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsl.go
More file actions
346 lines (303 loc) · 11.7 KB
/
Copy pathdsl.go
File metadata and controls
346 lines (303 loc) · 11.7 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package css
import (
"github.com/tinywasm/fmt"
)
type Stylesheet struct{ items []item }
type item interface{ writeTo(b *fmt.Builder) }
func NewStylesheet(items ...item) *Stylesheet { return &Stylesheet{items} }
func (s *Stylesheet) String() string {
b := fmt.GetConv()
defer b.PutConv()
for _, it := range s.items {
it.writeTo(b)
}
return b.String()
}
// Selector is a raw CSS selector string used by the DSL.
type Selector string
func (s Selector) cssValue() string { return string(s) }
// Pseudo-class helpers on Class
func (c Class) Hover() Selector { return Selector("." + string(c) + ":hover") }
func (c Class) Focus() Selector { return Selector("." + string(c) + ":focus") }
func (c Class) Disabled() Selector { return Selector("." + string(c) + ":disabled") }
type RuleItem struct {
sel string
decls []Decl
}
func (r RuleItem) writeTo(b *fmt.Builder) {
b.WriteString(r.sel)
b.WriteString(" {\n")
for _, d := range r.decls {
if d.Prop == "raw" {
b.WriteString(d.Val)
b.WriteString("\n")
continue
}
b.WriteString(" ")
b.WriteString(d.Prop)
b.WriteString(": ")
b.WriteString(d.Val)
b.WriteString(";\n")
}
b.WriteString("}\n\n")
}
type RuleContent interface{ ruleContent() }
func (d Decl) ruleContent() {}
type rawRule string
func (r rawRule) ruleContent() {}
// RawRule is a transitional escape hatch. New code SHOULD use the typed DSL.
// Each RawRule call site SHOULD carry a // TODO(css-dsl): add typed X comment
// naming the missing property, so reviewers can decide whether to extend the
// DSL or accept the raw use case (vendor-prefixed, exotic property, etc.).
func RawRule(s string) rawRule { return rawRule(s) }
func Rule(sel any, content ...RuleContent) item {
var s string
switch v := sel.(type) {
case Class:
s = "." + string(v)
case Selector:
s = string(v)
case string:
s = v
}
decls := make([]Decl, 0, len(content))
for _, c := range content {
switch v := c.(type) {
case Decl:
decls = append(decls, v)
case rawRule:
decls = append(decls, Decl{Prop: "raw", Val: string(v)})
}
}
return RuleItem{sel: s, decls: decls}
}
func Root(decls ...Decl) item {
return RuleItem{sel: ":root", decls: decls}
}
type MediaItem struct {
query string
items []item
}
func (m MediaItem) writeTo(b *fmt.Builder) {
b.WriteString("@media ")
b.WriteString(m.query)
b.WriteString(" {\n")
for _, it := range m.items {
switch s := it.(type) {
case RuleItem:
b.WriteString(" ")
b.WriteString(s.sel)
b.WriteString(" {\n")
for _, d := range s.decls {
if d.Prop == "raw" {
b.WriteString(" ")
b.WriteString(d.Val)
b.WriteString("\n")
continue
}
b.WriteString(" ")
b.WriteString(d.Prop)
b.WriteString(": ")
b.WriteString(d.Val)
b.WriteString(";\n")
}
b.WriteString(" }\n\n")
case *RuleItem:
b.WriteString(" ")
b.WriteString(s.sel)
b.WriteString(" {\n")
for _, d := range s.decls {
if d.Prop == "raw" {
b.WriteString(" ")
b.WriteString(d.Val)
b.WriteString("\n")
continue
}
b.WriteString(" ")
b.WriteString(d.Prop)
b.WriteString(": ")
b.WriteString(d.Val)
b.WriteString(";\n")
}
b.WriteString(" }\n\n")
default:
it.writeTo(b)
}
}
b.WriteString("}\n\n")
}
func Media(query string, items ...item) item {
return MediaItem{query: query, items: items}
}
func MediaPrefersDark(items ...item) item {
return Media("(prefers-color-scheme: dark)", items...)
}
// MediaDesktop wraps the canonical "landscape + hover" media query used by
// tinywasm layouts to distinguish desktop from mobile.
// Reference: appears 4 times verbatim in platformd Appendix A.
func MediaDesktop(items ...item) item {
return Media("(orientation: landscape) and (hover: hover)", items...)
}
// KeyframeStep is one step of a keyframes animation.
// At is the percentage or named position ("0%", "50%", "100%", "from", "to").
type KeyframeStep struct {
At string
Decls []Decl
}
// At builds a KeyframeStep. Variadic Decls match the DSL's existing rule shape.
func At(at string, decls ...Decl) KeyframeStep {
return KeyframeStep{At: at, Decls: decls}
}
type KeyframesItem struct {
name string
steps []KeyframeStep
}
func (k KeyframesItem) writeTo(b *fmt.Builder) {
b.WriteString("@keyframes ")
b.WriteString(k.name)
b.WriteString(" {\n")
for _, s := range k.steps {
b.WriteString(" ")
b.WriteString(s.At)
b.WriteString(" {\n")
for _, d := range s.Decls {
b.WriteString(" ")
b.WriteString(d.Prop)
b.WriteString(": ")
b.WriteString(d.Val)
b.WriteString(";\n")
}
b.WriteString(" }\n")
}
b.WriteString("}\n\n")
}
// Keyframes builds an @keyframes at-rule.
func Keyframes(name string, steps ...KeyframeStep) item {
return KeyframesItem{name: name, steps: steps}
}
type RawItem string
func (r RawItem) writeTo(b *fmt.Builder) {
b.WriteString(string(r))
b.WriteString("\n\n")
}
func Raw(css string) item { return RawItem(css) }
type Decl struct{ Prop, Val string }
type Value interface{ cssValue() string }
func (t Token) cssValue() string { return t.Var() }
type stringValue string
func (s stringValue) cssValue() string { return string(s) }
func Px(n int) Value { return stringValue(fmt.Sprintf("%dpx", n)) }
func Rem(f float64) Value { return stringValue(fmt.Sprintf("%grem", f)) }
func Em(f float64) Value { return stringValue(fmt.Sprintf("%gem", f)) }
func Pct(n int) Value { return stringValue(fmt.Sprintf("%d%%", n)) }
func Vw(f float64) Value { return stringValue(fmt.Sprintf("%gvw", f)) }
func Vh(f float64) Value { return stringValue(fmt.Sprintf("%gvh", f)) }
func Calc(expr string) Value { return stringValue(fmt.Sprintf("calc(%s)", expr)) }
func Hex(s string) Value { return stringValue(s) }
func Str(s string) Value { return stringValue(s) }
type kw string
func (k kw) cssValue() string { return string(k) }
var (
Auto Value = kw("auto")
None Value = kw("none")
Block Value = kw("block")
Flex_ Value = kw("flex")
Grid Value = kw("grid")
Inline Value = kw("inline-block")
Center Value = kw("center")
Zero Value = kw("0")
Pointer Value = kw("pointer")
Fixed Value = kw("fixed")
Absolute Value = kw("absolute")
Unset Value = kw("unset")
Initial Value = kw("initial")
FlexEnd Value = kw("flex-end")
SpaceAround Value = kw("space-around")
Row Value = kw("row")
Column Value = kw("column")
Hidden Value = kw("hidden")
Visible Value = kw("visible")
Uppercase Value = kw("uppercase")
Capitalize Value = kw("capitalize")
RightText Value = kw("right")
)
func joinValues(vs []Value) string {
b := fmt.GetConv()
defer b.PutConv()
for i, v := range vs {
if i > 0 {
b.WriteString(" ")
}
b.WriteString(v.cssValue())
}
return b.String()
}
func Background(v Value) Decl { return Decl{"background", v.cssValue()} }
func BackgroundColor(v Value) Decl { return Decl{"background-color", v.cssValue()} }
func BackgroundImage(v Value) Decl { return Decl{"background-image", v.cssValue()} }
func Color(v Value) Decl { return Decl{"color", v.cssValue()} }
func Padding(v ...Value) Decl { return Decl{"padding", joinValues(v)} }
func Margin(v ...Value) Decl { return Decl{"margin", joinValues(v)} }
func Border(v ...Value) Decl { return Decl{"border", joinValues(v)} }
func BorderColor(v Value) Decl { return Decl{"border-color", v.cssValue()} }
func BorderRadius(v ...Value) Decl { return Decl{"border-radius", joinValues(v)} }
func BoxShadow(v Value) Decl { return Decl{"box-shadow", v.cssValue()} }
func BoxSizing(v Value) Decl { return Decl{"box-sizing", v.cssValue()} }
func Display(v Value) Decl { return Decl{"display", v.cssValue()} }
func Flex(v ...Value) Decl { return Decl{"flex", joinValues(v)} }
func FlexDirection(v Value) Decl { return Decl{"flex-direction", v.cssValue()} }
func Gap(v Value) Decl { return Decl{"gap", v.cssValue()} }
func JustifyContent(v Value) Decl { return Decl{"justify-content", v.cssValue()} }
func AlignItems(v Value) Decl { return Decl{"align-items", v.cssValue()} }
func Width(v Value) Decl { return Decl{"width", v.cssValue()} }
func Height(v Value) Decl { return Decl{"height", v.cssValue()} }
func MaxWidth(v Value) Decl { return Decl{"max-width", v.cssValue()} }
func MinHeight(v Value) Decl { return Decl{"min-height", v.cssValue()} }
func FontSize(v Value) Decl { return Decl{"font-size", v.cssValue()} }
func FontWeight(v Value) Decl { return Decl{"font-weight", v.cssValue()} }
func LineHeight(v Value) Decl { return Decl{"line-height", v.cssValue()} }
func LetterSpacing(v Value) Decl { return Decl{"letter-spacing", v.cssValue()} }
func Transition(v ...Value) Decl { return Decl{"transition", joinValues(v)} }
func Animation(v ...Value) Decl { return Decl{"animation", joinValues(v)} }
func Transform(v Value) Decl { return Decl{"transform", v.cssValue()} }
func Cursor(v Value) Decl { return Decl{"cursor", v.cssValue()} }
func Outline(v Value) Decl { return Decl{"outline", v.cssValue()} }
func OutlineOffset(v Value) Decl { return Decl{"outline-offset", v.cssValue()} }
func Opacity(v float64) Decl { return Decl{"opacity", fmt.Sprintf("%g", v)} }
func PointerEvents(v Value) Decl { return Decl{"pointer-events", v.cssValue()} }
func Position(v Value) Decl { return Decl{"position", v.cssValue()} }
func Top(v Value) Decl { return Decl{"top", v.cssValue()} }
func Right(v Value) Decl { return Decl{"right", v.cssValue()} }
func Bottom(v Value) Decl { return Decl{"bottom", v.cssValue()} }
func Left(v Value) Decl { return Decl{"left", v.cssValue()} }
func ZIndex(v Value) Decl { return Decl{"z-index", v.cssValue()} }
func FontFamily(v Value) Decl { return Decl{"font-family", v.cssValue()} }
func MinWidth(v Value) Decl { return Decl{"min-width", v.cssValue()} }
func MaxHeight(v Value) Decl { return Decl{"max-height", v.cssValue()} }
func AlignSelf(v Value) Decl { return Decl{"align-self", v.cssValue()} }
func Overflow(v Value) Decl { return Decl{"overflow", v.cssValue()} }
func Visibility(v Value) Decl { return Decl{"visibility", v.cssValue()} }
func TextAlign(v Value) Decl { return Decl{"text-align", v.cssValue()} }
func TextTransform(v Value) Decl { return Decl{"text-transform", v.cssValue()} }
func TextDecoration(v Value) Decl { return Decl{"text-decoration", v.cssValue()} }
func TextShadow(v ...Value) Decl { return Decl{"text-shadow", joinValues(v)} }
func UserSelect(v Value) Decl { return Decl{"user-select", v.cssValue()} }
func TouchAction(v Value) Decl { return Decl{"touch-action", v.cssValue()} }
func ListStyleType(v Value) Decl { return Decl{"list-style-type", v.cssValue()} }
func GridArea(v Value) Decl { return Decl{"grid-area", v.cssValue()} }
func GridTemplate(v Value) Decl { return Decl{"grid-template", v.cssValue()} }
func MarginLeft(v Value) Decl { return Decl{"margin-left", v.cssValue()} }
func MarginRight(v Value) Decl { return Decl{"margin-right", v.cssValue()} }
func PaddingBottom(v Value) Decl { return Decl{"padding-bottom", v.cssValue()} }
func ListStyle(v Value) Decl { return Decl{"list-style", v.cssValue()} }
func All(v Value) Decl { return Decl{"all", v.cssValue()} }
func OverflowY(v Value) Decl { return Decl{"overflow-y", v.cssValue()} }
func GridTemplateRows(v Value) Decl { return Decl{"grid-template-rows", v.cssValue()} }
func GridTemplateColumns(v Value) Decl { return Decl{"grid-template-columns", v.cssValue()} }
func BorderRight(v ...Value) Decl { return Decl{"border-right", joinValues(v)} }
func Declare(t Token, value string) Decl {
return Decl{t.Name, value}
}
func Bind(active, source Token) Decl {
return Decl{active.Name, source.Var()}
}