Skip to main content

Element

Struct Element 

Source
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

Source

pub fn new(tag: Tag) -> Self

Create a new element, with a given tag.

Source

pub fn p() -> Self

Create a P element.

Source

pub fn span() -> Self

Create a Span element.

Source

pub fn div() -> Self

Create a Div element.

Source

pub fn code() -> Self

Create a Code element.

Source

pub fn h1() -> Self

Create an H1 element.

Source

pub fn h2() -> Self

Create an H2 element.

Source

pub fn h3() -> Self

Create an H3 element.

Source

pub fn with_location(self, line: usize, col: usize) -> Self

Set the location of an element in a source file.

Source

pub fn with_child(self, child: Element) -> Self

Append a child element, when constructing.

Source

pub fn with_text(self, child: impl Into<String>) -> Self

Append a text child, when constructing.

Source

pub fn with_attribute( self, name: impl AsRef<str>, value: impl AsRef<str>, ) -> Self

Set an attribute when creating an element.

Source

pub fn with_boolean_attribute(self, name: impl Into<String>) -> Self

Set a boolean attribute when creating an element.

Source

pub fn with_class(self, class: impl Into<String>) -> Self

Add a class when creating an element.

Source

pub fn tag(&self) -> Tag

Return the Tag of the element.

Source

pub fn location(&self) -> Option<(usize, usize)>

Return the location of the element.

Source

pub fn children(&self) -> &[Content]

Return list children.

Source

pub fn attributes(&self) -> impl Iterator<Item = &str>

Return an iterator over the names of the attributes of an element.

Source

pub fn attribute(&self, name: impl AsRef<str>) -> Option<&AttributeValue>

Return the value of an attribute, if the attribute is set. Otherwise, return None.

Source

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.

Source

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.

Source

pub fn set_boolean_attribute(&mut self, name: impl Into<String>)

Set a boolean attribute.

Source

pub fn unset_attribute(&mut self, name: impl AsRef<str>)

Remove an attribute, which can be key/value or boolean.

Source

pub fn classes(&self) -> impl Iterator<Item = &str>

Return current classes set directly for this element.

Source

pub fn has_class(&self, wanted: impl AsRef<str>) -> bool

Does the element have a class set directly?

Source

pub fn add_class(&mut self, class: impl Into<String>)

Add a class to the element. This does not replace existing classes.

Source

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.

Source

pub fn push_child(&mut self, child: Element)

Append a child element to this element.

Source

pub fn push_children(&mut self, children: &[Element])

Append several child elements to this element.

Source

pub fn clear_children(&mut self)

Remove all children.

Source

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.

Source

pub fn serialize(&self) -> String

Serialize an element into HTML.

Source

pub fn plain_text(&self) -> String

Return all the textual content in an element and its children. This does not include attributes.

Trait Implementations§

Source§

impl Clone for Element

Source§

fn clone(&self) -> Element

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Element

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Element

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl From<&Element> for Element

Source§

fn from(value: &Element) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Element

Source§

fn eq(&self, other: &Element) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Element

Source§

impl StructuralPartialEq for Element

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.