Compare commits
17 Commits
1813e5b33f
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
| 88cf5b8533 | |||
| d3fef98203 | |||
| 191b23ed51 | |||
| 66b4633e6b | |||
| 177d96975a | |||
| e97b6966ba | |||
| 283c19defb | |||
| 0d2f2146e9 | |||
| 497ca14c27 | |||
| a5e7963412 | |||
| 6f37c879c2 | |||
| ab5689133f | |||
| 9e08601bb7 | |||
| c68a024552 | |||
| 46bb07d567 | |||
| a1021e5cda | |||
| a5b938aa27 |
4
.github/workflows/gradle.yml
vendored
4
.github/workflows/gradle.yml
vendored
@@ -9,9 +9,9 @@ jobs:
|
|||||||
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v1
|
- uses: actions/checkout@v1
|
||||||
- name: Set up JDK 1.8
|
- name: Set up JDK 11
|
||||||
uses: actions/setup-java@v1
|
uses: actions/setup-java@v1
|
||||||
with:
|
with:
|
||||||
java-version: 1.8
|
java-version: 11
|
||||||
- name: Build with Gradle
|
- name: Build with Gradle
|
||||||
run: ./gradlew build
|
run: ./gradlew build
|
||||||
|
|||||||
144
.junie/guidelines.md
Normal file
144
.junie/guidelines.md
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
# Kotlin Komponent (Komp) Development Guidelines
|
||||||
|
|
||||||
|
This document provides specific information for developing with the Kotlin Komponent (Komp) library, a component-based UI library for Kotlin/JS.
|
||||||
|
|
||||||
|
## Build/Configuration Instructions
|
||||||
|
|
||||||
|
### Prerequisites
|
||||||
|
- Kotlin 2.1.10 or higher
|
||||||
|
- Gradle 7.0 or higher
|
||||||
|
|
||||||
|
### Building the Project
|
||||||
|
|
||||||
|
The project uses Kotlin Multiplatform with a focus on JavaScript (and potentially WebAssembly in the future).
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Build the project
|
||||||
|
./gradlew build
|
||||||
|
|
||||||
|
# Build only JS target
|
||||||
|
./gradlew jsJar
|
||||||
|
|
||||||
|
# Publish to Maven Local for local development
|
||||||
|
./gradlew publishToMavenLocal
|
||||||
|
```
|
||||||
|
|
||||||
|
### Configuration
|
||||||
|
|
||||||
|
The project uses the following Gradle plugins:
|
||||||
|
- Kotlin Multiplatform
|
||||||
|
- Maven Publish
|
||||||
|
- Dokka for documentation
|
||||||
|
|
||||||
|
Key configuration files:
|
||||||
|
- `build.gradle.kts` - Main build configuration
|
||||||
|
- `gradle.properties` - Contains publishing credentials and signing configuration
|
||||||
|
- `settings.gradle.kts` - Project settings and repository configuration
|
||||||
|
|
||||||
|
## Testing Information
|
||||||
|
|
||||||
|
### Running Tests
|
||||||
|
|
||||||
|
Tests are written using the Kotlin Test library and run with Karma using Chrome Headless.
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# Run all tests
|
||||||
|
./gradlew jsTest
|
||||||
|
|
||||||
|
# Run browser tests
|
||||||
|
./gradlew jsBrowserTest
|
||||||
|
```
|
||||||
|
|
||||||
|
### Test Structure
|
||||||
|
|
||||||
|
Tests are located in the `src/jsTest` directory. The project uses the standard Kotlin Test library with annotations:
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
@Test
|
||||||
|
fun testSomething() {
|
||||||
|
// Test code here
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
### Writing New Tests
|
||||||
|
|
||||||
|
When writing tests for Komponents:
|
||||||
|
|
||||||
|
1. Create a test class in the `src/jsTest/kotlin/nl/astraeus/komp` directory
|
||||||
|
2. Use the `@Test` annotation for test methods
|
||||||
|
3. For UI component tests:
|
||||||
|
- Create a test DOM element using `document.createElement("div")`
|
||||||
|
- Create your Komponent instance
|
||||||
|
- Render it using `Komponent.create(element, komponent)`
|
||||||
|
- Modify state and call `requestImmediateUpdate()` to test updates
|
||||||
|
- Verify the DOM structure using assertions
|
||||||
|
|
||||||
|
### Example Test
|
||||||
|
|
||||||
|
Here's a simple test example:
|
||||||
|
|
||||||
|
```kotlin
|
||||||
|
@Test
|
||||||
|
fun testSimpleComponent() {
|
||||||
|
// Create a test component
|
||||||
|
val component = SimpleKomponent()
|
||||||
|
val div = document.createElement("div") as HTMLDivElement
|
||||||
|
|
||||||
|
// Render it
|
||||||
|
Komponent.create(div, component)
|
||||||
|
|
||||||
|
// Verify initial state
|
||||||
|
assertEquals("Hello", div.querySelector("div")?.textContent)
|
||||||
|
|
||||||
|
// Update state and re-render
|
||||||
|
component.hello = false
|
||||||
|
component.requestImmediateUpdate()
|
||||||
|
|
||||||
|
// Verify updated state
|
||||||
|
assertEquals("Good bye", div.querySelector("span")?.textContent)
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
## Additional Development Information
|
||||||
|
|
||||||
|
### Project Structure
|
||||||
|
|
||||||
|
- `src/commonMain` - Common Kotlin code
|
||||||
|
- `src/jsMain` - JavaScript-specific implementation
|
||||||
|
- `src/wasmJsMain` - WebAssembly JavaScript implementation (experimental)
|
||||||
|
- `src/jsTest` - JavaScript tests
|
||||||
|
|
||||||
|
### Key Components
|
||||||
|
|
||||||
|
1. **Komponent** - Base class for all UI components
|
||||||
|
- Override `HtmlBuilder.render()` to define the component's UI
|
||||||
|
- Use `requestUpdate()` for scheduled updates
|
||||||
|
- Use `requestImmediateUpdate()` for immediate updates
|
||||||
|
- Override `generateMemoizeHash()` to optimize re-renders
|
||||||
|
|
||||||
|
2. **HtmlBuilder** - Handles DOM creation and updates
|
||||||
|
- Uses a virtual DOM-like approach to update only what changed
|
||||||
|
- Supports including child components with `include()`
|
||||||
|
- Handles attribute and event binding
|
||||||
|
|
||||||
|
3. **ElementExtensions** - Utility functions for DOM manipulation
|
||||||
|
|
||||||
|
### Optimization Features
|
||||||
|
|
||||||
|
- **Memoization**: Components can implement `generateMemoizeHash()` to avoid unnecessary re-renders
|
||||||
|
- **Batched Updates**: Multiple `requestUpdate()` calls are batched for performance
|
||||||
|
- **Efficient DOM Updates**: Only changed elements are updated in the DOM
|
||||||
|
|
||||||
|
### Error Handling
|
||||||
|
|
||||||
|
The library provides detailed error information when rendering fails:
|
||||||
|
- Set custom error handlers with `Komponent.setErrorHandler()`
|
||||||
|
- Enable debug logging with `Komponent.logRenderEvent = true`
|
||||||
|
- Use `debug {}` blocks in render functions for additional validation
|
||||||
|
|
||||||
|
### Code Style
|
||||||
|
|
||||||
|
- Follow Kotlin's official code style (`kotlin.code.style=official`)
|
||||||
|
- Use functional programming patterns where appropriate
|
||||||
|
- Prefer immutable state when possible
|
||||||
|
- Use descriptive names for components and methods
|
||||||
207
build.gradle.kts
207
build.gradle.kts
@@ -1,190 +1,139 @@
|
|||||||
import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
|
@file:OptIn(ExperimentalWasmDsl::class)
|
||||||
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl
|
|
||||||
|
import com.vanniktech.maven.publish.SonatypeHost
|
||||||
|
import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
|
||||||
|
|
||||||
plugins {
|
plugins {
|
||||||
kotlin("multiplatform") version "2.0.0"
|
kotlin("multiplatform") version "2.1.10"
|
||||||
id("maven-publish")
|
signing
|
||||||
id("signing")
|
id("org.jetbrains.dokka") version "2.0.0"
|
||||||
id("org.jetbrains.dokka") version "1.5.31"
|
id("com.vanniktech.maven.publish") version "0.31.0"
|
||||||
}
|
}
|
||||||
|
|
||||||
group = "nl.astraeus"
|
group = "nl.astraeus"
|
||||||
version = "1.2.3-SNAPSHOT"
|
version = "1.2.10"
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
mavenCentral()
|
mavenCentral()
|
||||||
|
maven {
|
||||||
|
name = "Sonatype Releases"
|
||||||
|
url = uri("https://central.sonatype.com/api/v1/publisher/deployments/download/")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
tasks.withType<Test>(Test::class.java) {
|
||||||
|
useJUnitPlatform()
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
kotlin {
|
kotlin {
|
||||||
js {
|
js {
|
||||||
browser {
|
browser {
|
||||||
testTask {
|
testTask {
|
||||||
useKarma {
|
useKarma {
|
||||||
useChromiumHeadless()
|
useChromeHeadless()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* @OptIn(ExperimentalWasmDsl::class)
|
/* wasmJs {
|
||||||
wasmJs {
|
//moduleName = project.name
|
||||||
//moduleName = project.name
|
browser()
|
||||||
browser()
|
|
||||||
|
|
||||||
mavenPublication {
|
mavenPublication {
|
||||||
groupId = group as String
|
groupId = group as String
|
||||||
pom { name = "${project.name}-wasm-js" }
|
pom { name = "${project.name}-wasm-js" }
|
||||||
}
|
}
|
||||||
}*/
|
}*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@OptIn(ExperimentalKotlinGradlePluginApi::class)
|
@OptIn(ExperimentalKotlinGradlePluginApi::class)
|
||||||
applyDefaultHierarchyTemplate {
|
applyDefaultHierarchyTemplate {
|
||||||
common {
|
common {
|
||||||
group("jsCommon") {
|
group("jsCommon") {
|
||||||
withJs()
|
withJs()
|
||||||
// TODO: switch to `withWasmJs()` after upgrade to Kotlin 2.0
|
// TODO: switch to `withWasmJs()` after upgrade to Kotlin 2.0
|
||||||
withWasm()
|
withWasm()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
*/
|
||||||
*/
|
|
||||||
|
|
||||||
sourceSets {
|
sourceSets {
|
||||||
val commonMain by getting {
|
val commonMain by getting {
|
||||||
dependencies {
|
dependencies {
|
||||||
api("org.jetbrains.kotlinx:kotlinx-html:0.11.0")
|
api("org.jetbrains.kotlinx:kotlinx-html:0.12.0")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val commonTest by getting {
|
||||||
|
dependencies {
|
||||||
|
implementation(kotlin("test"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val jsMain by getting
|
val jsMain by getting
|
||||||
val jsTest by getting {
|
val jsTest by getting {
|
||||||
dependencies {
|
dependencies {
|
||||||
implementation(kotlin("test-js"))
|
implementation(kotlin("test"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
//val wasmJsMain by getting
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extra["PUBLISH_GROUP_ID"] = group
|
|
||||||
extra["PUBLISH_VERSION"] = version
|
|
||||||
extra["PUBLISH_ARTIFACT_ID"] = name
|
|
||||||
|
|
||||||
// Stub secrets to let the project sync and build without the publication values set up
|
|
||||||
val signingKeyId: String? by project
|
|
||||||
val signingPassword: String? by project
|
|
||||||
val signingSecretKeyRingFile: String? by project
|
|
||||||
val ossrhUsername: String? by project
|
|
||||||
val ossrhPassword: String? by project
|
|
||||||
|
|
||||||
extra["signing.keyId"] = signingKeyId
|
|
||||||
extra["signing.password"] = signingPassword
|
|
||||||
extra["signing.secretKeyRingFile"] = signingSecretKeyRingFile
|
|
||||||
extra["ossrhUsername"] = ossrhUsername
|
|
||||||
extra["ossrhPassword"] = ossrhPassword
|
|
||||||
|
|
||||||
val javadocJar by tasks.registering(Jar::class) {
|
|
||||||
archiveClassifier.set("javadoc")
|
|
||||||
}
|
|
||||||
|
|
||||||
publishing {
|
publishing {
|
||||||
repositories {
|
repositories {
|
||||||
mavenLocal()
|
mavenLocal()
|
||||||
maven {
|
|
||||||
name = "releases"
|
|
||||||
// change to point to your repo, e.g. http://my.org/repo
|
|
||||||
setUrl("https://reposilite.astraeus.nl/releases")
|
|
||||||
credentials {
|
|
||||||
val reposiliteUsername: String? by project
|
|
||||||
val reposilitePassword: String? by project
|
|
||||||
|
|
||||||
username = reposiliteUsername
|
|
||||||
password = reposilitePassword
|
|
||||||
}
|
|
||||||
}
|
|
||||||
maven {
|
|
||||||
name = "snapshots"
|
|
||||||
// change to point to your repo, e.g. http://my.org/repo
|
|
||||||
setUrl("https://reposilite.astraeus.nl/snapshots")
|
|
||||||
credentials {
|
|
||||||
val reposiliteUsername: String? by project
|
|
||||||
val reposilitePassword: String? by project
|
|
||||||
|
|
||||||
username = reposiliteUsername
|
|
||||||
password = reposilitePassword
|
|
||||||
}
|
|
||||||
}
|
|
||||||
maven {
|
|
||||||
name = "sonatype"
|
|
||||||
setUrl("https://s01.oss.sonatype.org/service/local/staging/deploy/maven2")
|
|
||||||
credentials {
|
|
||||||
username = ossrhUsername
|
|
||||||
password = ossrhPassword
|
|
||||||
}
|
|
||||||
}
|
|
||||||
maven {
|
maven {
|
||||||
name = "gitea"
|
name = "gitea"
|
||||||
setUrl("https://gitea.astraeus.nl/api/packages/rnentjes/maven")
|
setUrl("https://gitea.astraeus.nl/api/packages/rnentjes/maven")
|
||||||
|
|
||||||
credentials() {
|
credentials {
|
||||||
val giteaUsername: kotlin.String? by project
|
val giteaUsername: String? by project
|
||||||
val giteaPassword: kotlin.String? by project
|
val giteaPassword: String? by project
|
||||||
|
|
||||||
username = giteaUsername
|
username = giteaUsername
|
||||||
password = giteaPassword
|
password = giteaPassword
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Configure all publications
|
tasks.withType<AbstractPublishToMaven> {
|
||||||
publications.withType<MavenPublication> {
|
dependsOn(tasks.withType<Sign>())
|
||||||
// Stub javadoc.jar artifact
|
|
||||||
artifact(javadocJar.get())
|
|
||||||
|
|
||||||
// Provide artifacts information requited by Maven Central
|
|
||||||
pom {
|
|
||||||
name.set("kotlin-komponent")
|
|
||||||
description.set("Kotlin komponent")
|
|
||||||
url.set("https://github.com/rnentjes/komponent")
|
|
||||||
|
|
||||||
licenses {
|
|
||||||
license {
|
|
||||||
name.set("MIT")
|
|
||||||
url.set("https://opensource.org/licenses/MIT")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
developers {
|
|
||||||
developer {
|
|
||||||
id.set("rnentjes")
|
|
||||||
name.set("Rien Nentjes")
|
|
||||||
email.set("info@nentjes.com")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
scm {
|
|
||||||
url.set("https://github.com/rnentjes/komponent")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
signing {
|
signing {
|
||||||
sign(publishing.publications)
|
sign(publishing.publications)
|
||||||
}
|
}
|
||||||
|
|
||||||
tasks.named<Task>("signJsPublication") {
|
mavenPublishing {
|
||||||
dependsOn(tasks.named<Task>("publishKotlinMultiplatformPublicationToMavenLocal"))
|
publishToMavenCentral(SonatypeHost.CENTRAL_PORTAL)
|
||||||
}
|
|
||||||
|
|
||||||
tasks.named<Task>("publishJsPublicationToReleasesRepository") {
|
signAllPublications()
|
||||||
dependsOn(tasks.named<Task>("signKotlinMultiplatformPublication"))
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.named<Task>("publishKotlinMultiplatformPublicationToMavenLocalRepository") {
|
coordinates(group.toString(), name, version.toString())
|
||||||
dependsOn(tasks.named<Task>("signJsPublication"))
|
|
||||||
}
|
|
||||||
|
|
||||||
tasks.named<Task>("publishKotlinMultiplatformPublicationToReleasesRepository") {
|
pom {
|
||||||
dependsOn(tasks.named<Task>("signJsPublication"))
|
name = "kotlin-komponent"
|
||||||
}
|
description = "Kotlin komponent"
|
||||||
|
inceptionYear = "2017"
|
||||||
tasks.named<Task>("publishKotlinMultiplatformPublicationToSonatypeRepository") {
|
url = "https://github.com/rnentjes/komponent"
|
||||||
dependsOn(tasks.named<Task>("signJsPublication"))
|
licenses {
|
||||||
|
license {
|
||||||
|
name = "MIT"
|
||||||
|
url = "https://opensource.org/licenses/MIT"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
developers {
|
||||||
|
developer {
|
||||||
|
id = "rnentjes"
|
||||||
|
name = "Rien Nentjes"
|
||||||
|
email = "info@nentjes.com"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
scm {
|
||||||
|
url = "https://github.com/rnentjes/komponent"
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
2
gradle/wrapper/gradle-wrapper.properties
vendored
2
gradle/wrapper/gradle-wrapper.properties
vendored
@@ -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-8.4-all.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-8.5-all.zip
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
|
|||||||
@@ -8,6 +8,6 @@ See the komp-todo repository for a basic example here: [komp-todo](https://githu
|
|||||||
|
|
||||||
For a more complete example take a look at the simple-password-manager repository: [simple-password-manager](https://github.com/rnentjes/simple-password-manager)
|
For a more complete example take a look at the simple-password-manager repository: [simple-password-manager](https://github.com/rnentjes/simple-password-manager)
|
||||||
|
|
||||||
Available on maven central: "nl.astraeus:kotlin-komponent-js:1.0.0"
|
Available on maven central: "nl.astraeus:kotlin-komponent-js:1.2.7"
|
||||||
|
|
||||||
Some getting started documentation can be found [here](docs/getting-started.md)
|
Some getting started documentation can be found [here](docs/getting-started.md)
|
||||||
|
|||||||
@@ -1,2 +1,17 @@
|
|||||||
|
|
||||||
|
pluginManagement {
|
||||||
|
repositories {
|
||||||
|
google()
|
||||||
|
mavenCentral()
|
||||||
|
gradlePluginPortal()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencyResolutionManagement {
|
||||||
|
repositories {
|
||||||
|
google()
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
rootProject.name = "kotlin-komponent"
|
rootProject.name = "kotlin-komponent"
|
||||||
|
|||||||
@@ -1,9 +1,8 @@
|
|||||||
package nl.astraeus.komp
|
package nl.astraeus.komp
|
||||||
|
|
||||||
import org.w3c.dom.events.Event
|
|
||||||
import org.w3c.dom.Element
|
import org.w3c.dom.Element
|
||||||
import org.w3c.dom.HTMLInputElement
|
import org.w3c.dom.HTMLInputElement
|
||||||
import org.w3c.dom.events.EventListener
|
import org.w3c.dom.events.Event
|
||||||
import org.w3c.dom.get
|
import org.w3c.dom.get
|
||||||
|
|
||||||
private fun Int.asSpaces(): String {
|
private fun Int.asSpaces(): String {
|
||||||
@@ -41,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) {
|
||||||
@@ -66,59 +57,52 @@ 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(attributeName: String, value: String) {
|
||||||
//val attributeName = name.lowercase()
|
if (this is HTMLInputElement) {
|
||||||
if (value == null || value.isBlank()) {
|
when (attributeName) {
|
||||||
if (this is HTMLInputElement) {
|
"checked" -> {
|
||||||
when (attributeName) {
|
checked = "checked" == value
|
||||||
"checked" -> {
|
|
||||||
checked = false
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
"class" -> {
|
|
||||||
className = ""
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
"value" -> {
|
|
||||||
this.value = ""
|
|
||||||
}
|
|
||||||
else -> {
|
|
||||||
removeAttribute(attributeName)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
removeAttribute(attributeName)
|
"class" -> {
|
||||||
}
|
className = value
|
||||||
} else {
|
}
|
||||||
if (this is HTMLInputElement) {
|
|
||||||
when (attributeName) {
|
"value" -> {
|
||||||
"checked" -> {
|
this.value = value
|
||||||
checked = "checked" == value
|
}
|
||||||
}
|
|
||||||
/*
|
else -> {
|
||||||
"class" -> {
|
setAttribute(attributeName, value)
|
||||||
className = value
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
"value" -> {
|
|
||||||
this.value = value
|
|
||||||
}
|
|
||||||
else -> {
|
|
||||||
setAttribute(attributeName, value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else if (this.getAttribute(attributeName) != value) {
|
|
||||||
setAttribute(attributeName, value)
|
|
||||||
}
|
}
|
||||||
|
} else if (this.getAttribute(attributeName) != value) {
|
||||||
|
setAttribute(attributeName, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun Element.clearKompEvents() {
|
internal fun Element.clearKompAttribute(attributeName: String) {
|
||||||
val events = getKompEvents()
|
if (this is HTMLInputElement) {
|
||||||
for ((name, event) in getKompEvents()) {
|
when (attributeName) {
|
||||||
removeEventListener(name, event)
|
"checked" -> {
|
||||||
|
checked = false
|
||||||
|
}
|
||||||
|
|
||||||
|
"class" -> {
|
||||||
|
className = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
"value" -> {
|
||||||
|
this.value = ""
|
||||||
|
}
|
||||||
|
|
||||||
|
else -> {
|
||||||
|
removeAttribute(attributeName)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
removeAttribute(attributeName)
|
||||||
}
|
}
|
||||||
events.clear()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal fun Element.setKompEvent(name: String, event: (Event) -> Unit) {
|
internal fun Element.setKompEvent(name: String, event: (Event) -> Unit) {
|
||||||
@@ -128,22 +112,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) {
|
||||||
|
|||||||
@@ -6,7 +6,6 @@ import org.w3c.dom.get
|
|||||||
data class ElementIndex(
|
data class ElementIndex(
|
||||||
val parent: Node,
|
val parent: Node,
|
||||||
var childIndex: Int,
|
var childIndex: Int,
|
||||||
var setAttr: MutableSet<String> = mutableSetOf()
|
|
||||||
) {
|
) {
|
||||||
override fun toString(): String {
|
override fun toString(): String {
|
||||||
return "${parent.nodeName}[$childIndex]"
|
return "${parent.nodeName}[$childIndex]"
|
||||||
@@ -39,7 +38,6 @@ fun ArrayList<ElementIndex>.currentPosition(): ElementIndex? {
|
|||||||
|
|
||||||
fun ArrayList<ElementIndex>.nextElement() {
|
fun ArrayList<ElementIndex>.nextElement() {
|
||||||
this.lastOrNull()?.let {
|
this.lastOrNull()?.let {
|
||||||
it.setAttr.clear()
|
|
||||||
it.childIndex++
|
it.childIndex++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ interface HtmlConsumer : TagConsumer<Element> {
|
|||||||
fun FlowOrMetaDataOrPhrasingContent.currentElement(): Element =
|
fun FlowOrMetaDataOrPhrasingContent.currentElement(): Element =
|
||||||
currentElement ?: error("No current element defined!")
|
currentElement ?: error("No current element defined!")
|
||||||
|
|
||||||
private fun Node.asElement() = this as? HTMLElement
|
private fun Node?.asElement() = this as? HTMLElement
|
||||||
|
|
||||||
class HtmlBuilder(
|
class HtmlBuilder(
|
||||||
private val komponent: Komponent?,
|
private val komponent: Komponent?,
|
||||||
@@ -57,12 +57,6 @@ class HtmlBuilder(
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// current element should become parent
|
// current element should become parent
|
||||||
/*
|
|
||||||
val ce = komponent.element
|
|
||||||
if (ce != null) {
|
|
||||||
append(ce as Element)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
komponent.create(
|
komponent.create(
|
||||||
currentPosition.last().parent as Element,
|
currentPosition.last().parent as Element,
|
||||||
currentPosition.last().childIndex
|
currentPosition.last().childIndex
|
||||||
@@ -100,39 +94,26 @@ class HtmlBuilder(
|
|||||||
"onTagStart, [${tag.tagName}, ${tag.namespace ?: ""}], currentPosition: $currentPosition"
|
"onTagStart, [${tag.tagName}, ${tag.namespace ?: ""}], currentPosition: $currentPosition"
|
||||||
}
|
}
|
||||||
|
|
||||||
currentNode = currentPosition.currentElement()
|
currentNode = if (tag.namespace != null) {
|
||||||
|
document.createElementNS(tag.namespace, tag.tagName)
|
||||||
|
} else {
|
||||||
|
document.createElement(tag.tagName)
|
||||||
|
}
|
||||||
|
|
||||||
if (currentNode == null) {
|
if (currentNode == null) {
|
||||||
logReplace { "onTagStart, currentNode1: $currentNode" }
|
logReplace { "onTagStart, currentNode1: $currentNode" }
|
||||||
currentNode = if (tag.namespace != null) {
|
|
||||||
document.createElementNS(tag.namespace, tag.tagName)
|
|
||||||
} else {
|
|
||||||
document.createElement(tag.tagName)
|
|
||||||
}
|
|
||||||
|
|
||||||
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}"
|
||||||
tag.namespace != null &&
|
}
|
||||||
!currentNode?.asElement()?.namespaceURI.equals(tag.namespace, true)
|
logReplace {
|
||||||
)
|
"onTagStart, currentElement, replace: ${currentNode?.asElement()?.tagName} -> ${tag.tagName}"
|
||||||
) {
|
}
|
||||||
logReplace {
|
|
||||||
"onTagStart, currentElement, namespace: ${currentNode?.asElement()?.namespaceURI} -> ${tag.namespace}"
|
|
||||||
}
|
|
||||||
logReplace {
|
|
||||||
"onTagStart, currentElement, replace: ${currentNode?.asElement()?.tagName} -> ${tag.tagName}"
|
|
||||||
}
|
|
||||||
|
|
||||||
currentNode = if (tag.namespace != null) {
|
currentPosition.replace(currentNode!!)
|
||||||
document.createElementNS(tag.namespace, tag.tagName)
|
|
||||||
} else {
|
|
||||||
document.createElement(tag.tagName)
|
|
||||||
}
|
|
||||||
|
|
||||||
currentPosition.replace(currentNode!!)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
currentElement = currentNode as? Element ?: currentElement
|
currentElement = currentNode as? Element ?: currentElement
|
||||||
@@ -144,15 +125,8 @@ class HtmlBuilder(
|
|||||||
firstTag = false
|
firstTag = false
|
||||||
}
|
}
|
||||||
|
|
||||||
currentElement?.clearKompEvents()
|
|
||||||
|
|
||||||
// if currentElement = checkbox make sure it's cleared
|
|
||||||
(currentElement as? HTMLInputElement)?.checked = false
|
|
||||||
|
|
||||||
currentPosition.lastOrNull()?.setAttr?.clear()
|
|
||||||
for (entry in tag.attributesEntries) {
|
for (entry in tag.attributesEntries) {
|
||||||
currentElement!!.setKompAttribute(entry.key, entry.value)
|
currentElement?.setKompAttribute(entry.key, entry.value)
|
||||||
currentPosition.lastOrNull()?.setAttr?.add(entry.key)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -181,11 +155,10 @@ class HtmlBuilder(
|
|||||||
checkTag("onTagAttributeChange", tag)
|
checkTag("onTagAttributeChange", tag)
|
||||||
}
|
}
|
||||||
|
|
||||||
currentElement?.setKompAttribute(attribute, value)
|
|
||||||
if (value == null || value.isEmpty()) {
|
if (value == null || value.isEmpty()) {
|
||||||
currentPosition.currentPosition()?.setAttr?.remove(attribute)
|
currentElement?.clearKompAttribute(attribute)
|
||||||
} else {
|
} else {
|
||||||
currentPosition.currentPosition()?.setAttr?.add(attribute)
|
currentElement?.setKompAttribute(attribute, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -200,7 +173,7 @@ class HtmlBuilder(
|
|||||||
checkTag("onTagEvent", tag)
|
checkTag("onTagEvent", tag)
|
||||||
}
|
}
|
||||||
|
|
||||||
currentElement?.setKompEvent(event.lowercase(), value.asDynamic())
|
currentElement?.setKompEvent(event.lowercase(), value)
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onTagEnd(tag: Tag) {
|
override fun onTagEnd(tag: Tag) {
|
||||||
@@ -222,28 +195,6 @@ class HtmlBuilder(
|
|||||||
checkTag("onTagEnd", tag)
|
checkTag("onTagEnd", 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()
|
currentNode = currentPosition.currentElement()
|
||||||
@@ -327,9 +278,7 @@ class HtmlBuilder(
|
|||||||
namespace == "http://www.w3.org/2000/svg"
|
namespace == "http://www.w3.org/2000/svg"
|
||||||
)
|
)
|
||||||
) {
|
) {
|
||||||
if (currentElement?.innerHTML != textContent) {
|
currentElement?.innerHTML += textContent.trim()
|
||||||
currentElement?.innerHTML += textContent
|
|
||||||
}
|
|
||||||
} else if (currentElement?.textContent != textContent) {
|
} else if (currentElement?.textContent != textContent) {
|
||||||
currentElement?.textContent = textContent
|
currentElement?.textContent = textContent
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -191,6 +191,7 @@ abstract class Komponent {
|
|||||||
private var scheduledForUpdate = mutableSetOf<Komponent>()
|
private var scheduledForUpdate = mutableSetOf<Komponent>()
|
||||||
private var interceptor: (Komponent, () -> Unit) -> Unit = { _, block -> block() }
|
private var interceptor: (Komponent, () -> Unit) -> Unit = { _, block -> block() }
|
||||||
|
|
||||||
|
var logUpdateEvent = false
|
||||||
var logRenderEvent = false
|
var logRenderEvent = false
|
||||||
var logReplaceEvent = false
|
var logReplaceEvent = false
|
||||||
var enableAssertions = false
|
var enableAssertions = false
|
||||||
@@ -214,7 +215,7 @@ abstract class Komponent {
|
|||||||
scheduledForUpdate.add(komponent)
|
scheduledForUpdate.add(komponent)
|
||||||
|
|
||||||
if (updateCallback == null) {
|
if (updateCallback == null) {
|
||||||
window.setTimeout({
|
updateCallback = window.setTimeout({
|
||||||
runUpdate()
|
runUpdate()
|
||||||
}, 0)
|
}, 0)
|
||||||
}
|
}
|
||||||
@@ -244,6 +245,9 @@ abstract class Komponent {
|
|||||||
val memoizeHash = next.generateMemoizeHash()
|
val memoizeHash = next.generateMemoizeHash()
|
||||||
|
|
||||||
if (next.memoizeChanged()) {
|
if (next.memoizeChanged()) {
|
||||||
|
if (logUpdateEvent) {
|
||||||
|
console.log("Rendering", next)
|
||||||
|
}
|
||||||
next.onBeforeUpdate()
|
next.onBeforeUpdate()
|
||||||
next.renderUpdate()
|
next.renderUpdate()
|
||||||
next.updateMemoizeHash()
|
next.updateMemoizeHash()
|
||||||
|
|||||||
73
src/jsTest/kotlin/nl/astraeus/komp/TestClassUpdate.kt
Normal file
73
src/jsTest/kotlin/nl/astraeus/komp/TestClassUpdate.kt
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
package nl.astraeus.komp
|
||||||
|
|
||||||
|
import kotlinx.browser.document
|
||||||
|
import kotlinx.html.div
|
||||||
|
import kotlinx.html.classes
|
||||||
|
import org.w3c.dom.HTMLDivElement
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.assertFalse
|
||||||
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class for verifying class attribute updates and removals
|
||||||
|
*/
|
||||||
|
class ClassKomponent : Komponent() {
|
||||||
|
var includeClass = true
|
||||||
|
var className = "test-class"
|
||||||
|
|
||||||
|
override fun HtmlBuilder.render() {
|
||||||
|
div {
|
||||||
|
if (includeClass) {
|
||||||
|
classes = setOf(className)
|
||||||
|
}
|
||||||
|
+"Content"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestClassUpdate {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testClassRemoval() {
|
||||||
|
// Create a test component
|
||||||
|
val classComponent = ClassKomponent()
|
||||||
|
val div = document.createElement("div") as HTMLDivElement
|
||||||
|
|
||||||
|
// Render it
|
||||||
|
Komponent.create(div, classComponent)
|
||||||
|
|
||||||
|
// Verify initial state - should have the class
|
||||||
|
var contentDiv = div.querySelector("div")
|
||||||
|
println("[DEBUG_LOG] Initial DOM: ${div.printTree()}")
|
||||||
|
assertTrue(contentDiv?.classList?.contains("test-class") ?: false, "Div should have the class initially")
|
||||||
|
|
||||||
|
// Update to remove the class
|
||||||
|
classComponent.includeClass = false
|
||||||
|
classComponent.requestImmediateUpdate()
|
||||||
|
|
||||||
|
// Verify the class was removed
|
||||||
|
contentDiv = div.querySelector("div")
|
||||||
|
println("[DEBUG_LOG] After class removal: ${div.printTree()}")
|
||||||
|
assertFalse(contentDiv?.classList?.contains("test-class") ?: true, "Class should be removed after update")
|
||||||
|
|
||||||
|
// Add the class back
|
||||||
|
classComponent.includeClass = true
|
||||||
|
classComponent.requestImmediateUpdate()
|
||||||
|
|
||||||
|
// Verify the class was added back
|
||||||
|
contentDiv = div.querySelector("div")
|
||||||
|
println("[DEBUG_LOG] After class added back: ${div.printTree()}")
|
||||||
|
assertTrue(contentDiv?.classList?.contains("test-class") ?: false, "Class should be added back")
|
||||||
|
|
||||||
|
// Change the class name
|
||||||
|
classComponent.className = "new-class"
|
||||||
|
classComponent.requestImmediateUpdate()
|
||||||
|
|
||||||
|
// Verify the class was changed
|
||||||
|
contentDiv = div.querySelector("div")
|
||||||
|
println("[DEBUG_LOG] After class name change: ${div.printTree()}")
|
||||||
|
assertFalse(contentDiv?.classList?.contains("test-class") ?: true, "Old class should be removed")
|
||||||
|
assertTrue(contentDiv?.classList?.contains("new-class") ?: false, "New class should be added")
|
||||||
|
}
|
||||||
|
}
|
||||||
100
src/jsTest/kotlin/nl/astraeus/komp/TestInsert.kt
Normal file
100
src/jsTest/kotlin/nl/astraeus/komp/TestInsert.kt
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
package nl.astraeus.komp
|
||||||
|
|
||||||
|
import kotlinx.browser.document
|
||||||
|
import kotlinx.html.div
|
||||||
|
import kotlinx.html.table
|
||||||
|
import kotlinx.html.tbody
|
||||||
|
import kotlinx.html.td
|
||||||
|
import kotlinx.html.tr
|
||||||
|
import org.w3c.dom.HTMLDivElement
|
||||||
|
import org.w3c.dom.HTMLTableElement
|
||||||
|
import org.w3c.dom.HTMLTableRowElement
|
||||||
|
import org.w3c.dom.HTMLTableCellElement
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.assertNotNull
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Komponent that renders a table with rows
|
||||||
|
*/
|
||||||
|
class TableKomponent : Komponent() {
|
||||||
|
val rows = mutableListOf<RowKomponent>()
|
||||||
|
|
||||||
|
fun addRow(text: String) {
|
||||||
|
rows.add(RowKomponent(text))
|
||||||
|
requestImmediateUpdate()
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun HtmlBuilder.render() {
|
||||||
|
div {
|
||||||
|
table {
|
||||||
|
tbody {
|
||||||
|
for (row in rows) {
|
||||||
|
include(row)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A Komponent that represents a single row in a table
|
||||||
|
*/
|
||||||
|
class RowKomponent(val text: String) : Komponent() {
|
||||||
|
override fun generateMemoizeHash(): Int = text.hashCode()
|
||||||
|
|
||||||
|
override fun HtmlBuilder.render() {
|
||||||
|
tr {
|
||||||
|
td {
|
||||||
|
+text
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class for inserting rows in the DOM with a Komponent
|
||||||
|
*/
|
||||||
|
class TestInsert {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testInsertRow() {
|
||||||
|
// Create a test component
|
||||||
|
val tableComponent = TableKomponent()
|
||||||
|
val div = document.createElement("div") as HTMLDivElement
|
||||||
|
|
||||||
|
// Render it
|
||||||
|
Komponent.create(div, tableComponent)
|
||||||
|
|
||||||
|
// Verify initial state - should be an empty table
|
||||||
|
val table = div.querySelector("table")
|
||||||
|
assertNotNull(table, "Table should be rendered")
|
||||||
|
val initialRows = table.querySelectorAll("tr")
|
||||||
|
assertEquals(0, initialRows.length, "Table should initially have no rows")
|
||||||
|
|
||||||
|
// Add a row and verify it was inserted
|
||||||
|
tableComponent.addRow("First Row")
|
||||||
|
|
||||||
|
// Verify the row was added
|
||||||
|
val rowsAfterFirstInsert = div.querySelector("table")?.querySelectorAll("tr")
|
||||||
|
assertEquals(1, rowsAfterFirstInsert?.length, "Table should have one row after insertion")
|
||||||
|
val firstRowCell = div.querySelector("table")?.querySelector("tr td")
|
||||||
|
assertNotNull(firstRowCell, "First row cell should exist")
|
||||||
|
assertEquals("First Row", firstRowCell.textContent, "Row content should match")
|
||||||
|
|
||||||
|
// Add another row and verify it was inserted
|
||||||
|
tableComponent.addRow("Second Row")
|
||||||
|
|
||||||
|
// Verify both rows are present
|
||||||
|
val rowsAfterSecondInsert = div.querySelector("table")?.querySelectorAll("tr")
|
||||||
|
assertEquals(2, rowsAfterSecondInsert?.length, "Table should have two rows after second insertion")
|
||||||
|
val allCells = div.querySelector("table")?.querySelectorAll("tr td")
|
||||||
|
assertEquals(2, allCells?.length, "Table should have two cells")
|
||||||
|
assertEquals("First Row", allCells?.item(0)?.textContent, "First row content should match")
|
||||||
|
assertEquals("Second Row", allCells?.item(1)?.textContent, "Second row content should match")
|
||||||
|
|
||||||
|
// Print the DOM tree for debugging
|
||||||
|
println("Table DOM: ${div.printTree()}")
|
||||||
|
}
|
||||||
|
}
|
||||||
79
src/jsTest/kotlin/nl/astraeus/komp/TestStyleUpdate.kt
Normal file
79
src/jsTest/kotlin/nl/astraeus/komp/TestStyleUpdate.kt
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
package nl.astraeus.komp
|
||||||
|
|
||||||
|
import kotlinx.browser.document
|
||||||
|
import kotlinx.html.div
|
||||||
|
import kotlinx.html.style
|
||||||
|
import org.w3c.dom.HTMLDivElement
|
||||||
|
import kotlin.test.Test
|
||||||
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.assertNull
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test class for verifying style attribute updates and removals
|
||||||
|
*/
|
||||||
|
class StyleKomponent : Komponent() {
|
||||||
|
var includeStyle = true
|
||||||
|
var styleValue = "color: red;"
|
||||||
|
|
||||||
|
override fun HtmlBuilder.render() {
|
||||||
|
div {
|
||||||
|
if (includeStyle) {
|
||||||
|
style = styleValue
|
||||||
|
}
|
||||||
|
+"Content"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestStyleUpdate {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testStyleRemoval() {
|
||||||
|
// Create a test component
|
||||||
|
val styleComponent = StyleKomponent()
|
||||||
|
val div = document.createElement("div") as HTMLDivElement
|
||||||
|
|
||||||
|
// Render it
|
||||||
|
Komponent.create(div, styleComponent)
|
||||||
|
|
||||||
|
// Verify initial state - should have the style
|
||||||
|
var contentDiv = div.querySelector("div")
|
||||||
|
println("[DEBUG_LOG] Initial DOM: ${div.printTree()}")
|
||||||
|
assertEquals(
|
||||||
|
"color: red;",
|
||||||
|
contentDiv?.getAttribute("style"),
|
||||||
|
"Div should have the style initially"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Update to remove the style
|
||||||
|
styleComponent.includeStyle = false
|
||||||
|
styleComponent.requestImmediateUpdate()
|
||||||
|
|
||||||
|
// Verify the style was removed
|
||||||
|
contentDiv = div.querySelector("div")
|
||||||
|
println("[DEBUG_LOG] After style removal: ${div.printTree()}")
|
||||||
|
assertNull(contentDiv?.getAttribute("style"), "Style should be removed after update")
|
||||||
|
|
||||||
|
// Add the style back
|
||||||
|
styleComponent.includeStyle = true
|
||||||
|
styleComponent.requestImmediateUpdate()
|
||||||
|
|
||||||
|
// Verify the style was added back
|
||||||
|
contentDiv = div.querySelector("div")
|
||||||
|
println("[DEBUG_LOG] After style added back: ${div.printTree()}")
|
||||||
|
assertEquals("color: red;", contentDiv?.getAttribute("style"), "Style should be added back")
|
||||||
|
|
||||||
|
// Change the style value
|
||||||
|
styleComponent.styleValue = "color: blue;"
|
||||||
|
styleComponent.requestImmediateUpdate()
|
||||||
|
|
||||||
|
// Verify the style was changed
|
||||||
|
contentDiv = div.querySelector("div")
|
||||||
|
println("[DEBUG_LOG] After style value change: ${div.printTree()}")
|
||||||
|
assertEquals(
|
||||||
|
"color: blue;",
|
||||||
|
contentDiv?.getAttribute("style"),
|
||||||
|
"Style should be updated to new value"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
50
src/jsTest/kotlin/nl/astraeus/komp/TestSvg.kt
Normal file
50
src/jsTest/kotlin/nl/astraeus/komp/TestSvg.kt
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package nl.astraeus.komp
|
||||||
|
|
||||||
|
import kotlinx.browser.document
|
||||||
|
import kotlinx.html.InputType
|
||||||
|
import kotlinx.html.classes
|
||||||
|
import kotlinx.html.div
|
||||||
|
import kotlinx.html.i
|
||||||
|
import kotlinx.html.id
|
||||||
|
import kotlinx.html.input
|
||||||
|
import kotlinx.html.js.onClickFunction
|
||||||
|
import kotlinx.html.p
|
||||||
|
import kotlinx.html.span
|
||||||
|
import kotlinx.html.svg
|
||||||
|
import kotlinx.html.table
|
||||||
|
import kotlinx.html.td
|
||||||
|
import kotlinx.html.tr
|
||||||
|
import kotlinx.html.unsafe
|
||||||
|
import org.w3c.dom.Element
|
||||||
|
import org.w3c.dom.HTMLDivElement
|
||||||
|
import kotlin.test.Test
|
||||||
|
|
||||||
|
class TestSvgKomponent : Komponent() {
|
||||||
|
override fun HtmlBuilder.render() {
|
||||||
|
div {
|
||||||
|
+"Test"
|
||||||
|
|
||||||
|
svg("my-class") {
|
||||||
|
classes += "added-class"
|
||||||
|
unsafe {
|
||||||
|
+"""arc(1,2)"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestSvg {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun testUpdateWithEmpty() {
|
||||||
|
val div = document.createElement("div") as HTMLDivElement
|
||||||
|
val rk = TestSvgKomponent()
|
||||||
|
|
||||||
|
Komponent.logRenderEvent = true
|
||||||
|
|
||||||
|
Komponent.create(div, rk)
|
||||||
|
|
||||||
|
println("SvgKomponent: ${div.printTree()}")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,6 @@
|
|||||||
package nl.astraeus.komp
|
package nl.astraeus.komp
|
||||||
|
|
||||||
import kotlinx.browser.document
|
import kotlinx.browser.document
|
||||||
import kotlinx.html.DIV
|
|
||||||
import kotlinx.html.InputType
|
import kotlinx.html.InputType
|
||||||
import kotlinx.html.classes
|
import kotlinx.html.classes
|
||||||
import kotlinx.html.div
|
import kotlinx.html.div
|
||||||
|
|||||||
Reference in New Issue
Block a user