fixed bug with lambda return value
All checks were successful
CI / build (push) Successful in 19s

This commit is contained in:
2024-04-14 18:47:22 +08:00
parent 18eb4fac37
commit a0b0301644

View File

@@ -12,9 +12,9 @@ import org.w3c.dom.Document
import org.w3c.dom.Element
import org.w3c.dom.events.Event
class Khtml private constructor(private val doc : Document, val element: Element) {
class Khtml private constructor(private val doc: Document, val element: Element) {
companion object {
fun of(doc : Document, el: Element, cb : Khtml.(el : Element) -> Unit) : Element {
fun of(doc: Document, el: Element, cb: Khtml.(el: Element) -> Unit): Element {
Khtml(doc, el).cb(el)
return el
}
@@ -25,7 +25,7 @@ class Khtml private constructor(private val doc : Document, val element: Element
}
}
fun Element.on(eventName : String, eventListener : (Event) -> Unit) {
fun Element.on(eventName: String, eventListener: (Event) -> Unit) {
addEventListener(eventName, eventListener)
}
@@ -43,137 +43,299 @@ class Khtml private constructor(private val doc : Document, val element: Element
private inline fun bdn(
name : String,
attrs : Map<String, String>,
cb : Khtml.(el : Element) -> Unit) : Element {
name: String,
attrs: Map<String, String>,
cb: Khtml.(el: Element) -> Unit
): Element {
val child = doc.createElement(name)
for((key, value) in attrs) {
for ((key, value) in attrs) {
child.setAttribute(key, value)
}
element.appendChild(child)
Khtml(doc, child).cb(child)
return element
return child
.also {
element.appendChild(it)
Khtml(doc, it).cb(it)
}
}
fun html(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("html", attrs, cb)
fun head(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("head", attrs, cb)
fun body(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("body", attrs, cb)
fun use(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("use", attrs, cb)
fun svg(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("svg", attrs, cb)
fun div(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("div", attrs, cb)
fun header(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("header", attrs, cb)
fun main(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("main", attrs, cb)
fun footer(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("footer", attrs, cb)
fun a(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("a", attrs, cb)
fun meta(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("meta", attrs, cb)
fun script(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("script", attrs, cb)
fun link(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("link", attrs, cb)
fun title(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("title", attrs, cb)
fun p(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("p", attrs, cb)
fun span(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("span", attrs, cb)
fun i(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("i", attrs, cb)
fun del(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("del", attrs, cb)
fun s(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("s", attrs, cb)
fun ins(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("ins", attrs, cb)
fun u(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("u", attrs, cb)
fun b(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("b", attrs, cb)
fun small(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("small", attrs, cb)
fun strong(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("strong", attrs, cb)
fun em(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("em", attrs, cb)
fun mark(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("mark", attrs, cb)
fun obj(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("object", attrs, cb)
fun h1(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("h1", attrs, cb)
fun h2(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("h2", attrs, cb)
fun h3(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("h3", attrs, cb)
fun h4(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("h4", attrs, cb)
fun h5(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("h5", attrs, cb)
fun h6(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("h6", attrs, cb)
fun table(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("table", attrs, cb)
fun thead(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("thead", attrs, cb)
fun tbody(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("tbody", attrs, cb)
fun tfoot(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("tfoot", attrs, cb)
fun tr(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("tr", attrs, cb)
fun th(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("th", attrs, cb)
fun td(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("td", attrs, cb)
fun ol(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("ol", attrs, cb)
fun ul(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("ul", attrs, cb)
fun li(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("li", attrs, cb)
fun img(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("img", attrs, cb)
fun form(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("form", attrs, cb)
fun label(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("label", attrs, cb)
fun button(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("button", attrs, cb)
fun input(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("input", attrs, cb)
fun select(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("select", attrs, cb)
fun option(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("option", attrs, cb)
fun meter(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("meter", attrs, cb)
fun nav(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("nav", attrs, cb)
fun menu(attrs : Map<String, String> = emptyMap(),
cb : Khtml.(el : Element) -> Unit) : Element = bdn("menu", attrs, cb)
fun classes(vararg classes : String) {
for(cls in classes) element.classList.add(cls)
fun html(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("html", attrs, cb)
fun head(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("head", attrs, cb)
fun body(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("body", attrs, cb)
fun use(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("use", attrs, cb)
fun svg(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("svg", attrs, cb)
fun div(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("div", attrs, cb)
fun header(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("header", attrs, cb)
fun main(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("main", attrs, cb)
fun footer(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("footer", attrs, cb)
fun a(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("a", attrs, cb)
fun meta(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("meta", attrs, cb)
fun script(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("script", attrs, cb)
fun link(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("link", attrs, cb)
fun title(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("title", attrs, cb)
fun p(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("p", attrs, cb)
fun span(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("span", attrs, cb)
fun i(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("i", attrs, cb)
fun del(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("del", attrs, cb)
fun s(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("s", attrs, cb)
fun ins(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("ins", attrs, cb)
fun u(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("u", attrs, cb)
fun b(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("b", attrs, cb)
fun small(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("small", attrs, cb)
fun strong(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("strong", attrs, cb)
fun em(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("em", attrs, cb)
fun mark(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("mark", attrs, cb)
fun obj(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("object", attrs, cb)
fun h1(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("h1", attrs, cb)
fun h2(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("h2", attrs, cb)
fun h3(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("h3", attrs, cb)
fun h4(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("h4", attrs, cb)
fun h5(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("h5", attrs, cb)
fun h6(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("h6", attrs, cb)
fun table(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("table", attrs, cb)
fun thead(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("thead", attrs, cb)
fun tbody(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("tbody", attrs, cb)
fun tfoot(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("tfoot", attrs, cb)
fun tr(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("tr", attrs, cb)
fun th(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("th", attrs, cb)
fun td(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("td", attrs, cb)
fun ol(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("ol", attrs, cb)
fun ul(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("ul", attrs, cb)
fun li(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("li", attrs, cb)
fun img(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("img", attrs, cb)
fun form(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("form", attrs, cb)
fun label(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("label", attrs, cb)
fun button(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("button", attrs, cb)
fun input(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("input", attrs, cb)
fun select(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("select", attrs, cb)
fun option(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("option", attrs, cb)
fun meter(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("meter", attrs, cb)
fun nav(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("nav", attrs, cb)
fun menu(
attrs: Map<String, String> = emptyMap(),
cb: Khtml.(el: Element) -> Unit
): Element = bdn("menu", attrs, cb)
fun classes(vararg classes: String) {
for (cls in classes) element.classList.add(cls)
}
fun attr(key: String, value : String) {
fun attr(key: String, value: String) {
element.setAttribute(key, value)
}
fun text(txt : String) {
fun text(txt: String) {
element.appendChild(doc.createTextNode(txt))
}
fun on(eventName : String, cb: (Event) -> Unit) {
fun on(eventName: String, cb: (Event) -> Unit) {
element.addEventListener(eventName, cb)
}
}