Struct hamlet::attr::Attribute
[−]
[src]
pub struct Attribute<'a> { pub name: Cow<'a, str>, pub value: Cow<'a, str>, }
An HTML attribute.
Examples
let attr = hamlet::attr::Attribute::new("id", "foo"); assert_eq!(format!("{}", attr), "id=\"foo\"");
Fields
name | The attribute's name. The value of this field will not be validated, you must ensure it meets the requirements specified in the spec yourself. Exampleslet attr = hamlet::attr::Attribute::new("invalid=id", "foo"); assert_eq!(format!("{}", attr), "invalid=id=\"foo\""); |
value | The attribute's value. This field will be escaped automatically, if it is an empty string then the attribute will be written with the 'Empty attribute syntax'. Exampleslet attr = hamlet::attr::Attribute::new("id", "bar & baz"); assert_eq!(format!("{}", attr), "id=\"bar & baz\""); let attr = hamlet::attr::Attribute::new("checked", ""); assert_eq!(format!("{}", attr), "checked"); |
Methods
impl<'a> Attribute<'a>
fn new<N, V>(name: N, value: V) -> Attribute<'a> where N: Into<Cow<'a, str>>, V: Into<Cow<'a, str>>
Create an attribute, useful to avoid having to convert strings to
Cow<str>
yourself.
Generally this shouldn't be used directly by end users, it's likely
that there are builder APIs or macros available that make attribute
construction easier, for example the modification methods on
AttributeList
or the
attrs!
macro.
Examples
use std::borrow::Cow; use hamlet::attr::Attribute; let foo = "foo".to_owned(); let foo2 = foo.clone(); assert_eq!( Attribute::new("id", foo), Attribute { name: Cow::Borrowed("id"), value: Cow::Owned(foo2), });