Skip to content
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

Fix attribute removal in props updates #151

Closed
wants to merge 2 commits into from

Conversation

chromakode
Copy link

While developing a cyclejs application, I noticed that sometimes spurious class attributes would remain after diffing. After digging a bit, it appears that the test for this functionality wasn't working: it was not checking the right element instance for the attribute, so the test was naively succeeding even though the attribute was not removed.

This patch fixes the test and implements working attribute removal. The removeAttribute method is now used for this, which requires us to translate JS property names to DOM attribute names ("className" -> "class"). I added a dependency on html-attributes for this.

Initially, I tried setting the attribute properties to null or undefined, because I didn't want to have to translate the prop names to attribute names. Unfortunately, some HTML attributes like "href" are tricky, and assigning null to them actually stringifies the value to 'null'. So it appears that this added complexity is necessary.

@TylorS
Copy link
Member

TylorS commented Aug 17, 2016

I must admit that I'm not really in favor of adding a dependency
Is it really needed?

That's my 2 cents anyways

@chromakode
Copy link
Author

chromakode commented Aug 17, 2016

Yeah, I didn't want to add a dep either. Looks like we could take a page out of React's book and special-case the 4 attributes that aren't simply a lowercase transformation. Will try this out and update.

@chromakode
Copy link
Author

Alright! That worked. Updated as discussed and expanded the test to cover all of the cases.

@chromakode
Copy link
Author

chromakode commented Sep 6, 2016

@paldepind apologies if this is disruptive, but wanted to bump this PR in case it slipped through the cracks. I think this is a pretty serious bug: snabbdom is currently not removing attributes in DOM updates.

I've rewritten the PR to no longer add any dependencies. This is a pretty simple fix and includes complete tests. Please let me know if there's anything else I can do to help finish this up. I'd love to get it out, since it fixes broken styling in a cyclejs project I'm publishing soon. Thanks!

@amirouche
Copy link
Contributor

This might fix #167

@AlexGalays
Copy link
Contributor

AlexGalays commented Jan 16, 2017

Would it fix your issue if snabbdom did set the class attribute rather than the className property inside createElm ? Or are you using the className property yourself ?

Also, I don't think all properties mirror to an attribute. e.g input.value. While removeAttribute might be a no-op in theses cases, it seems wasteful.

@mightyiam
Copy link
Contributor

The props module and the attributes module are two separate modules. They currently do not intend to overlap, even though in HTML and the DOM some of them do. This keeps the library simple, at the possible cost of inconvenience for the user in those cases. To determine this inconvenience, I would like to see a use case with some code, please. I know it has been a while, but still. If someone bothered making a PR for this, it may be worth addressing, either as a change or as documentation.

Is there an issue open for this?

Copy link
Contributor

@mightyiam mightyiam left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just marking this as "changes requested" for tracking purposes.

mightyiam added a commit that referenced this pull request May 27, 2020
BREAKING CHANGE: props module does not attempt to delete node
properties. This may affect you if you are using the props module
to add non-native (custom) properties to DOM nodes. Instead, it is
recommended to use _data-* attributes_.
https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes

Fixes #623.
Fixes #283.
Closes #415.
Closes #307.
Closes #151.
Closes #307.
Closes #416.
mightyiam added a commit that referenced this pull request May 27, 2020
BREAKING CHANGE: props module does not attempt to delete node
properties. This may affect you if you are using the props module
to add non-native (custom) properties to DOM nodes. Instead, it is
recommended to use _data-* attributes_.
https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes

Fixes #623.
Fixes #283.
Closes #415.
Closes #307.
Closes #151.
Closes #416.
mightyiam added a commit that referenced this pull request May 30, 2020
BREAKING CHANGE: props module does not attempt to delete node
properties. This may affect you if you are using the props module
to add non-native (custom) properties to DOM nodes. Instead, it is
recommended to use _data-* attributes_.
https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes

Fixes #623.
Fixes #283.
Closes #415.
Closes #307.
Closes #151.
Closes #416.
mightyiam added a commit that referenced this pull request Jun 12, 2020
BREAKING CHANGE: props module does not attempt to delete node
properties. This may affect you if you are using the props module
to add non-native (custom) properties to DOM nodes. Instead, it is
recommended to use _data-* attributes_.
https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes

Fixes #623.
Fixes #283.
Closes #415.
Closes #307.
Closes #151.
Closes #416.
mightyiam added a commit that referenced this pull request Jun 12, 2020
BREAKING CHANGE: props module does not attempt to delete node
properties. This may affect you if you are using the props module
to add non-native (custom) properties to DOM nodes. Instead, it is
recommended to use _data-* attributes_.
https://developer.mozilla.org/en-US/docs/Learn/HTML/Howto/Use_data_attributes

Fixes #623.
Fixes #283.
Closes #415.
Closes #307.
Closes #151.
Closes #416.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants