Enum Html

Source
#[non_exhaustive]
pub enum Html { Comment(String), Doctype { name: String, attr: Option<String>, }, Empty, Tag { tag: Tag, child: Box<Html>, }, Text(String), Vec(Box<[Html]>), }
Expand description

Dom tree structure to represent the parsed html.

This tree represents the whole parsed HTML. To create an Html from a string, use the Html::parse function.

§Examples

use html_filter::prelude::*;

let _html: Html = Html::parse(
    r#"<nav>
    <!-- Navigation menu -->
    <ul>
        <li href="first">First link</li>
        <li href="second">Second link</li>
        <li href="third">Third link</li>
    </ul>
</nav>"#,
)
.unwrap();

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Comment(String)

Comment block

§Example

<!-- some comment -->

§

Doctype

Document tag.

These are tags with exclamation marks

§Examples

<!doctype html>

Fields

This variant is marked as non-exhaustive
Non-exhaustive enum variants could have additional fields added in future. Therefore, non-exhaustive enum variants cannot be constructed in external crates and cannot be matched against.
§name: String

Name of the tag

§Examples

In the previous example, the name is doctype.

§attr: Option<String>

Attribute of the tag

§Examples

In the previous example, the attribute is html.

§

Empty

Empty html tree

Corresponds to an empty string

§

Tag

Tag

§Examples

  • <div id="blob">content</div>
  • <div attr />
  • </>

Fields

This variant is marked as non-exhaustive
Non-exhaustive enum variants could have additional fields added in future. Therefore, non-exhaustive enum variants cannot be constructed in external crates and cannot be matched against.
§tag: Tag

Opening tag

Contains the name of the tag and its attributes.

§child: Box<Html>

Child of the tag

Everything between the opening and the closing tag.

§Note

This is always empty if the tag is self-closing.

§

Text(String)

Raw text

Text outside of a tag.

§Examples

In a<strong>b, a and b are Html::Text elements

§

Vec(Box<[Html]>)

List of nodes

§Examples

In a<strong>b, the node is a vector, with Html::Text a, Html::Tag strong Html::Text b.

Implementations§

Source§

impl Html

Source

pub fn filter(self, filter: &Filter) -> Self

Filters html based on a defined filter.

See Filter to learn how to create filters.

Filters allow you to select the portions of the html code you want to keep or remove.

§Returns

The html tree obtains by keeping only the nodes that fulfills the filter.

Source

pub fn find(self, filter: &Filter) -> Self

Finds an html node based on a defined filter.

See Filter to know how to define a filter.

Filters allow you to select the portions of the html code you want to keep or remove.

§Returns

The first node that fulfills the filter.

Source

pub fn to_filtered(&self, filter: &Filter) -> Self

Filters html based on a defined filter.

Equivalent of Html::filter when data is not owned.

Source

pub fn to_found(&self, filter: &Filter) -> Self

Finds an html node based on a defined filter.

Equivalent of Html::find when data is not owned.

Source§

impl Html

Source

pub fn parse(html: &str) -> Result<Self, String>

Parses an HTML string into a Dom tree.

§Errors

This function returns an error when the input HTML’s syntax is invalid.

§Examples
use html_filter::prelude::*;

let html: &str = r#"
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Html sample</title>
    </head>
    <body>
        <p>This is an html sample.</p>
    </body>
</html>
"#;
let tree: Html = Html::parse(html).expect("Invalid HTML");
assert!(format!("{tree}") == html);
Source§

impl Html

Source

pub const fn new() -> Self

Creates an empty Html

Trait Implementations§

Source§

impl Clone for Html

Source§

fn clone(&self) -> Html

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

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Html

Source§

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

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

impl Default for Html

Source§

fn default() -> Html

Returns the “default value” for a type. Read more
Source§

impl Display for Html

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Html

§

impl RefUnwindSafe for Html

§

impl Send for Html

§

impl Sync for Html

§

impl Unpin for Html

§

impl UnwindSafe for Html

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.