/
CssDSL.kt
517 lines (429 loc) · 16.2 KB
/
CssDSL.kt
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
package kotlinx.html
import java.util.*
interface Selector {
fun toExternalForm(): String
}
interface SelectorTrait {
fun toExternalForm(): String
}
object EmptyTrait : SelectorTrait {
override fun toExternalForm(): String {
return ""
}
}
interface StyleClass : SelectorTrait, Selector {
val name: String
override fun toExternalForm(): String {
return ".$name"
}
}
class SimpleClassStyle(override val name : String) : StyleClass {
}
class CompositeStyleClass(val a: StyleClass, val b: StyleClass) : StyleClass {
override val name: String get() = "${a.name} ${b.name}"
override fun toExternalForm(): String {
return "${a.toExternalForm()} ${b.toExternalForm()}"
}
}
operator fun StyleClass?.plus(another: StyleClass?): StyleClass? = when {
this == null && another == null -> null
this == null -> another
another == null -> this
else -> CompositeStyleClass(this, another)
}
enum class PseudoClass : StyleClass {
root, firstChild, lastChild, firstOfType, lastOfType, onlyChild, onlyOfType,
empty, link, visited, active, focus, hover, target, enabled, disabled, checked;
override fun toExternalForm(): String {
return ":$name"
}
}
/**
* Represents a single stylesheet element.
*/
open class CssElement() {
val children = arrayListOf<StyledElement>()
val attributes = HashMap<String, Any>()
inner class IdSelector(val name: String) : SelectorTrait, Selector {
operator fun invoke(body: StyledElement.() -> Unit) {
any.invoke(this, body = body)
}
override fun toExternalForm(): String {
return "#$name"
}
}
inner open class TagSelector(val name: String) : Selector {
fun id(name: String): Selector = invoke(IdSelector(name))
fun id(name: String, body: StyledElement.() -> Unit) = id(IdSelector(name), body)
fun id(id: IdSelector, body: StyledElement.() -> Unit) = invoke(id, body = body)
fun c(vararg klass: StyleClass, body: StyledElement.() -> Unit) = invoke(*klass, body = body)
fun c(vararg klass: StyleClass): Selector = invoke(*klass)
operator fun invoke(vararg traits: SelectorTrait, body: StyledElement.() -> Unit) {
s(SimpleSelector(this, traits).toExternalForm(), body)
}
operator fun invoke(classes: String, body: StyledElement.() -> Unit) {
s(SimpleSelector(this, arrayOf(SimpleClassStyle(classes))).toExternalForm(), body)
}
operator fun invoke(vararg t: SelectorTrait): Selector {
return SimpleSelector(this, t)
}
fun isAny(): Boolean = "*" == name
override fun toExternalForm(): String {
return name
}
}
val any: TagSelector get() = TagSelector("*")
val a: TagSelector get() = TagSelector("a")
val b: TagSelector get() = TagSelector("b")
val body: TagSelector get() = TagSelector("body")
val button: TagSelector get() = TagSelector("button")
val canvas: TagSelector get() = TagSelector("canvas")
val div: TagSelector get() = TagSelector("div")
val em: TagSelector get() = TagSelector("em")
val fieldset: TagSelector get() = TagSelector("fieldset")
val form: TagSelector get() = TagSelector("form")
val h1: TagSelector get() = TagSelector("h1")
val h2: TagSelector get() = TagSelector("h2")
val h3: TagSelector get() = TagSelector("h3")
val h4: TagSelector get() = TagSelector("h4")
val h5: TagSelector get() = TagSelector("h5")
val hr: TagSelector get() = TagSelector("hr")
val i: TagSelector get() = TagSelector("i")
val img: TagSelector get() = TagSelector("img")
val input: TagSelector get() = TagSelector("input")
val legend: TagSelector get() = TagSelector("legend")
val label: TagSelector get() = TagSelector("label")
val ol: TagSelector get() = TagSelector("ol")
val p: TagSelector get() = TagSelector("p")
val select: TagSelector get() = TagSelector("select")
val span: TagSelector get() = TagSelector("span")
val small: TagSelector get() = TagSelector("small")
val strong: TagSelector get() = TagSelector("strong")
val blockquote: TagSelector get() = TagSelector("blockquote")
val table: TagSelector get() = TagSelector("table")
val textarea: TagSelector get() = TagSelector("textarea")
val ul: TagSelector get() = TagSelector("ul")
val li: TagSelector get() = TagSelector("li")
val option: TagSelector get() = TagSelector("option")
val optgroup: TagSelector get() = TagSelector("optgroup")
val tr: TagSelector get() = TagSelector("tr")
val tbody: TagSelector get() = TagSelector("tbody")
val td: TagSelector get() = TagSelector("td")
val th: TagSelector get() = TagSelector("th")
val dl: TagSelector get() = TagSelector("dl")
val dt: TagSelector get() = TagSelector("dt")
val dd: TagSelector get() = TagSelector("dd")
fun id(name: String, body: StyledElement.() -> Unit) {
any.id(name, body)
}
fun id(name: String): IdSelector = IdSelector(name)
fun c(klass: StyleClass, body: StyledElement.() -> Unit) {
any.invoke(klass, body = body)
}
fun att(name: String): Attribute = Attribute(name, kotlinx.html.HasAttribute(name))
class Attribute internal constructor(val name: String, val filter: kotlinx.html.AttFilter) : SelectorTrait {
infix fun startsWith(value: String): Attribute {
return Attribute(name, kotlinx.html.StartsWith(value))
}
infix fun equalTo(value: String): Attribute {
return Attribute(name, kotlinx.html.Equals(value))
}
infix fun endsWith(value: String): Attribute {
return Attribute(name, kotlinx.html.EndsWith(value))
}
infix fun contains(value: String): Attribute {
return Attribute(name, kotlinx.html.Contains(value, kotlinx.html.AttributeValueTokenizer.Substring))
}
infix fun containsInHypen(value: String): Attribute {
return Attribute(name, kotlinx.html.Contains(value, kotlinx.html.AttributeValueTokenizer.Hypen))
}
infix fun containsInSpaces(value: String): Attribute {
return Attribute(name, kotlinx.html.Contains(value, kotlinx.html.AttributeValueTokenizer.Spaces))
}
override fun toExternalForm(): String {
return "[$name${filter.toExternalForm()}]"
}
}
class SimpleSelector(val tag: TagSelector, val traits: Array<out SelectorTrait>) : Selector {
override fun toExternalForm(): String = buildString {
val isAny = tag.isAny()
if (!isAny) {
append(tag.name)
}
for (t in traits) {
append(t.toExternalForm())
}
if (length == 0 && isAny) {
return "*"
}
}
}
fun forAny(vararg selectors: Selector, body: StyledElement.() -> Unit) {
s(UnionSelector(selectors).toExternalForm(), body)
}
fun forAny(vararg selectors: Selector): UnionSelector {
return UnionSelector(selectors)
}
class UnionSelector(val selectors: Array<out Selector>) : Selector {
override fun toExternalForm(): String {
return "(${selectors.map ({ it.toExternalForm() }).joinToString(",")})"
}
}
/**
* Creates a new child element with the given selector and block.
*/
fun s(selector: String, init: StyledElement.() -> Unit) {
val element = StyledElement(selector)
element.init()
children.add(element)
}
}
class StyledElement(val selector: String) : CssElement() {
/**
* Writes the element to the builder with the given indenation.
*/
fun build(builder: StringBuilder, baseSelector: String) {
val thisSelector = if (baseSelector.length > 0) if (selector.startsWith(':')) "$baseSelector$selector" else "$baseSelector $selector" else selector
builder.append("$thisSelector {\n")
for (a in attributes.keys) {
val attr = attributes[a]!!
builder.append(" $a: $attr;\n")
}
builder.append("}\n")
for (child in children) {
child.build(builder, thisSelector)
}
}
/** Strongly-typed method for pulling attributes out of the hash. */
@Suppress("UNCHECKED_CAST")
fun <T:Any?> getAttribute(name: String): T {
if (attributes.containsKey(name))
return attributes[name] as T
else
throw Exception("Element has no attribute $name")
}
/** Strongly-typed method for pulling attributes out of the hash, with a default return value. */
@Suppress("UNCHECKED_CAST")
fun <T> getAttribute(name: String, default: T): T {
if (attributes.containsKey(name))
return attributes[name] as T
else
return default
}
/** Shorthand for making a color inside a stylesheet. */
fun c(colorString: String): Color = color(colorString)
var backgroundAttachment: BackgroundAttachment?
get() = getAttribute("background-attachment")
set(value) {
attributes["background-attachment"] = value.toString()
}
var backgroundColor: Color?
get() = getAttribute("background-color")
set(value) {
attributes["background-color"] = value.toString()
}
var backgroundImage: String?
get() = getAttribute("background-image")
set(value) {
attributes["background-image"] = value.toString()
}
var backgroundPosition: String?
get() = getAttribute("background-position")
set(value) {
attributes["background-position"] = value.toString()
}
var backgroundRepeat: BackgroundRepeat?
get() = getAttribute("background-repeat")
set(value) {
attributes["background-repeat"] = value.toString()
}
var border: String = ""
set(value) {
val tokens = value.split(' ')
for (token in tokens) {
if (isLinearDimension(token))
borderWidth = LinearDimension.fromString(token)
else if (isColor(token))
borderColor = color(token)
else if (isBorderStyle(token))
borderStyle = makeBorderStyle(token)
else
throw Exception("Invalid border property: $token")
}
}
var borderColor: Color?
get() = getAttribute("border-color")
set(value) {
attributes["border-color"] = value.toString()
}
var borderRadius: LinearDimension
get() = getAttribute("border-radius", 0.px)
set(value) {
attributes["border-radius"] = value.toString()
}
var borderBottomLeftRadius: LinearDimension
get() = getAttribute("border-bottom-left-radius", 0.px)
set(value) {
attributes["border-bottom-left-radius"] = value.toString()
}
var borderBottomRightRadius: LinearDimension
get() = getAttribute("border-bottom-right-radius", 0.px)
set(value) {
attributes["border-bottom-right-radius"] = value.toString()
}
var borderTopLeftRadius: LinearDimension
get() = getAttribute("border-top-left-radius", 0.px)
set(value) {
attributes["border-top-left-radius"] = value.toString()
}
var borderTopRightRadius: LinearDimension
get() = getAttribute("border-top-right-radius", 0.px)
set(value) {
attributes["border-top-right-radius"] = value.toString()
}
var borderStyle: BorderStyle?
get() = getAttribute("border-style")
set(value) {
attributes["border-style"] = value.toString()
}
var borderWidth: LinearDimension
get() = getAttribute("border-width", 0.px)
set(value) {
attributes["border-width"] = value.toString()
}
var clear: Clear?
get() = getAttribute("clear")
set(value) {
attributes["clear"] = value.toString()
}
var color: Color?
get() = getAttribute("color")
set(value) {
attributes["color"] = value.toString()
}
var display: Display?
get() = getAttribute("display")
set(value) {
attributes["display"] = value.toString()
}
var float: FloatType?
get() = getAttribute("float")
set(value) {
attributes["float"] = value.toString()
}
var fontFamily: String?
get() = getAttribute("font-family")
set(value) {
attributes["font-family"] = value.toString()
}
var fontSize: LinearDimension?
get() = getAttribute("font-size")
set(value) {
attributes["font-size"] = value.toString()
}
var fontWeight: FontWeight
get() = getAttribute("font-weight")
set(value) {
attributes["font-weight"] = value.toString()
}
var height: LinearDimension?
get() = getAttribute("height")
set(value) {
attributes["height"] = value.toString()
}
var lineHeight: LinearDimension?
get() = getAttribute("line-height")
set(value) {
attributes["line-height"] = value.toString()
}
var margin: BoxDimensions?
get() = getAttribute("margin")
set(value) {
attributes["margin"] = value.toString()
}
var marginTop: LinearDimension?
get() = getAttribute("margin-top")
set(value) {
attributes["margin-top"] = value.toString()
}
var marginBottom: LinearDimension?
get() = getAttribute("margin-bottom")
set(value) {
attributes["margin-bottom"] = value.toString()
}
var marginLeft: LinearDimension?
get() = getAttribute("margin-left")
set(value) {
attributes["margin-left"] = value.toString()
}
var marginRight: LinearDimension?
get() = getAttribute("margin-right")
set(value) {
attributes["margin-right"] = value.toString()
}
var maxHeight: LinearDimension?
get() = getAttribute("max-height")
set(value) {
attributes["max-height"] = value.toString()
}
var maxWidth: LinearDimension?
get() = getAttribute("max-width")
set(value) {
attributes["max-width"] = value.toString()
}
var minHeight: LinearDimension?
get() = getAttribute("min-height")
set(value) {
attributes["min-height"] = value.toString()
}
var minWidth: LinearDimension?
get() = getAttribute("min-width")
set(value) {
attributes["min-width"] = value.toString()
}
var overflow: Overflow
get() = getAttribute("overflow", Overflow.inherit)
set(value) {
attributes["overflow"] = value.toString()
}
var padding: BoxDimensions?
get() = getAttribute("padding")
set(value) {
attributes["padding"] = value.toString()
}
var paddingTop: LinearDimension?
get() = getAttribute("padding-top")
set(value) {
attributes["padding-top"] = value.toString()
}
var paddingBottom: LinearDimension?
get() = getAttribute("padding-bottom")
set(value) {
attributes["padding-bottom"] = value.toString()
}
var paddingLeft: LinearDimension?
get() = getAttribute("padding-left")
set(value) {
attributes["padding-left"] = value.toString()
}
var paddingRight: LinearDimension?
get() = getAttribute("padding-right")
set(value) {
attributes["padding-right"] = value.toString()
}
var textAlign: TextAlign
get() = getAttribute("text-align", TextAlign.inherit)
set(value) {
attributes["text-align"] = value.toString()
}
var verticalAlign: VerticalAlign
get() = getAttribute("vertical-align", VerticalAlign.inherit)
set(value) {
attributes["vertical-align"] = value.toString()
}
var width: LinearDimension?
get() = getAttribute("width")
set(value) {
attributes["width"] = value.toString()
}
}