solid icon indicating copy to clipboard operation
solid copied to clipboard

Question regarding 'empty' boolean properties

Open kiel-sparklayer opened this issue 1 year ago • 2 comments

Describe the bug

I'd like to clarify that the behaviour i'm observing with regards to 'empty' boolean properties of custom elements is intentional or not. Referring to the playground link provided, i have created a custom element that accepts a properties object with a value named 'reducedSize' defaulted to false via the 'customElement' function. If i supply a value to the property it behaves as i would expect, but if the property is added, but no value is passed, it behaves as if the property is not specified at all. See case 6 in the playground example.

Your Example Website or App

https://playground.solidjs.com/anonymous/5bb48332-a44b-46e7-b102-d70b0ff7f300

Steps to Reproduce the Bug or Issue

See the playground link for demo. Case 6 at the link is the one i'm nost interested in as that isn't behaving the way i would expect.

Expected behavior

I would expect the boolean typed property here to default to false when not provided at all, which it is, and when the property is specified without a value that it would be parsed as truthy and therefore end up assigned as a boolean true.

Screenshots or Videos

No response

Platform

Any

Additional context

No response

kiel-sparklayer avatar Oct 14 '24 14:10 kiel-sparklayer

Yeah that's an interesting one. I do think there is probably a bug in solid-element here but I'm not sure you'd get the results you were looking for anyway. Mostly that we'd need to know it is a boolean attribute to treat it like a boolean. In truly boolean attribute in the DOM a string value of "false" is true. So the behavior of it is a bit more ambiguous. Because here we parse "false" to false which is also inconsistent. The problem of just casting it to true in this case isn't enough.

It's possible that we shouldn't be trying to parse any attribute value but I wanted to support serialization of arrays and objects as well as handle non-string values like false properly. So we are kind of trapped both ways. It's possible that we could make parse not the default behavior but that has wider reprocussions.

Alternatively if you want a truly boolean property you could turn parsing off. Right now that is broken: https://playground.solidjs.com/anonymous/5d11b0d3-fb73-4401-8e83-2998cae379da

But if I fix the bug then this would work. I think this might be the best option so you could simulate both situations.

Actually all 3 if you really wanted to. If you leave parsing on and check the way you do with strict equals then "" won't be false and give you the expected results.

ryansolid avatar Oct 14 '24 19:10 ryansolid

For my use case, the ability to disable parsing for these type of boolean properties would be ideal. Thanks for looking into it.

kiel-sparklayer avatar Oct 15 '24 07:10 kiel-sparklayer

For my use case, the ability to disable parsing for these type of boolean properties would be ideal. Thanks for looking into it.

Perfect. Lets see what we can do.

ryansolid avatar Oct 21 '24 17:10 ryansolid