From 191b23ed512293a6819b91d7adfea7d029716533 Mon Sep 17 00:00:00 2001 From: rnentjes Date: Thu, 12 Jun 2025 19:22:31 +0200 Subject: [PATCH] Bump version to 1.2.8, add `TestStyleUpdate` for verifying style attribute updates and removals, and simplify style attribute handling in `HtmlBuilder`. --- build.gradle.kts | 2 +- .../kotlin/nl/astraeus/komp/HtmlBuilder.kt | 5 +- .../nl/astraeus/komp/TestStyleUpdate.kt | 68 +++++++++++++++++++ 3 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 src/jsTest/kotlin/nl/astraeus/komp/TestStyleUpdate.kt diff --git a/build.gradle.kts b/build.gradle.kts index 4dd765a..5acdc44 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -11,7 +11,7 @@ plugins { } group = "nl.astraeus" -version = "1.2.7" +version = "1.2.8" repositories { mavenCentral() diff --git a/src/jsMain/kotlin/nl/astraeus/komp/HtmlBuilder.kt b/src/jsMain/kotlin/nl/astraeus/komp/HtmlBuilder.kt index 1e70289..16e9b66 100644 --- a/src/jsMain/kotlin/nl/astraeus/komp/HtmlBuilder.kt +++ b/src/jsMain/kotlin/nl/astraeus/komp/HtmlBuilder.kt @@ -233,10 +233,7 @@ class HtmlBuilder( if (attribute?.name != null) { val attr = attribute.name - if ( - !setAttrs.contains(attr) && - attr != "style" - ) { + if (!setAttrs.contains(attr)) { element.setKompAttribute(attr, null) } } diff --git a/src/jsTest/kotlin/nl/astraeus/komp/TestStyleUpdate.kt b/src/jsTest/kotlin/nl/astraeus/komp/TestStyleUpdate.kt new file mode 100644 index 0000000..2ab7c55 --- /dev/null +++ b/src/jsTest/kotlin/nl/astraeus/komp/TestStyleUpdate.kt @@ -0,0 +1,68 @@ +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 + val 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 + 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 + 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 + println("[DEBUG_LOG] After style value change: ${div.printTree()}") + assertEquals("color: blue;", contentDiv?.getAttribute("style"), "Style should be updated to new value") + } +} \ No newline at end of file