Allow creation of HtmlElement with HtmlBuilder
This commit is contained in:
@@ -11,6 +11,7 @@ import org.w3c.dom.Node
|
||||
import org.w3c.dom.asList
|
||||
import org.w3c.dom.css.CSSStyleDeclaration
|
||||
import org.w3c.dom.events.Event
|
||||
import kotlin.browser.document
|
||||
|
||||
@Suppress("NOTHING_TO_INLINE")
|
||||
private inline fun HTMLElement.setEvent(name: String, noinline callback : (Event) -> Unit) : Unit {
|
||||
@@ -139,7 +140,7 @@ class HtmlBuilder(
|
||||
// stupid hack as browsers doesn't support createEntityReference
|
||||
val s = document.createElement("span") as HTMLElement
|
||||
s.innerHTML = entity.text
|
||||
path.last().appendChild(s.childNodes.asList().filter { it.nodeType == Node.TEXT_NODE }.first())
|
||||
path.last().appendChild(s.childNodes.asList().first { it.nodeType == Node.TEXT_NODE })
|
||||
|
||||
// other solution would be
|
||||
// pathLast().innerHTML += entity.text
|
||||
@@ -170,4 +171,11 @@ class HtmlBuilder(
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
private fun HTMLElement.asR(): HTMLElement = this.asDynamic()
|
||||
|
||||
companion object {
|
||||
fun create(content: HtmlBuilder.() -> Unit) : HTMLElement {
|
||||
val consumer = HtmlBuilder(DummyKomponent(), document)
|
||||
content.invoke(consumer)
|
||||
return consumer.finalize()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package nl.astraeus.komp
|
||||
|
||||
import kotlinx.html.Tag
|
||||
import kotlinx.html.div
|
||||
import org.w3c.dom.HTMLDivElement
|
||||
import org.w3c.dom.HTMLElement
|
||||
import org.w3c.dom.Node
|
||||
@@ -20,6 +21,14 @@ fun Tag.include(component: Komponent) {
|
||||
}
|
||||
}
|
||||
|
||||
class DummyKomponent: Komponent() {
|
||||
override fun HtmlBuilder.render() {
|
||||
div {
|
||||
+ "dummy"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
abstract class Komponent {
|
||||
var element: Node? = null
|
||||
val declaredStyles: MutableMap<String, CSSStyleDeclaration> = HashMap()
|
||||
|
||||
Reference in New Issue
Block a user