Merge branch 'fix_equality' into 'master'

Fix the PartialEq implementation for Element

Closes #3

See merge request !3
This commit is contained in:
lumi 2017-04-25 22:44:59 +00:00
commit 64f212d11b
3 changed files with 27 additions and 2 deletions

View file

@ -9,7 +9,7 @@ use std::fmt;
/// This is of the form: `name`="`value`"
///
/// This does not support prefixed/namespaced attributes yet.
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
pub struct Attribute {
/// The name of the attribute.
pub name: String,

View file

@ -20,7 +20,7 @@ use std::slice;
use convert::{IntoElements, IntoAttributeValue, ElementEmitter};
#[derive(Clone, PartialEq, Eq)]
#[derive(Clone, Eq)]
/// A struct representing a DOM Element.
pub struct Element {
name: String,
@ -29,6 +29,20 @@ pub struct Element {
children: Vec<Node>,
}
impl PartialEq for Element {
fn eq(&self, other: &Element) -> bool {
let mut my_attr = self.attributes.clone();
my_attr.sort();
let mut other_attr = other.attributes.clone();
other_attr.sort();
self.name == other.name &&
self.namespace == other.namespace &&
my_attr == other_attr &&
self.children == other.children
}
}
impl fmt::Debug for Element {
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
write!(fmt, "<{}", self.name)?;

View file

@ -94,3 +94,14 @@ fn namespace_propagation_works() {
.get_child("grandchild", "root_ns").unwrap()
.ns(), root.ns());
}
#[test]
fn two_elements_with_same_arguments_different_order_are_equal() {
let elem1: Element = "<a b='a' c=''/>".parse().unwrap();
let elem2: Element = "<a c='' b='a'/>".parse().unwrap();
assert_eq!(elem1, elem2);
let elem1: Element = "<a b='a' c=''/>".parse().unwrap();
let elem2: Element = "<a c='d' b='a'/>".parse().unwrap();
assert_ne!(elem1, elem2);
}