pub struct Element { /* private fields */ }Expand description
An HTML element.
The element has a Tag, possibly some attributes, and possibly
some children. It may also have a location: this is used when the
element is constructed by parsing some input value.
Implementations§
Source§impl Element
impl Element
Sourcepub fn with_location(self, line: usize, col: usize) -> Self
pub fn with_location(self, line: usize, col: usize) -> Self
Set the location of an element in a source file.
Sourcepub fn with_child(self, child: Element) -> Self
pub fn with_child(self, child: Element) -> Self
Append a child element, when constructing.
Sourcepub fn with_text(self, child: impl Into<String>) -> Self
pub fn with_text(self, child: impl Into<String>) -> Self
Append a text child, when constructing.
Sourcepub fn with_attribute(
self,
name: impl AsRef<str>,
value: impl AsRef<str>,
) -> Self
pub fn with_attribute( self, name: impl AsRef<str>, value: impl AsRef<str>, ) -> Self
Set an attribute when creating an element.
Sourcepub fn with_boolean_attribute(self, name: impl Into<String>) -> Self
pub fn with_boolean_attribute(self, name: impl Into<String>) -> Self
Set a boolean attribute when creating an element.
Sourcepub fn with_class(self, class: impl Into<String>) -> Self
pub fn with_class(self, class: impl Into<String>) -> Self
Add a class when creating an element.
Sourcepub fn attributes(&self) -> impl Iterator<Item = &str>
pub fn attributes(&self) -> impl Iterator<Item = &str>
Return an iterator over the names of the attributes of an element.
Sourcepub fn attribute(&self, name: impl AsRef<str>) -> Option<&AttributeValue>
pub fn attribute(&self, name: impl AsRef<str>) -> Option<&AttributeValue>
Return the value of an attribute, if the attribute is set.
Otherwise, return None.
Sourcepub fn attribute_value(&self, name: impl AsRef<str>) -> Option<&str>
pub fn attribute_value(&self, name: impl AsRef<str>) -> Option<&str>
Return the value of an attribute as text, if the attribute is
set. Otherwise, return None.
Sourcepub fn set_attribute(
&mut self,
name: impl Into<String>,
value: impl Into<String>,
)
pub fn set_attribute( &mut self, name: impl Into<String>, value: impl Into<String>, )
Set a key/value attribute. If the attribute was already set, change the value it has.
Sourcepub fn set_boolean_attribute(&mut self, name: impl Into<String>)
pub fn set_boolean_attribute(&mut self, name: impl Into<String>)
Set a boolean attribute.
Sourcepub fn unset_attribute(&mut self, name: impl AsRef<str>)
pub fn unset_attribute(&mut self, name: impl AsRef<str>)
Remove an attribute, which can be key/value or boolean.
Sourcepub fn classes(&self) -> impl Iterator<Item = &str>
pub fn classes(&self) -> impl Iterator<Item = &str>
Return current classes set directly for this element.
Sourcepub fn has_class(&self, wanted: impl AsRef<str>) -> bool
pub fn has_class(&self, wanted: impl AsRef<str>) -> bool
Does the element have a class set directly?
Sourcepub fn add_class(&mut self, class: impl Into<String>)
pub fn add_class(&mut self, class: impl Into<String>)
Add a class to the element. This does not replace existing classes.
Sourcepub fn push_text(&mut self, text: impl Into<String>)
pub fn push_text(&mut self, text: impl Into<String>)
Append text to element. It will be escaped, if needed, when the element is serialized.
Sourcepub fn push_child(&mut self, child: Element)
pub fn push_child(&mut self, child: Element)
Append a child element to this element.
Sourcepub fn push_children(&mut self, children: &[Element])
pub fn push_children(&mut self, children: &[Element])
Append several child elements to this element.
Sourcepub fn clear_children(&mut self)
pub fn clear_children(&mut self)
Remove all children.
Sourcepub fn push_html(&mut self, html: impl Into<String>)
pub fn push_html(&mut self, html: impl Into<String>)
Append HTML to element. It will NOT be escaped, when the element is serialized. This is an easy to inject arbitrary junk into the HTML. No validation is done. You should avoid this if you can.
Sourcepub fn plain_text(&self) -> String
pub fn plain_text(&self) -> String
Return all the textual content in an element and its children. This does not include attributes.