Fix the PartialEq implementation for Element

The order of attributes in an `Element` doesn't matter anymore.
`<elem a="b" c="d" />` and `<elem c="d" a="b" />` are now correctly
considered equal.

For that I had to derive `PartialOrd` and `Ord` for `Attribute`.
This allows us to sort cloned vectors of `Attribute` in the `PartialEq`
implementation and compare them instead of the struct `attributes`.

Fixes #3
This commit is contained in:
Eijebong 2017-04-24 20:06:21 +02:00
commit 0d0c4b76eb
3 changed files with 27 additions and 2 deletions

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)?;