From 6ca21dcbcd70d1729d4ab283c2af1dafce40a0f0 Mon Sep 17 00:00:00 2001 From: Yongshun Shreck Ye Date: Mon, 9 Dec 2024 20:45:59 +0800 Subject: [PATCH 1/4] Add an `AttrsScope<*>.attr` extension function with a `Boolean` parameter --- .../org/jetbrains/compose/web/attributes/AttrsScope.kt | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/html/core/src/jsMain/kotlin/org/jetbrains/compose/web/attributes/AttrsScope.kt b/html/core/src/jsMain/kotlin/org/jetbrains/compose/web/attributes/AttrsScope.kt index 43bb3992f25..0e706b6008a 100644 --- a/html/core/src/jsMain/kotlin/org/jetbrains/compose/web/attributes/AttrsScope.kt +++ b/html/core/src/jsMain/kotlin/org/jetbrains/compose/web/attributes/AttrsScope.kt @@ -246,3 +246,10 @@ open class AttrsScopeBuilder( private val setClassList: (HTMLElement, Array) -> Unit = { e, classList -> e.classList.add(*classList) } + +/** + * Adds a boolean attribute. + * @see AttrsScope.attr + */ +fun AttrsScope<*>.attr(attr: String, value: Boolean = true) = + attr(attr, value.toString()) From cf307ece602f65dc25e9bfc528dc07a622a04d39 Mon Sep 17 00:00:00 2001 From: Yongshun Shreck Ye Date: Thu, 12 Dec 2024 00:42:46 +0800 Subject: [PATCH 2/4] Convert the `AttrsScope<*>.attr` extension function into the function `attr` in the `AttrsScope` interface as suggested by @eymar --- .../compose/web/attributes/AttrsScope.kt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/html/core/src/jsMain/kotlin/org/jetbrains/compose/web/attributes/AttrsScope.kt b/html/core/src/jsMain/kotlin/org/jetbrains/compose/web/attributes/AttrsScope.kt index 0e706b6008a..5aa369f4a85 100644 --- a/html/core/src/jsMain/kotlin/org/jetbrains/compose/web/attributes/AttrsScope.kt +++ b/html/core/src/jsMain/kotlin/org/jetbrains/compose/web/attributes/AttrsScope.kt @@ -97,10 +97,17 @@ interface AttrsScope : EventsListenerScope { * @param attr - the name of the attribute * @param value - the value of the attribute * - * For boolean attributes cast boolean value to String and pass it as value. + * For boolean attributes, use the other overload with a boolean [value] parameter, or cast boolean value to String and pass it as value. */ fun attr(attr: String, value: String): AttrsScope + /** + * Adds a boolean attribute. + * @see AttrsScope.attr + */ + fun attr(attr: String, value: Boolean = true) = + attr(attr, value.toString()) + /** * [prop] allows setting values of element's properties which can't be set using [attr]. * [update] is a lambda with two parameters: `element` and `value`. `element` is a reference to a native element. @@ -198,7 +205,7 @@ open class AttrsScopeBuilder( * @param attr - the name of the attribute * @param value - the value of the attribute * - * For boolean attributes cast boolean value to String and pass it as value. + * For boolean attributes, use the other overload with a boolean [value] parameter, or cast boolean value to String and pass it as value. */ override fun attr(attr: String, value: String): AttrsScope { attributesMap[attr] = value @@ -246,10 +253,3 @@ open class AttrsScopeBuilder( private val setClassList: (HTMLElement, Array) -> Unit = { e, classList -> e.classList.add(*classList) } - -/** - * Adds a boolean attribute. - * @see AttrsScope.attr - */ -fun AttrsScope<*>.attr(attr: String, value: Boolean = true) = - attr(attr, value.toString()) From 06485453c405d9457745911acaf95d57973304c4 Mon Sep 17 00:00:00 2001 From: Yongshun Shreck Ye Date: Thu, 12 Dec 2024 01:05:10 +0800 Subject: [PATCH 3/4] Add a `booleanAttributeTest` in `AttributesTests` as requested by @eymar --- .../src/jsTest/kotlin/elements/AttributesTests.kt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/html/core/src/jsTest/kotlin/elements/AttributesTests.kt b/html/core/src/jsTest/kotlin/elements/AttributesTests.kt index 790e895383c..addf881eb40 100644 --- a/html/core/src/jsTest/kotlin/elements/AttributesTests.kt +++ b/html/core/src/jsTest/kotlin/elements/AttributesTests.kt @@ -579,4 +579,16 @@ class AttributesTests { assertEquals("400", attrsMap["height"]) } } + + @Test + fun booleanAttributeTest() = runTest { + composition { + TextInput { + attr("required", true) + } + } + with(nextChild()) { + assertEquals("true", getAttribute("required")) + } + } } From 07db07f7b6d326cdd15f489238ca52d2f6600d63 Mon Sep 17 00:00:00 2001 From: Yongshun Shreck Ye Date: Thu, 12 Dec 2024 01:05:41 +0800 Subject: [PATCH 4/4] Reformat AttributesTests.kt BTW --- .../src/jsTest/kotlin/elements/AttributesTests.kt | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/html/core/src/jsTest/kotlin/elements/AttributesTests.kt b/html/core/src/jsTest/kotlin/elements/AttributesTests.kt index addf881eb40..f866706bcc7 100644 --- a/html/core/src/jsTest/kotlin/elements/AttributesTests.kt +++ b/html/core/src/jsTest/kotlin/elements/AttributesTests.kt @@ -1,8 +1,8 @@ package org.jetbrains.compose.web.core.tests import androidx.compose.runtime.Composable -import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import kotlinx.browser.document import kotlinx.dom.clear @@ -10,11 +10,10 @@ import org.jetbrains.compose.web.attributes.* import org.jetbrains.compose.web.css.* import org.jetbrains.compose.web.dom.* import org.jetbrains.compose.web.dom.Text +import org.jetbrains.compose.web.testutils.runTest +import org.w3c.dom.* import kotlin.test.Test import kotlin.test.assertEquals -import org.jetbrains.compose.web.testutils.* -import org.w3c.dom.* -import kotlin.test.assertContains import kotlin.test.assertTrue class AttributesTests { @@ -459,7 +458,7 @@ class AttributesTests { id("id$readKey") } ) { - @Suppress("DEPRECATION") + @Suppress("DEPRECATION") DisposableRefEffect(readKey) { val p = document.createElement("p").also { it.innerHTML = "Key=$readKey" } it.appendChild(p) @@ -508,7 +507,7 @@ class AttributesTests { with(child) { val attrs = getAttributeNames().toList() assertEquals(2, attrs.size) - assertTrue(attrs.containsAll(listOf("style", "class",))) + assertTrue(attrs.containsAll(listOf("style", "class"))) assertEquals("button", tagName.lowercase()) assertEquals("a", getAttribute("class"))