attribute.rs

 1use xml::escape::escape_str_attribute;
 2
 3use std::fmt;
 4
 5/// An attribute of a DOM element.
 6///
 7/// This is of the form: `name`="`value`"
 8///
 9/// This does not support prefixed/namespaced attributes yet.
10#[derive(Clone, Debug, PartialEq, Eq)]
11pub struct Attribute {
12    /// The name of the attribute.
13    pub name: String,
14    /// The value of the attribute.
15    pub value: String,
16}
17
18impl fmt::Display for Attribute {
19    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
20        write!(fmt, "{}=\"{}\"", self.name, escape_str_attribute(&self.value))
21    }
22}
23
24impl Attribute {
25    /// Construct a new attribute from the given `name` and `value`.
26    ///
27    /// # Examples
28    ///
29    /// ```
30    /// use minidom::Attribute;
31    ///
32    /// let attr = Attribute::new("name", "value");
33    ///
34    /// assert_eq!(attr.name, "name");
35    /// assert_eq!(attr.value, "value");
36    /// ```
37    pub fn new<N: Into<String>, V: Into<String>>(name: N, value: V) -> Attribute {
38        Attribute {
39            name: name.into(),
40            value: value.into(),
41        }
42    }
43}