Skip to content

Commit

Permalink
Merge #424
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
bors[bot] and chvp committed Aug 3, 2019
2 parents 795d79a + 5abc145 commit 16695a2
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
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().ne(other.classes.iter()) {
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);
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

0 comments on commit 16695a2

Please sign in to comment.