Browse Source

web: fix a bug when inline styles get empty after attributes update (#980)

Co-authored-by: Oleksandr Karpovich <oleksandr.karpovich@jetbrains.com>
pull/987/head
Oleksandr Karpovich 3 years ago committed by GitHub
parent
commit
6504196bc9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      web/core/src/jsMain/kotlin/org/jetbrains/compose/web/DomApplier.kt
  2. 24
      web/core/src/jsTest/kotlin/elements/AttributesTests.kt

7
web/core/src/jsMain/kotlin/org/jetbrains/compose/web/DomApplier.kt

@ -93,14 +93,17 @@ open class DomNodeWrapper(open val node: Node) {
class DomElementWrapper(override val node: HTMLElement): DomNodeWrapper(node) { class DomElementWrapper(override val node: HTMLElement): DomNodeWrapper(node) {
private var currentAttrs: Map<String, String>? = null
fun updateAttrs(attrs: Map<String, String>) { fun updateAttrs(attrs: Map<String, String>) {
while (node.attributes.length > 0) { currentAttrs?.forEach {
node.removeAttributeNode(node.attributes[0]!!) node.removeAttribute(it.key)
} }
attrs.forEach { attrs.forEach {
node.setAttribute(it.key, it.value) node.setAttribute(it.key, it.value)
} }
currentAttrs = attrs
} }
fun updateProperties(list: List<Pair<(Element, Any) -> Unit, Any>>) { fun updateProperties(list: List<Pair<(Element, Any) -> Unit, Any>>) {

24
web/core/src/jsTest/kotlin/elements/AttributesTests.kt

@ -6,6 +6,7 @@ import androidx.compose.runtime.setValue
import org.jetbrains.compose.web.attributes.AttrsBuilder import org.jetbrains.compose.web.attributes.AttrsBuilder
import org.jetbrains.compose.web.attributes.disabled import org.jetbrains.compose.web.attributes.disabled
import org.jetbrains.compose.web.attributes.forId import org.jetbrains.compose.web.attributes.forId
import org.jetbrains.compose.web.attributes.value
import org.jetbrains.compose.web.css.* import org.jetbrains.compose.web.css.*
import org.jetbrains.compose.web.dom.Button import org.jetbrains.compose.web.dom.Button
import org.jetbrains.compose.web.dom.Div import org.jetbrains.compose.web.dom.Div
@ -331,4 +332,27 @@ class AttributesTests {
assertEquals(2, attrsCallCounter) assertEquals(2, attrsCallCounter)
assertEquals(0, refDisposeCounter) assertEquals(0, refDisposeCounter)
} }
@Test // issue: https://github.com/JetBrains/compose-jb/issues/981
fun attributesUpdateShouldNotCauseInlineStylesCleanUp() = runTest {
var hasValue by mutableStateOf(false)
composition {
Button(attrs = {
style {
color(Color.red)
}
if (hasValue) value("buttonValue")
}) {
Text("Button")
}
}
assertEquals("""<button style="color: red;">Button</button>""", root.innerHTML)
hasValue = true
waitForAnimationFrame()
assertEquals("""<button style="color: red;" value="buttonValue">Button</button>""", root.innerHTML)
}
} }

Loading…
Cancel
Save