pub struct Attribute { /* private fields */ }Expand description
A single HTML attribute.
Values are escaped when the attribute is built, not when it is rendered — see
crate::core::escape.
§Examples
use molt::core::Attribute;
assert_eq!(Attribute::new("href", "/a?x=1&y=2").to_string(), r#" href="/a?x=1&y=2""#);
assert_eq!(Attribute::boolean("required").to_string(), " required");Implementations§
Source§impl Attribute
impl Attribute
Sourcepub fn new(key: impl Into<String>, value: impl AsRef<str>) -> Self
pub fn new(key: impl Into<String>, value: impl AsRef<str>) -> Self
Creates an attribute, escaping the value.
Sourcepub fn trusted_url(key: impl Into<String>, value: impl AsRef<str>) -> Self
pub fn trusted_url(key: impl Into<String>, value: impl AsRef<str>) -> Self
A URL attribute rendered as given, even when the value is a script URL.
href, src, action and the other URL attributes render as # when the value
would run script. Use this only for a URL you wrote yourself. The value is still
attribute-escaped.
§Examples
use molt::prelude::*;
let link = a().add_attribute(Attribute::trusted_url("href", "javascript:print()"));
assert_eq!(link.render(), "<a href=\"javascript:print()\"></a>");Sourcepub fn raw(key: impl Into<String>, value: impl Into<String>) -> Self
pub fn raw(key: impl Into<String>, value: impl Into<String>) -> Self
Creates an attribute without escaping the value.
One of the three escape hatches, with Node::raw and
Element::raw_text — grep for the three to audit
a codebase. Use it for a value you wrote yourself and have already escaped. Never
pass user data: a " in the value closes the attribute and what follows is markup.
The URL policy still runs on the stored value, so a raw javascript: URL in href
is rendered as #; only Self::trusted_url skips that.
The meta_* helpers do not use it: they are pub and take &str, so they escape.
§Examples
use molt::core::Attribute;
// Already escaped by the caller, so it must not be escaped again.
assert_eq!(Attribute::raw("title", "a & b").to_string(), r#" title="a & b""#);Sourcepub fn boolean(key: impl Into<String>) -> Self
pub fn boolean(key: impl Into<String>) -> Self
Creates a boolean attribute, which renders as a bare key.
hidden, checked, required, open, controls, autoplay, muted.
Sourcepub fn is_boolean(&self) -> bool
pub fn is_boolean(&self) -> bool
Whether this attribute renders as a bare key.
Trait Implementations§
impl Eq for Attribute
impl StructuralPartialEq for Attribute
Auto Trait Implementations§
impl Freeze for Attribute
impl RefUnwindSafe for Attribute
impl Send for Attribute
impl Sync for Attribute
impl Unpin for Attribute
impl UnsafeUnpin for Attribute
impl UnwindSafe for Attribute
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more