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

ordering of classes in html tags #393

Closed
stephanbuys opened this issue Sep 7, 2018 · 7 comments · Fixed by #424
Closed

ordering of classes in html tags #393

stephanbuys opened this issue Sep 7, 2018 · 7 comments · Fixed by #424

Comments

@stephanbuys
Copy link
Contributor

Description

bug report

Currently the order in which classes are added to the DOM in the browser do not reflect the same order in which they were added in the Renderable -> View function using the html! macro.

Expected Results

html!(<div class="three wide column",></div>) should result in <div class="three wide column"></div> in the DOM.

Actual Results

html!(<div class="three wide column",></div>) should result in <div class="wide column three"></div> in the DOM.

Context (Environment)

  • Rust: nightly-2018-08-24

  • yew: git/master e6fc66e

  • target: wasm32-unknown-unknown

  • cargo-web 0.6.15

  • firefox nightly

@stephanbuys
Copy link
Contributor Author

This matter because of frameworks such as Semantic UI: Semantic-Org/Semantic-UI#5484

@davidkna
Copy link
Contributor

davidkna commented Sep 7, 2018

Found the reason: https://github.com/DenisKolodin/yew/blob/e6fc66e185c83bb9bb08a86750a57a3558bc9a18/src/virtual_dom/mod.rs#L42-L43
Would need to switch to a different data structure that preservers order, maybe Vec. Arguably worth the loss in performance because the DOM preserves the order.

@hgzimmerman
Copy link
Member

hgzimmerman commented Sep 7, 2018

BTreeSet<String> may be more desirable than HashSet, given how its iter() method returns an iterator that preserves order, but the datastructure still has the properties of deduplication that HashSet<String> has, making it better than Vec<String>.

Ordered iterator: https://doc.rust-lang.org/std/collections/struct.BTreeSet.html#method.iter
No duplicate entries: https://doc.rust-lang.org/std/collections/struct.BTreeSet.html#method.insert

@stephanbuys
Copy link
Contributor Author

@hgzimmerman agreed, and going from HashSet to BTreeSet would preserve the Set API.

@stephanbuys
Copy link
Contributor Author

I tried a quick test, just replacing all occurrences, the classes seem to be disordered still, but at least deterministic.

@davidkna
Copy link
Contributor

davidkna commented Sep 7, 2018

It would be hard to implement the Ord trait for insertion order with BTreeSet. I think it would need a wrapper struct.
The js-native Set or and outside crate might do the job. The advantages of a set in general are also dimished here because the number of classes is usually very low. I think going with plain Vec might be most simple.

@hgzimmerman
Copy link
Member

Oh, I'm dumb. The BTreeSet will order them, but in no relation to insertion order (alphabetical).
@davidkna is right in that Vec should be the optimal datastructure for this task.

It may make sense to wrap Vec in a VecSet wrapper that prevents duplicate entries and implements the common set operations, but given how this is going to only be used as a list, it probably isn't worth the effort, unless deduplication is absolutely required.

bors bot added a commit that referenced this issue Aug 3, 2019
424: Preserve ordering of classes r=jstarry a=charvp

This should fix #393.

I didn't add functionality that preserves uniqueness of classes, since `classList.add` will do that for us.

Co-authored-by: Charlotte Van Petegem <charlotte@vanpetegem.me>
@bors bors bot closed this as completed in #424 Aug 3, 2019
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 a pull request may close this issue.

3 participants