Testing dom diffing

This commit is contained in:
2017-09-11 17:11:58 +02:00
parent 6a69ce85cb
commit f40b6d5f64
5 changed files with 27 additions and 51 deletions

View File

@@ -19,8 +19,12 @@ repositories {
mavenCentral() mavenCentral()
} }
ext {
kotlin_version = '1.1.4-2'
}
buildscript { buildscript {
ext.kotlin_version = '1.1.4' ext.kotlin_version = '1.1.4-2'
repositories { repositories {
maven { maven {
url "http://nexus.astraeus.nl/nexus/content/groups/public" url "http://nexus.astraeus.nl/nexus/content/groups/public"
@@ -34,7 +38,7 @@ buildscript {
dependencies { dependencies {
compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version" compile "org.jetbrains.kotlin:kotlin-stdlib-js:$kotlin_version"
compile 'org.jetbrains.kotlinx:kotlinx-html-js:0.6.2' compile 'org.jetbrains.kotlinx:kotlinx-html-js:0.6.4'
} }
compileKotlin2Js { compileKotlin2Js {

View File

@@ -22,6 +22,7 @@
<option name="pluginClasspaths"> <option name="pluginClasspaths">
<array /> <array />
</option> </option>
<option name="verbose" value="true" />
<option name="suppressWarnings" value="true" /> <option name="suppressWarnings" value="true" />
</compilerArguments> </compilerArguments>
</configuration> </configuration>
@@ -44,7 +45,7 @@
</content> </content>
<orderEntry type="inheritedJdk" /> <orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="Gradle: org.jetbrains.kotlin:kotlin-stdlib-js:1.1.4" level="project" /> <orderEntry type="library" name="Gradle: org.jetbrains.kotlin:kotlin-stdlib-js:1.1.4-2" level="project" />
<orderEntry type="library" name="Gradle: org.jetbrains.kotlinx:kotlinx-html-js:0.6.2" level="project" /> <orderEntry type="library" name="Gradle: org.jetbrains.kotlinx:kotlinx-html-js:0.6.2" level="project" />
<orderEntry type="library" name="Gradle: org.jetbrains.kotlinx:kotlinx-html-common:0.6.2" level="project" /> <orderEntry type="library" name="Gradle: org.jetbrains.kotlinx:kotlinx-html-common:0.6.2" level="project" />
<orderEntry type="library" name="Gradle: org.jetbrains.kotlin:kotlin-stdlib-common:1.1.0" level="project" /> <orderEntry type="library" name="Gradle: org.jetbrains.kotlin:kotlin-stdlib-common:1.1.0" level="project" />

View File

@@ -139,13 +139,13 @@
<root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.1.0/446dff483a1ff560b2da6058481da04c0e97428f/kotlin-stdlib-common-1.1.0-sources.jar!/" /> <root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-common/1.1.0/446dff483a1ff560b2da6058481da04c0e97428f/kotlin-stdlib-common-1.1.0-sources.jar!/" />
</SOURCES> </SOURCES>
</library> </library>
<library name="Gradle: org.jetbrains.kotlin:kotlin-stdlib-js:1.1.4" type="kotlin.js"> <library name="Gradle: org.jetbrains.kotlin:kotlin-stdlib-js:1.1.4-2" type="kotlin.js">
<CLASSES> <CLASSES>
<root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-js/1.1.4/55f39a77bc0893576d48e1cc4065e41d6feb9233/kotlin-stdlib-js-1.1.4.jar!/" /> <root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-js/1.1.4-2/bb372269ed17ca9ffad8bb253440544ce209dabd/kotlin-stdlib-js-1.1.4-2.jar!/" />
</CLASSES> </CLASSES>
<JAVADOC /> <JAVADOC />
<SOURCES> <SOURCES>
<root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-js/1.1.4/cac83db2042ebed585ce6cf418079aea2a14ade0/kotlin-stdlib-js-1.1.4-sources.jar!/" /> <root url="jar://$USER_HOME$/.gradle/caches/modules-2/files-2.1/org.jetbrains.kotlin/kotlin-stdlib-js/1.1.4-2/b3d59978e58be7402fe3c061ed5928a95f851c8a/kotlin-stdlib-js-1.1.4-2-sources.jar!/" />
</SOURCES> </SOURCES>
</library> </library>
<library name="Gradle: org.jetbrains.kotlinx:kotlinx-html-common:0.6.2" type="kotlin.js"> <library name="Gradle: org.jetbrains.kotlinx:kotlinx-html-common:0.6.2" type="kotlin.js">

View File

@@ -1,6 +1,7 @@
package nl.astraeus.komp package nl.astraeus.komp
import org.w3c.dom.HTMLElement import org.w3c.dom.Element
import org.w3c.dom.Node
import org.w3c.dom.get import org.w3c.dom.get
/** /**
@@ -11,23 +12,21 @@ import org.w3c.dom.get
object DomDiffer { object DomDiffer {
fun replaceDiff(newElement: HTMLElement, oldElement: HTMLElement): HTMLElement { fun replaceDiff(newElement: Element, oldElement: Element): Element {
println("CHECK $newElement -> $oldElement") if (!newElement.isEqualNode(oldElement)) {
if (!match(newElement, oldElement)) {
println("no match, replace")
replaceNode(newElement, oldElement) replaceNode(newElement, oldElement)
return newElement return newElement
} else { } else {
println("check children")
// think of the children! // think of the children!
for (index in 0 until newElement.childElementCount) { for (index in 0 until newElement.children.length) {
val newChild = newElement.children[index] val newChild = newElement.children[index]
val oldChild = oldElement.children[index] val oldChild = oldElement.children[index]
if (newChild is HTMLElement && oldChild is HTMLElement) { if (newChild is Element && oldChild is Element) {
replaceDiff(newChild, oldChild) replaceDiff(newChild, oldChild)
} else {
throw IllegalStateException("Children are not nodes! $newChild, $oldChild")
} }
} }
@@ -35,39 +34,10 @@ object DomDiffer {
} }
} }
private fun replaceNode(newElement: HTMLElement, oldElement: HTMLElement) { private fun replaceNode(newElement: Node, oldElement: Node) {
val parent = oldElement.parentElement val parent = oldElement.parentElement ?: throw IllegalStateException("oldElement has no parent! $oldElement")
parent?.replaceChild(newElement, oldElement) parent.replaceChild(newElement, oldElement)
}
fun match(newElement: HTMLElement, oldElement: HTMLElement): Boolean {
var result = true
result = result && newElement.namespaceURI == oldElement.namespaceURI
result = result && newElement.nodeName == oldElement.nodeName
result = result && newElement.childElementCount == oldElement.childElementCount
val newAttr = newElement.attributes
val oldAttr = oldElement.attributes
result = result && newAttr.length == oldAttr.length
if (result) {
for (index in 0 until newAttr.length) {
val attr = newAttr[index]
if (attr != null) {
result = result && newAttr.getNamedItem(attr.name)?.name == oldAttr.getNamedItem(attr.name)?.name
result = result && newAttr.getNamedItem(attr.name)?.value == oldAttr.getNamedItem(attr.name)?.value
}
if (!result) {
break
}
}
}
return result
} }
} }

View File

@@ -5,6 +5,7 @@ import kotlinx.html.FORM
import kotlinx.html.SPAN import kotlinx.html.SPAN
import kotlinx.html.TagConsumer import kotlinx.html.TagConsumer
import kotlinx.html.dom.create import kotlinx.html.dom.create
import org.w3c.dom.Element
import org.w3c.dom.HTMLElement import org.w3c.dom.HTMLElement
import kotlin.browser.document import kotlin.browser.document
@@ -30,7 +31,7 @@ fun FORM.include(component: Komponent) {
} }
abstract class Komponent { abstract class Komponent {
var element: HTMLElement? = null var element: Element? = null
var rendered = false var rendered = false
fun create(): HTMLElement { fun create(): HTMLElement {
@@ -65,7 +66,7 @@ abstract class Komponent {
companion object { companion object {
private val elements: MutableMap<HTMLElement, Komponent> = HashMap() private val elements: MutableMap<Element, Komponent> = HashMap()
private val elementList: MutableList<Komponent> = ArrayList() private val elementList: MutableList<Komponent> = ArrayList()
fun define(element: HTMLElement, component: Komponent) { fun define(element: HTMLElement, component: Komponent) {
@@ -86,7 +87,7 @@ abstract class Komponent {
elementList.add(component) elementList.add(component)
} }
fun remove(element: HTMLElement) { fun remove(element: Element) {
val component = elements[element] val component = elements[element]
elements.remove(element) elements.remove(element)
@@ -107,7 +108,7 @@ abstract class Komponent {
refresh(component.element) refresh(component.element)
} }
fun refresh(element: HTMLElement?) { fun refresh(element: Element?) {
if (element != null) { if (element != null) {
elements[element]?.let { elements[element]?.let {
//val parent = element.parentElement //val parent = element.parentElement