-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ignore undefined/falsey properties on components and elements #1434
Comments
I'd advice against changing the semantics of spread. I think comprehensions in js could have solved this issue: |
I think the proposal in Gitter wasn't spread-specific, but would rather apply here as well: <div aria-hidden={hidden}>...</div> That currently renders as If we removed attributes with falsy values other than |
It may be bad practice, but I do expect to be able to write styles using selectors like |
I'm leaning toward @TehShrike's answer, especially since one could still get undefined in an attribute through a string:
|
I like |
I also like About ignoring |
To clarify: is the correct implementation of this proposal (to ignore all properties with value I thiiiink that might be my preference, so I could do component.set(Object.assign({}, someObject, { ignoreThisProperty: undefined )) but that might break everything for folks who actually do want to set values to The alternative would be to change |
Don't render undefined/null attributes
Huh. As of 2.15, div.className = ctx.foo; So I guess we should reopen this? |
Maybe be should also ignore NaN values? |
I was having a look at this earlier today. As mentioned above, the problem is that for many attributes, we are updating them via their corresponding properties. So instead of As far as I know, there is no way to remove an attribute via operations to its corresponding prop. We'd need to ad an extra check for each of these, and either remove the attribute or update the prop as appropriate. It did make me wonder, though: What was the original purpose of using props when possible? This has been present since 1.0.0. Was it for performance reasons? Code size reasons? Knowing that would help with deciding how to implement this. |
Can anybody suggest any suitable workaround for this issue? |
Seems, this issue still relevant for v3. Any chance it to be fixed in the nearest future? |
It seems, that Vue use attributes: |
For now, using spread attributes allows you to control which attributes appear on an element. |
I know this is not a pressing issue but it'd be nice to remove attributes when the value is |
Just ran into this issue today. Adding my vote to have it fixed. Was it ever determined whether using |
This morning I was looking at this again and asked @lukeed whether he had any performance or other ideas about it. He said in the past he had written a view library that actually used attributes in most cases - all of them except for What this means for us I think is that we should comb through everything in the |
Fwiw, the spread workaround didn't work for me. It still set pattern="undefined" Can repro with I ended up using Lodash omitBy and isUndefined. E.g. |
@Conduitry How exactly can I do it by passing props from an external component to a component which spreads it's props? In my REPL example, assigning a value to the object prop reassign all it's content, omitting non-passed properties. |
@guilhermeocosta You're overriding the |
Thanks @kaisermann, I've ended up doing something very similar with your REPL. |
I've done some extensive research and experimentation. Here are my results. My findingsHow these result were obtained
Unique attributesI only found two attributes in the list that required special handling.
Boolean attributesThe handling of boolean attributes is some what unique among attribute types. If the property is set to any value other than One note is that
All other attributesAll other content attributes reflect the underlining IDL attributes exactly when setting, blindly setting the corresponding content attribute. getting the value is quite another story, many properties sanitize or transform the value before returning. This doesn't matter in our case since we only care about setting but I thought I'd mention it. ConclusionBased on the above we should be safe to use Edit: One other note. Not all browsers set the |
Thank you so much for doing all this @RedHatter! |
Using that list of attributes, but also adding back in
|
Functionally they're the same, jsdom just isn't reflecting the IDL attribute like it should. When the expected output is modified to include the attribute the tests pass fine. This leads me to believe that the binding features still work as they should and the failures are superficial.
|
I want to reopen this Issue because the spread operator is less usable than I'd like it to be.
https://svelte.dev/repl/cb2ad72a9e14443ea6c3deeb2c7b8c13?version=3.6.9 My guess, easiest way to resolve is to remove Thanks! |
Any update? I am hitting what seems to be the same kind of issue: I have a button that uses |
3.13.0 fixed a lot of niggling bugs with spread attributes. It looks like @cvlab's REPL example above is working now. @joelparkerhenderson Do you have a repro? If it only looks like a button - but is not actually one of the elements that the spec says has |
Brought up in chat by @arxpoetica: when using spread to apply an object's properties to an element, it is desirable to be able to say "apply everything except these specific properties".
Say, for example, something like
<svelte:component this={MyComponent} {...all} />
, which would result in aMyComponent
property being set on the component.If the spread operator ignored falsey values (or more safely, just ignored undefined values) on the object, it would be relatively easy to use something like
Object.assign
to specifically exclude some properties, say with this computed property:The text was updated successfully, but these errors were encountered: