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
45 lines
1.1 KiB
Rust
45 lines
1.1 KiB
Rust
//! Provides an `Attribute` type which represents an attribute in an XML document.
|
|
|
|
use xml::escape::escape_str_attribute;
|
|
|
|
use std::fmt;
|
|
|
|
/// An attribute of a DOM element.
|
|
///
|
|
/// This is of the form: `name`="`value`"
|
|
///
|
|
/// This does not support prefixed/namespaced attributes yet.
|
|
#[derive(Clone, Debug, PartialEq, Eq, Ord, PartialOrd)]
|
|
pub struct Attribute {
|
|
/// The name of the attribute.
|
|
pub name: String,
|
|
/// The value of the attribute.
|
|
pub value: String,
|
|
}
|
|
|
|
impl fmt::Display for Attribute {
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
write!(fmt, "{}=\"{}\"", self.name, escape_str_attribute(&self.value))
|
|
}
|
|
}
|
|
|
|
impl Attribute {
|
|
/// Construct a new attribute from the given `name` and `value`.
|
|
///
|
|
/// # Examples
|
|
///
|
|
/// ```
|
|
/// use minidom::Attribute;
|
|
///
|
|
/// let attr = Attribute::new("name", "value");
|
|
///
|
|
/// assert_eq!(attr.name, "name");
|
|
/// assert_eq!(attr.value, "value");
|
|
/// ```
|
|
pub fn new<N: Into<String>, V: Into<String>>(name: N, value: V) -> Attribute {
|
|
Attribute {
|
|
name: name.into(),
|
|
value: value.into(),
|
|
}
|
|
}
|
|
}
|