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 wrong order of classes when reordering or adding classes to the front #927

Merged
merged 3 commits into from
Feb 8, 2020

Conversation

liquidnya
Copy link
Contributor

An implementation with 2 test cases which should fix #926.
I also replaced set_attribute and remove_attribute with their stdweb equivalents.
Unfortunately the iterator of indexmap::set::IndexSet is not fused, this might create some overhead.
This fix might introduce more updates (add, remove) to the class_list.

@jstarry
Copy link
Member

jstarry commented Feb 7, 2020

Thanks for the quick turn around on this PR @LiquidBlock!

What do you think about using set_attribute("class", className) instead of using patches to update classList? We would only need to set that attribute if the two indexsets are different

@liquidnya
Copy link
Contributor Author

liquidnya commented Feb 7, 2020

What do you think about using set_attribute("class", className) instead of using patches to update classList? We would only need to set that attribute if the two indexsets are different

That would propably result in faster and less code. I'll give it a try.

@liquidnya
Copy link
Contributor Author

There is a comment in the code at https://github.com/yewstack/yew/blob/master/src/virtual_dom/vtag.rs#L158-L160.

     /// Adds attribute to a virtual node. Not every attribute works when
     /// it set as attribute. We use workarounds for:
     /// `class`, `type/kind`, `value` and `checked`.

Does this mean, that there might be (or have been) complications with set_attribute("class", className)?

@@ -136,6 +136,12 @@ impl<T: AsRef<str>> From<Vec<T>> for Classes {
}
}

impl PartialEq for Classes {
fn eq(&self, other: &Self) -> bool {
self.set.len() == other.set.len() && self.set.iter().eq(other.set.iter())
Copy link
Member

Choose a reason for hiding this comment

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

Nice, did your tests catch this? Pretty odd that IndexSet's equality implementation isn't transitive

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My tests did not catch this, but the vtag implementation did not use the PartialEq trait of Classes.
Instead it compared the inner classes.set, but since diff_classes(...) also needs to check if the classes changed, i moved the ordered equal check to the PartialEq trait of Classes.

@jstarry
Copy link
Member

jstarry commented Feb 8, 2020

Does this mean, that there might be (or have been) complications with set_attribute("class", className)?

Good catch, I wasn't aware of the inconsistency of browser support for setAttribute("class", ...). Looks like the ideal solution would be if stdweb supported setting the className property on elements. Unfortunately, it doesn't :/

As far as I can tell, it's only IE that doesn't work well with setAttribute. IE support isn't a priority right now so let's move forward this simpler approach.

@jstarry jstarry merged commit 839b793 into yewstack:master Feb 8, 2020
@liquidnya
Copy link
Contributor Author

Good catch, I wasn't aware of the inconsistency of browser support for setAttribute("class", ...). Looks like the ideal solution would be if stdweb supported setting the className property on elements. Unfortunately, it doesn't :/

The absent of className in stdweb is why i first tried to fix the bug with remove and add patches.
What do you think about using className within an js!(...) workaround like
kind (https://github.com/yewstack/yew/blob/master/src/virtual_dom/vtag.rs#L314) and checked (https://github.com/yewstack/yew/blob/master/src/virtual_dom/vtag.rs#L463) do?

@jstarry
Copy link
Member

jstarry commented Feb 10, 2020

What do you think about using className within an js!(...) workaround

I think that's a great idea

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.

ordering of classes different when reordering or re-adding
2 participants