Function builder test

This commit is contained in:
2020-03-01 13:19:27 +01:00
parent 6e5c10c089
commit 1f819c925f
6 changed files with 117 additions and 101 deletions

View File

@@ -10,57 +10,27 @@ annotation class CssTagMarker
abstract class CssGenerator {
val definitions: MutableMap<String, Css> = mutableMapOf()
val extentions: MutableList<Css> = mutableListOf()
abstract fun getMapping(): Map<String, Any?>
val properties: MutableMap<String, List<CssProperty>> = mutableMapOf()
abstract fun getValidator(name: String): List<Validator>?
private fun propertyCss(indent: String, name: String, prop: CssProperty, minified: Boolean): String {
private fun propertyCss(indent: String, name: String, props: List<CssProperty>, minified: Boolean): String {
val builder = StringBuilder()
getValidator(name)?.forEach {
if (!it.validate(prop)) {
if (!it.validate(props)) {
log.warn { "Validate error '$name' - ${it.getMessage(name)}" }
}
}
return if (!minified) {
val paddedName = StringBuilder()
paddedName.append(indent)
paddedName.append(name)
paddedName.append(":")
while (paddedName.length < 32) {
paddedName.append(' ')
}
"$paddedName${prop.css()};\n"
} else {
"$name:${prop.css()};"
}
}
private fun propertyCss(indent: String, name: String, props: List<*>, minified: Boolean): String {
val builder = StringBuilder()
getValidator(name)?.forEach {
if (!it.validate(props as List<CssProperty>)) {
log.warn { "Validate error '$name' - ${it.getListMessage(name)}" }
}
}
for ((index, prop) in props.withIndex()) {
if (prop is CssProperty) {
getValidator(name)?.forEach {
if (!it.validate(prop)) {
log.warn { "Validate error '$name' - ${it.getMessage(name)}" }
}
for (prop in props) {
if (builder.isNotEmpty()) {
builder.append(",")
if (!minified) {
builder.append(" ")
}
if (builder.isNotEmpty()) {
builder.append(",")
if (!minified) {
builder.append(" ")
}
}
builder.append(prop.css())
}
builder.append(prop.css())
}
return if (!minified) {
@@ -80,12 +50,8 @@ abstract class CssGenerator {
fun generatePropertyCss(indent: String, minified: Boolean): String {
val builder = StringBuilder()
for ((name, prop) in getMapping()) {
if (prop is List<*> && prop.isNotEmpty()) {
builder.append(propertyCss(indent, name, prop, minified))
} else if (prop is CssProperty) {
builder.append(propertyCss(indent, name, prop, minified))
}
for ((name, prop) in properties) {
builder.append(propertyCss(indent, name, prop, minified))
}
return builder.toString()
@@ -145,6 +111,7 @@ abstract class CssGenerator {
@CssTagMarker
open class Style(
/*
var alignContent: AlignContent? = null,
var alignItems: AlignItems? = null,
var alignSelf: AlignSelf? = null,
@@ -248,10 +215,12 @@ open class Style(
var fontSize: FontSize? = null,
var height: Measurement? = null,
var left: Measurement? = null,
var margin: List<Measurement>? = null,
var top: Measurement? = null,
var transitionDelay: DelayDuration? = null,
var transitionDuration: DelayDuration? = null,
var width: Measurement? = null
*/
) : CssGenerator() {
var fontFace: FontFace? = null
private val validators = mapOf<String, List<Validator>>(
@@ -270,6 +239,7 @@ open class Style(
override fun getValidator(name: String) = validators[name]
/*
override fun getMapping(): Map<String, Any?> = mapOf(
"align-content" to alignContent,
"align-items" to alignItems,
@@ -369,11 +339,13 @@ open class Style(
"font-size" to fontSize,
"height" to height,
"left" to left,
"margin" to margin,
"top" to top,
"transition-delay" to transitionDelay,
"transition-duration" to transitionDuration,
"width" to width
)
*/
fun select(selector: String, style: Css) {
definitions[selector] = style
@@ -388,4 +360,20 @@ open class Style(
face.invoke(fontFace!!)
}
fun alignContent(value: AlignContentValue) {
properties["align-content"] = listOf(CssProperty(value.css()))
}
fun margin(all: Measurement) {
properties["margin"] = listOf(all)
}
fun margin(topBottom: Measurement, leftRight: Measurement) {
properties["margin"] = listOf(topBottom, leftRight)
}
fun margin(top: Measurement, right: Measurement, bottom: Measurement, left: Measurement) {
properties["margin"] = listOf(top, right, bottom, left)
}
}