1 Commits

Author SHA1 Message Date
5d20a30ab4 v. 1.0.3 - Simplify rendering, replace i.o. update
Took 1 hour 27 minutes
2022-02-07 14:34:41 +01:00
8 changed files with 49 additions and 168 deletions

1
.gitignore vendored
View File

@@ -8,4 +8,3 @@ local.properties
*.ipr *.ipr
*.iws *.iws
kotlin-js-store kotlin-js-store
.idea

View File

@@ -1,12 +1,12 @@
plugins { plugins {
kotlin("multiplatform") version "1.7.20" kotlin("multiplatform") version "1.6.10"
`maven-publish` `maven-publish`
signing signing
id("org.jetbrains.dokka") version "1.5.31" id("org.jetbrains.dokka") version "1.5.31"
} }
group = "nl.astraeus" group = "nl.astraeus"
version = "1.0.8-SNAPSHOT" version = "1.0.3"
repositories { repositories {
mavenCentral() mavenCentral()

View File

@@ -1,5 +1,5 @@
#Wed Mar 04 13:29:12 CET 2020 #Wed Mar 04 13:29:12 CET 2020
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5.1-all.zip distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-all.zip
distributionBase=GRADLE_USER_HOME distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists distributionPath=wrapper/dists
zipStorePath=wrapper/dists zipStorePath=wrapper/dists

View File

@@ -1 +0,0 @@
package nl.astraeus.komp

View File

@@ -40,14 +40,6 @@ fun Element.printTree(indent: Int = 0): String {
} }
result.append(") {") result.append(") {")
result.append("\n") result.append("\n")
for ((name, event) in getKompEvents()) {
result.append(indent.asSpaces())
result.append("on")
result.append(name)
result.append(" -> ")
result.append(event)
result.append("\n")
}
for (index in 0 until childNodes.length) { for (index in 0 until childNodes.length) {
childNodes[index]?.let { childNodes[index]?.let {
if (it is Element) { if (it is Element) {
@@ -65,61 +57,39 @@ fun Element.printTree(indent: Int = 0): String {
return result.toString() return result.toString()
} }
internal fun Element.setKompAttribute(attributeName: String, value: String?) { internal fun Element.setKompAttribute(name: String, value: String?) {
//val attributeName = name.lowercase() // val setAttrs: MutableSet<String> = getKompAttributes()
// setAttrs.add(name)
//getNewAttributes().add(name)
if (value == null || value.isBlank()) { if (value == null || value.isBlank()) {
if (this is HTMLInputElement) { if (this is HTMLInputElement) {
when (attributeName) { checked = false
"checked" -> {
checked = false
}
/*
"class" -> {
className = ""
}
*/
"value" -> {
this.value = ""
}
else -> {
removeAttribute(attributeName)
}
}
} else { } else {
removeAttribute(attributeName) removeAttribute(name)
} }
} else { } else {
if (this is HTMLInputElement) { if (this is HTMLInputElement) {
when (attributeName) { when (name) {
"checked" -> { "checked" -> {
checked = "checked" == value checked = "checked" == value
} }
/*
"class" -> { "class" -> {
className = value className = value
} }
*/
"value" -> { "value" -> {
this.value = value this.value = value
} }
else -> { else -> {
setAttribute(attributeName, value) setAttribute(name, value)
} }
} }
} else if (this.getAttribute(attributeName) != value) { } else if (this.getAttribute(name) != value) {
setAttribute(attributeName, value) setAttribute(name, value)
} }
} }
} }
internal fun Element.clearKompEvents() {
val events = getKompEvents()
for ((name, event) in getKompEvents()) {
removeEventListener(name, event)
}
events.clear()
}
internal fun Element.setKompEvent(name: String, event: (Event) -> Unit) { internal fun Element.setKompEvent(name: String, event: (Event) -> Unit) {
val eventName: String = if (name.startsWith("on")) { val eventName: String = if (name.startsWith("on")) {
name.substring(2) name.substring(2)
@@ -127,22 +97,9 @@ internal fun Element.setKompEvent(name: String, event: (Event) -> Unit) {
name name
} }
getKompEvents()[eventName] = event
this.addEventListener(eventName, event) this.addEventListener(eventName, event)
} }
internal fun Element.getKompEvents(): MutableMap<String, (Event) -> Unit> {
var result: MutableMap<String, (Event) -> Unit>? = this.asDynamic()["komp-events"] as MutableMap<String, (Event) -> Unit>?
if (result == null) {
result = mutableMapOf()
this.asDynamic()["komp-events"] = result
}
return result
}
internal fun Element.findElementIndex(): Int { internal fun Element.findElementIndex(): Int {
val childNodes = parentElement?.children val childNodes = parentElement?.children
if (childNodes != null) { if (childNodes != null) {

View File

@@ -9,7 +9,6 @@ import kotlinx.html.TagConsumer
import kotlinx.html.Unsafe import kotlinx.html.Unsafe
import org.w3c.dom.Element import org.w3c.dom.Element
import org.w3c.dom.HTMLElement import org.w3c.dom.HTMLElement
import org.w3c.dom.HTMLInputElement
import org.w3c.dom.HTMLSpanElement import org.w3c.dom.HTMLSpanElement
import org.w3c.dom.Node import org.w3c.dom.Node
import org.w3c.dom.asList import org.w3c.dom.asList
@@ -29,8 +28,7 @@ fun FlowOrMetaDataOrPhrasingContent.currentElement(): Element =
private data class ElementIndex( private data class ElementIndex(
val parent: Node, val parent: Node,
var childIndex: Int, var childIndex: Int
var setAttr: MutableSet<String> = mutableSetOf()
) )
private fun ArrayList<ElementIndex>.currentParent(): Node { private fun ArrayList<ElementIndex>.currentParent(): Node {
@@ -49,17 +47,8 @@ private fun ArrayList<ElementIndex>.currentElement(): Node? {
return null return null
} }
private fun ArrayList<ElementIndex>.currentPosition(): ElementIndex? {
return if (this.size < 2) {
null
} else {
this[this.size - 2]
}
}
private fun ArrayList<ElementIndex>.nextElement() { private fun ArrayList<ElementIndex>.nextElement() {
this.lastOrNull()?.let { this.lastOrNull()?.let {
it.setAttr.clear()
it.childIndex++ it.childIndex++
} }
} }
@@ -74,10 +63,7 @@ private fun ArrayList<ElementIndex>.push(element: Node) {
private fun ArrayList<ElementIndex>.replace(new: Node) { private fun ArrayList<ElementIndex>.replace(new: Node) {
if (this.currentElement() != null) { if (this.currentElement() != null) {
this.currentElement()?.parentElement?.replaceChild( this.currentElement()?.parentElement?.replaceChild(new, this.currentElement()!!)
new,
this.currentElement()!!
)
} else { } else {
this.last().parent.appendChild(new) this.last().parent.appendChild(new)
} }
@@ -86,14 +72,14 @@ private fun ArrayList<ElementIndex>.replace(new: Node) {
private fun Node.asElement() = this as? HTMLElement private fun Node.asElement() = this as? HTMLElement
class HtmlBuilder( class HtmlBuilder(
private val komponent: Komponent?, val komponent: Komponent?,
parent: Element, parent: Element,
childIndex: Int = 0 childIndex: Int = 0,
) : HtmlConsumer { ) : HtmlConsumer {
private var currentPosition = arrayListOf<ElementIndex>() private var currentPosition = arrayListOf<ElementIndex>()
private var inDebug = false private var inDebug = false
private var exceptionThrown = false private var exceptionThrown = false
private var currentNode: Node? = null var currentNode: Node? = null
var root: Element? = null var root: Element? = null
init { init {
@@ -107,9 +93,7 @@ class HtmlBuilder(
) { ) {
currentPosition.replace(komponent.element!!) currentPosition.replace(komponent.element!!)
if (Komponent.logRenderEvent) { if (Komponent.logRenderEvent) {
console.log( console.log("Skipped include $komponent, memoize hasn't changed")
"Skipped include $komponent, memoize hasn't changed"
)
} }
} else { } else {
komponent.create( komponent.create(
@@ -138,21 +122,18 @@ class HtmlBuilder(
} }
} }
private fun logReplace(msg: () -> String) { private fun logReplace(msg: String) {
if (Komponent.logReplaceEvent && inDebug) { if (Komponent.logReplaceEvent && inDebug) {
console.log(msg.invoke()) console.log(msg)
} }
} }
override fun onTagStart(tag: Tag) { override fun onTagStart(tag: Tag) {
logReplace { logReplace("onTagStart, [${tag.tagName}, ${tag.namespace}], currentPosition: $currentPosition")
"onTagStart, [${tag.tagName}, ${tag.namespace}], currentPosition: $currentPosition"
}
currentNode = currentPosition.currentElement() currentNode = currentPosition.currentElement()
if (currentNode == null) { if (currentNode == null) {
logReplace { "onTagStart, currentNode1: $currentNode" } logReplace("onTagStart, currentNode1: $currentNode")
currentNode = if (tag.namespace != null) { currentNode = if (tag.namespace != null) {
document.createElementNS(tag.namespace, tag.tagName) document.createElementNS(tag.namespace, tag.tagName)
} else { } else {
@@ -161,19 +142,9 @@ class HtmlBuilder(
//logReplace"onTagStart, currentElement1.1: $currentNode") //logReplace"onTagStart, currentElement1.1: $currentNode")
currentPosition.currentParent().appendChild(currentNode!!) currentPosition.currentParent().appendChild(currentNode!!)
} else if ( } else {
!currentNode?.asElement()?.tagName.equals(tag.tagName, true) || logReplace("onTagStart, currentElement, namespace: ${currentNode?.asElement()?.namespaceURI} -> ${tag.namespace}")
( logReplace("onTagStart, currentElement, replace: ${currentNode?.asElement()?.tagName} -> ${tag.tagName}")
tag.namespace != null &&
!currentNode?.asElement()?.namespaceURI.equals(tag.namespace, true)
)
) {
logReplace {
"onTagStart, currentElement, namespace: ${currentNode?.asElement()?.namespaceURI} -> ${tag.namespace}"
}
logReplace {
"onTagStart, currentElement, replace: ${currentNode?.asElement()?.tagName} -> ${tag.tagName}"
}
currentNode = if (tag.namespace != null) { currentNode = if (tag.namespace != null) {
document.createElementNS(tag.namespace, tag.tagName) document.createElementNS(tag.namespace, tag.tagName)
@@ -192,15 +163,15 @@ class HtmlBuilder(
root = currentNode as Element root = currentNode as Element
} }
currentElement?.clearKompEvents()
(currentElement as? HTMLInputElement)?.checked = false
currentPosition.lastOrNull()?.setAttr?.clear()
// if currentElement = checkbox make sure it's cleared
for (entry in tag.attributesEntries) { for (entry in tag.attributesEntries) {
currentElement!!.setKompAttribute(entry.key, entry.value) val attributeName = entry.key.lowercase()
currentPosition.lastOrNull()?.setAttr?.add(entry.key) currentElement!!.setKompAttribute(attributeName, entry.value)
}
if (tag.namespace != null) {
//logReplace"onTagStart, same node type")
(currentNode as? Element)?.innerHTML = ""
} }
} }
@@ -218,37 +189,26 @@ class HtmlBuilder(
} }
} }
override fun onTagAttributeChange( override fun onTagAttributeChange(tag: Tag, attribute: String, value: String?) {
tag: Tag, logReplace("onTagAttributeChange, ${tag.tagName} [$attribute, $value]")
attribute: String,
value: String?
) {
logReplace { "onTagAttributeChange, ${tag.tagName} [$attribute, $value]" }
if (Komponent.enableAssertions) { if (Komponent.enableAssertions) {
checkTag(tag) checkTag(tag)
} }
currentElement?.setKompAttribute(attribute, value) val attributeName = attribute.lowercase()
if (value == null || value.isEmpty()) {
currentPosition.currentPosition()?.setAttr?.remove(attribute) currentElement?.setKompAttribute(attributeName, value)
} else {
currentPosition.currentPosition()?.setAttr?.add(attribute)
}
} }
override fun onTagEvent( override fun onTagEvent(tag: Tag, event: String, value: (Event) -> Unit) {
tag: Tag, logReplace("onTagEvent, ${tag.tagName} [$event, $value]")
event: String,
value: (Event) -> Unit
) {
logReplace { "onTagEvent, ${tag.tagName} [$event, $value]" }
if (Komponent.enableAssertions) { if (Komponent.enableAssertions) {
checkTag(tag) checkTag(tag)
} }
currentElement?.setKompEvent(event.toLowerCase(), value) currentElement?.setKompEvent(event.lowercase(), value)
} }
override fun onTagEnd(tag: Tag) { override fun onTagEnd(tag: Tag) {
@@ -266,33 +226,8 @@ class HtmlBuilder(
checkTag(tag) checkTag(tag)
} }
if (currentElement != null) {
val setAttrs: Set<String> = currentPosition.currentPosition()?.setAttr ?: setOf()
// remove attributes that where not set
val element = currentElement
if (element?.hasAttributes() == true) {
for (index in 0 until element.attributes.length) {
val attribute = element.attributes[index]
if (attribute?.name != null) {
val attr = attribute.name
if (
!setAttrs.contains(attr) &&
attr != "style"
) {
element.setKompAttribute(attr, null)
}
}
}
}
}
currentPosition.pop() currentPosition.pop()
currentNode = currentPosition.currentElement()
currentElement = currentNode as? Element ?: currentElement
currentPosition.nextElement() currentPosition.nextElement()
currentElement = currentElement?.parentElement as? HTMLElement currentElement = currentElement?.parentElement as? HTMLElement
@@ -366,10 +301,7 @@ class HtmlBuilder(
//logReplace"onTagContentUnsafe, namespace: [$namespace]") //logReplace"onTagContentUnsafe, namespace: [$namespace]")
if (Komponent.unsafeMode == UnsafeMode.UNSAFE_ALLOWED || if (Komponent.unsafeMode == UnsafeMode.UNSAFE_ALLOWED ||
( (Komponent.unsafeMode == UnsafeMode.UNSAFE_SVG_ONLY && namespace == "http://www.w3.org/2000/svg")
Komponent.unsafeMode == UnsafeMode.UNSAFE_SVG_ONLY &&
namespace == "http://www.w3.org/2000/svg"
)
) { ) {
if (currentElement?.innerHTML != textContent) { if (currentElement?.innerHTML != textContent) {
currentElement?.innerHTML += textContent currentElement?.innerHTML += textContent
@@ -401,7 +333,7 @@ class HtmlBuilder(
if (exception !is KomponentException) { if (exception !is KomponentException) {
val position = mutableListOf<Element>() val position = mutableListOf<Element>()
var ce = currentElement var ce = currentElement
while (ce != null) { while(ce != null) {
position.add(ce) position.add(ce)
ce = ce.parentElement ce = ce.parentElement
} }
@@ -419,7 +351,6 @@ class HtmlBuilder(
} }
builder.append(" ") builder.append(" ")
} }
throw KomponentException( throw KomponentException(
komponent, komponent,
currentElement, currentElement,
@@ -435,15 +366,13 @@ class HtmlBuilder(
override fun finalize(): Element { override fun finalize(): Element {
//logReplace"finalize, currentPosition: $currentPosition") //logReplace"finalize, currentPosition: $currentPosition")
return root ?: throw IllegalStateException( return root ?: throw IllegalStateException("We can't finalize as there was no tags")
"We can't finalize as there was no tags"
)
} }
companion object { companion object {
fun create(content: HtmlBuilder.() -> Unit): Element { fun create(content: HtmlBuilder.() -> Unit): Element {
val container = document.createElement("div") as HTMLElement val container = document.createElement("div") as HTMLElement
val consumer = HtmlBuilder(null, container) val consumer = HtmlBuilder(null, container, 0)
content.invoke(consumer) content.invoke(consumer)
return consumer.root ?: error("No root element found after render!") return consumer.root ?: error("No root element found after render!")
} }

View File

@@ -7,7 +7,6 @@ import org.w3c.dom.HTMLElement
import org.w3c.dom.get import org.w3c.dom.get
private var currentKomponent: Komponent? = null private var currentKomponent: Komponent? = null
fun FlowOrMetaDataOrPhrasingContent.currentKomponent(): Komponent = fun FlowOrMetaDataOrPhrasingContent.currentKomponent(): Komponent =
currentKomponent ?: error("No current komponent defined! Only call from render code!") currentKomponent ?: error("No current komponent defined! Only call from render code!")
@@ -138,6 +137,7 @@ abstract class Komponent {
} }
} }
val builder = HtmlBuilder(this, parent, childIndex) val builder = HtmlBuilder(this, parent, childIndex)
builder.root = null
try { try {
currentKomponent = this currentKomponent = this

View File

@@ -2,7 +2,6 @@ package nl.astraeus.komp
import kotlinx.browser.document import kotlinx.browser.document
import kotlinx.html.InputType import kotlinx.html.InputType
import kotlinx.html.classes
import kotlinx.html.div import kotlinx.html.div
import kotlinx.html.i import kotlinx.html.i
import kotlinx.html.id import kotlinx.html.id
@@ -38,7 +37,6 @@ class Child1 : Komponent() {
class Child2 : Komponent() { class Child2 : Komponent() {
override fun HtmlBuilder.render() { override fun HtmlBuilder.render() {
div { div {
id ="1234"
+"Child 2" +"Child 2"
} }
} }
@@ -211,8 +209,7 @@ class TestUpdate {
fun testCreate() { fun testCreate() {
var elemTest: Element? = null var elemTest: Element? = null
val element = HtmlBuilder.create { val element = HtmlBuilder.create {
div(classes = "div_class") { div("div_class") {
classes = classes + "bla'"
id = "123" id = "123"
+"Test" +"Test"