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