From 34dcd6fc7516b4fbe06ea6765ce7dba6db955fc0 Mon Sep 17 00:00:00 2001 From: rnentjes Date: Sun, 10 Sep 2017 15:06:27 +0200 Subject: [PATCH] Testing dom diffing --- .idea/compiler.xml | 9 - .idea/encodings.xml | 6 - .idea/gradle.xml | 18 -- ...ains_kotlin_kotlin_stdlib_common_1_1_0.xml | 11 -- ...etbrains_kotlin_kotlin_stdlib_js_1_1_4.xml | 11 -- ...ains_kotlinx_kotlinx_html_common_0_6_2.xml | 11 -- ...etbrains_kotlinx_kotlinx_html_js_0_6_2.xml | 11 -- .idea/misc.xml | 6 - .idea/modules.xml | 10 - .idea/modules/komp.iml | 13 -- .idea/modules/komp_test.iml | 44 ----- .idea/vcs.xml | 6 - build.gradle | 2 +- gradle/wrapper/gradle-wrapper.properties | 4 +- .idea/modules/komp_main.iml => komp.iml | 26 ++- komp.ipr | 186 ++++++++++++++++++ src/main/kotlin/nl/astraeus/komp/DomDiff.kt | 65 ++++++ src/main/kotlin/nl/astraeus/komp/Komponent.kt | 1 + 18 files changed, 271 insertions(+), 169 deletions(-) delete mode 100644 .idea/compiler.xml delete mode 100644 .idea/encodings.xml delete mode 100644 .idea/gradle.xml delete mode 100644 .idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_common_1_1_0.xml delete mode 100644 .idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_js_1_1_4.xml delete mode 100644 .idea/libraries/Gradle__org_jetbrains_kotlinx_kotlinx_html_common_0_6_2.xml delete mode 100644 .idea/libraries/Gradle__org_jetbrains_kotlinx_kotlinx_html_js_0_6_2.xml delete mode 100644 .idea/misc.xml delete mode 100644 .idea/modules.xml delete mode 100644 .idea/modules/komp.iml delete mode 100644 .idea/modules/komp_test.iml delete mode 100644 .idea/vcs.xml rename .idea/modules/komp_main.iml => komp.iml (54%) create mode 100644 komp.ipr create mode 100644 src/main/kotlin/nl/astraeus/komp/DomDiff.kt diff --git a/.idea/compiler.xml b/.idea/compiler.xml deleted file mode 100644 index a646e3b..0000000 --- a/.idea/compiler.xml +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/.idea/encodings.xml b/.idea/encodings.xml deleted file mode 100644 index 97626ba..0000000 --- a/.idea/encodings.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/gradle.xml b/.idea/gradle.xml deleted file mode 100644 index 3d0a41a..0000000 --- a/.idea/gradle.xml +++ /dev/null @@ -1,18 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_common_1_1_0.xml b/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_common_1_1_0.xml deleted file mode 100644 index 3810470..0000000 --- a/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_common_1_1_0.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_js_1_1_4.xml b/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_js_1_1_4.xml deleted file mode 100644 index 1b8b422..0000000 --- a/.idea/libraries/Gradle__org_jetbrains_kotlin_kotlin_stdlib_js_1_1_4.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_jetbrains_kotlinx_kotlinx_html_common_0_6_2.xml b/.idea/libraries/Gradle__org_jetbrains_kotlinx_kotlinx_html_common_0_6_2.xml deleted file mode 100644 index 835e744..0000000 --- a/.idea/libraries/Gradle__org_jetbrains_kotlinx_kotlinx_html_common_0_6_2.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/libraries/Gradle__org_jetbrains_kotlinx_kotlinx_html_js_0_6_2.xml b/.idea/libraries/Gradle__org_jetbrains_kotlinx_kotlinx_html_js_0_6_2.xml deleted file mode 100644 index 902474d..0000000 --- a/.idea/libraries/Gradle__org_jetbrains_kotlinx_kotlinx_html_js_0_6_2.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml deleted file mode 100644 index 84da703..0000000 --- a/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml deleted file mode 100644 index 86a46a8..0000000 --- a/.idea/modules.xml +++ /dev/null @@ -1,10 +0,0 @@ - - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules/komp.iml b/.idea/modules/komp.iml deleted file mode 100644 index a7df91a..0000000 --- a/.idea/modules/komp.iml +++ /dev/null @@ -1,13 +0,0 @@ - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/modules/komp_test.iml b/.idea/modules/komp_test.iml deleted file mode 100644 index 8991e48..0000000 --- a/.idea/modules/komp_test.iml +++ /dev/null @@ -1,44 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml deleted file mode 100644 index 94a25f7..0000000 --- a/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/build.gradle b/build.gradle index c70e633..efa0f3d 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ group 'nl.astraeus' -version '0.0.5-SNAPSHOT' +version '0.0.6-SNAPSHOT' apply plugin: 'kotlin2js' apply plugin: 'idea' diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 4e7ab3c..24f7c83 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ -#Sun Sep 03 11:51:07 CEST 2017 +#Sun Sep 10 13:56:55 CEST 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME zipStorePath=wrapper/dists -distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-bin.zip +distributionUrl=https\://services.gradle.org/distributions/gradle-2.5-all.zip diff --git a/.idea/modules/komp_main.iml b/komp.iml similarity index 54% rename from .idea/modules/komp_main.iml rename to komp.iml index 91f55dd..a88fd57 100644 --- a/.idea/modules/komp_main.iml +++ b/komp.iml @@ -1,13 +1,13 @@ - + - + - - - + + + - - - - + + + + + + + + + + diff --git a/komp.ipr b/komp.ipr new file mode 100644 index 0000000..f46dbaf --- /dev/null +++ b/komp.ipr @@ -0,0 +1,186 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1.7 + + + + + + + + \ No newline at end of file diff --git a/src/main/kotlin/nl/astraeus/komp/DomDiff.kt b/src/main/kotlin/nl/astraeus/komp/DomDiff.kt new file mode 100644 index 0000000..eb94855 --- /dev/null +++ b/src/main/kotlin/nl/astraeus/komp/DomDiff.kt @@ -0,0 +1,65 @@ +package nl.astraeus.komp + +import org.w3c.dom.HTMLElement +import org.w3c.dom.get + +/** + * User: rnentjes + * Date: 10-9-17 + * Time: 13:33 + */ + +object DomDiffer { + + fun replaceDiff(newElement: HTMLElement, oldElement: HTMLElement) { + if (!match(newElement, oldElement)) { + replaceNode(newElement, oldElement) + } else { + // think of the children! + for (index in 0 until newElement.childElementCount) { + val newChild = newElement.children[index] + val oldChild = oldElement.children[index] + + if (newChild is HTMLElement && oldChild is HTMLElement) { + replaceDiff(newChild, oldChild) + } + } + } + } + + private fun replaceNode(newElement: HTMLElement, oldElement: HTMLElement) { + val parent = oldElement.parentElement + + 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 + } + +} diff --git a/src/main/kotlin/nl/astraeus/komp/Komponent.kt b/src/main/kotlin/nl/astraeus/komp/Komponent.kt index 332325c..4692447 100644 --- a/src/main/kotlin/nl/astraeus/komp/Komponent.kt +++ b/src/main/kotlin/nl/astraeus/komp/Komponent.kt @@ -114,6 +114,7 @@ abstract class Komponent { val newElement = it.create() parent?.replaceChild(newElement, element) + //DomDiffer.replaceDiff(newElement, element) } } }