Allow creation of HtmlElement with HtmlBuilder

This commit is contained in:
2020-01-27 21:28:09 +01:00
parent 9868be505e
commit c798636064
4 changed files with 20 additions and 3 deletions

View File

@@ -1,5 +1,5 @@
group 'nl.astraeus'
version '0.1.14-SNAPSHOT'
version '0.1.15-SNAPSHOT'
apply plugin: 'kotlin2js'
apply plugin: 'kotlin-dce-js'

View File

@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<module external.linked.project.id="komp" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="nl.astraeus" external.system.module.version="0.1.14-SNAPSHOT" type="JAVA_MODULE" version="4">
<module external.linked.project.id="komp" external.linked.project.path="$MODULE_DIR$" external.root.project.path="$MODULE_DIR$" external.system.id="GRADLE" external.system.module.group="nl.astraeus" external.system.module.version="0.1.15-SNAPSHOT" type="JAVA_MODULE" version="4">
<component name="FacetManager">
<facet type="kotlin-language" name="Kotlin">
<configuration version="3" platform="JavaScript " allPlatforms="JS []" useProjectSettings="false">

View File

@@ -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()
}
}
}

View File

@@ -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()