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

Preserve ordering of classes #424

Merged
merged 3 commits into from
Aug 3, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ anymap = "0.12"
bincode = "=1.0.1"
failure = "0.1"
http = "0.1"
indexmap = "1.0.2"
log = "0.4"
proc-macro-hack = "0.5"
proc-macro-nested = "0.1"
Expand Down
5 changes: 3 additions & 2 deletions src/virtual_dom/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ pub mod vnode;
pub mod vtag;
pub mod vtext;

use std::collections::{HashMap, HashSet};
use std::collections::HashMap;
use std::fmt;
use stdweb::web::{Element, EventListenerHandle, Node};
use indexmap::set::IndexSet;

pub use self::vcomp::VComp;
pub use self::vlist::VList;
Expand Down Expand Up @@ -40,7 +41,7 @@ type Listeners<COMP> = Vec<Box<dyn Listener<COMP>>>;
type Attributes = HashMap<String, String>;

/// A set of classes.
type Classes = HashSet<String>;
type Classes = IndexSet<String>;

/// Patch for DOM node modification.
enum Patch<ID, T> {
Expand Down
2 changes: 1 addition & 1 deletion src/virtual_dom/vtag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ impl<COMP: Component> PartialEq for VTag<COMP> {
return false;
}

if self.classes != other.classes {
if self.classes.iter().collect::<Vec<&String>>() != other.classes.iter().collect::<Vec<&String>>() {
Copy link
Member

Choose a reason for hiding this comment

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

Looks like you can use this for inequality: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.ne

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I tried a simple != but that didn't work. Fixed!

return false;
}

Expand Down
21 changes: 20 additions & 1 deletion tests/vtag_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ fn supports_multiple_classes_string() {
<div class="class-2 class-3 class-1"></div>
};

assert_eq!(a, b);
jstarry marked this conversation as resolved.
Show resolved Hide resolved
assert_ne!(a, b);

if let VNode::VTag(vtag) = a {
println!("{:?}", vtag.classes);
Expand All @@ -213,6 +213,25 @@ fn supports_multiple_classes_string() {
}
}

#[test]
fn keeps_order_of_classes() {
let a: VNode<Comp> = html! {
<div class="class-1 class-2 class-3",></div>
};

if let VNode::VTag(mut vtag) = a {
println!("{:?}", vtag.classes);
assert_eq!(
vtag.classes.drain(..).collect::<Vec<String>>(),
vec![
String::from("class-1"),
String::from("class-2"),
String::from("class-3")
]
)
}
}

#[test]
fn it_compares_values() {
let a: VNode<Comp> = html! {
Expand Down