Compose HTML: add an AttrsScope<*>.attr extension function with a Boolean parameter - #5178
Compose HTML: add an AttrsScope<*>.attr extension function with a Boolean parameter#5178Yongshun Ye (ShreckYe) wants to merge 4 commits into
AttrsScope<*>.attr extension function with a Boolean parameter#5178Conversation
| * @see AttrsScope.attr | ||
| */ | ||
| fun AttrsScope<*>.attr(attr: String, value: Boolean = true) = | ||
| attr(attr, value.toString()) |
There was a problem hiding this comment.
Any specific reason making it an extension function?
What do you think about adding this function to the AttrsScope itself?
Would you like to add a test for this new function in AttributesTests?
There was a problem hiding this comment.
Thanks for replying. Adding it as a member function looks good to me too. And sure I'd love to add some tests for this.
… `attr` in the `AttrsScope` interface as suggested by @eymar
|
Hi, I have finished the requested changes. However, I have noticed something else that the Is this the new recommended way that's not documented yet? Shall we update the KDocs and this added boolean |
Yongshun Ye (@ShreckYe) , Good question!
So passing "false" doesn't really make it "false". And the current implementation you made won't be correct. Also here: https://developer.mozilla.org/en-US/docs/Web/HTML/Attributes#boolean_attributes
|
|
Thanks for replying and the references you provide. It explains everything.
For this, what I am thinking is just removing the |
It's already possible to achieve this by But there is a problem in the KDoc - it contradicts the official doc about boolean attributes - https://html.spec.whatwg.org/multipage/common-microsyntaxes.html#boolean-attributes: I think that adding an WDYT about this: /**
* Conditionally sets a boolean HTML attribute based on a condition.
*/
fun <TElement : Element> AttrsScope<TElement>.attr(
name: String,
condition: Boolean
): AttrsScope<TElement> {
if (condition) {
attr(name, "")
}
return this
}P.S. I apologise for no replies here. |
The
AttrsScope.attrmember function supports bolean attributes via "For boolean attributes cast boolean value to String and pass it as value.". This feature turns out to be frequently used in this compose-html-material project I have been working on. So I'd like to propose to add this to the core library.