Struct Filter

Source
#[non_exhaustive]
pub struct Filter { /* private fields */ }
Expand description

Filters to select the wanted elements of an Html tree.

The Filter structures allows you to

§Examples

#![allow(unused)]

use html_filter::prelude::*;

Filter::new().comment(false).doctype(false); // Removes comments (`<!---->`) and doctype tags (`<!DOCTYPE html>`).
Filter::new().tag_name("a"); // Lists all the `<a>` tags and their content.
Filter::new().attribute_name("onClick"); // Lists all the tags with a `onClick` attribute.
Filter::new().attribute_value("id", "first-title"); // Get the element of `id` `"first-title`
Filter::new().tag_name("li").depth(1); // Lists all the `<li>` tags and their parent (usually `ol` or `ul`).

Implementations§

Source§

impl Filter

Public API for Filter on node-type-filters (texts, doctypes, comments, etc.)

Source

pub const fn all(self, all: bool) -> Self

Keeps everything: comments, doctypes and texts

Source

pub const fn all_except_comment(self) -> Self

Removes the comments

Doctypes and texts are kept, unless said otherwise by the user.

Source

pub const fn all_except_doctype(self) -> Self

Removes the doctypes

Comments and texts are kept, unless said otherwise by the user.

Source

pub const fn all_except_text(self) -> Self

Removes the texts

Comments and doctypes are kept, unless said otherwise by the user.

Source

pub const fn comment(self, comment: bool) -> Self

Sets the filter for comments

If comment is set to true (default), comments are kept. If comment is set to false, comments are removed.

See Filter for usage information.

Source

pub const fn doctype(self, doctype: bool) -> Self

Sets the filter for doctype tags

If doctype is set to true (default), doctype tags are kept. If doctype is set to false, doctype tags are removed.

See Filter for usage information.

Source

pub const fn none_except_comment(self) -> Self

Keeps only the comments

Doctypes and texts are removed, unless said otherwise by the user.

Source

pub const fn none_except_doctype(self) -> Self

Keeps only the doctypes

Comments and texts are removed, unless said otherwise by the user.

Source

pub const fn none_except_text(self) -> Self

Keeps only the texts

Comments and doctypes are removed, unless said otherwise by the user.

Source

pub const fn text(self, text: bool) -> Self

Filters texts

  • If text is set to true (default), all texts are kept.
  • If text is set to false, all texts are removed.

See Filter for usage information.

Source§

impl Filter

Public API for Filter on tags and attributes

Source

pub fn attribute_name<N: Into<String>>(self, name: N) -> Self

Specifies the name of an attribute in the wanted tags.

This matches only tag attributes that don’t have any value, such as enabled in

<button enabled type="submit" />

See Filter for usage information.

Source

pub fn attribute_value<N: Into<String>, V: Into<String>>( self, name: N, value: V, ) -> Self

Specifies the value of an attribute in the wanted tags.

This matches only tag attributes that have the correct value for the given name.

See Filter for usage information.

Source

pub const fn depth(self, depth: usize) -> Self

Specifies the depth of the desired nodes.

The depth means at what depth the nodes must be kept according to the filter. for this node. This allows you to search for a node, and select the node, but also some of its ancestors, up to the chosen depth. For instance, a depth of 0 means you only keep the tag, but a depth of 1 means you keep the wanted tag, but it’s parent and all its children.

§Examples

For example, let’s consider this HTML code:

<main>
    <nav>
        <!-- Navigation menu -->
        <ul>
            <li href="first">First link</li>
            <li href="second">Second link</li>
            <li href="third">Third link</li>
        </ul>
    </nav>
</main>

For this piece of HTML code, the filter

#![allow(unused)]
html_filter::prelude::Filter::new()
    .attribute_value("href", "second")
    .depth(0);

will return:

<li href="second">Second link</li>

;

#![allow(unused)]
html_filter::prelude::Filter::new()
    .attribute_value("href", "second")
    .depth(1);

will return (note that the other children were kept):

<ul>
    <li href="first">First link</li>
    <li href="second">Second link</li>
    <li href="third">Third link</li>
</ul>

;

#![allow(unused)]
html_filter::prelude::Filter::new()
    .attribute_value("href", "second")
    .depth(2);

will return (note that even the comment was kept, if you want to remove the comment, you must add .comment(false) to the filter):

<nav>
    <!-- Navigation menu -->
    <ul>
        <li href="first">First link</li>
        <li href="second">Second link</li>
        <li href="third">Third link</li>
    </ul>
</nav>
Source

pub fn except_attribute_name<N: Into<String>>(self, name: N) -> Self

Specifies the name of an attribute in the tags that must be dismissed.

This matches only tag attributes that don’t have any value, such as enabled in

<button enabled type="submit" />

See Filter for usage information.

Source

pub fn except_attribute_value<N, V>(self, name: N, value: V) -> Self
where N: Into<String>, V: Into<String>,

Specifies the value of an attribute in the tags that must be dismissed.

This matches only tag attributes that have the correct value for the given name.

See Filter for usage information.

Source

pub fn except_tag_name<N: Into<String>>(self, name: N) -> Self

Specifies the tag name of the wanted tags.

See Filter for usage information.

Source

pub fn new() -> Self

Creates a default Filter

By default, comments and doctypes are allowed, however no node is wanted, so filtering on a default filter will return an empty Html.

§Examples
use html_filter::prelude::*;

let _filter: Filter = Filter::new();
Source

pub const fn no_tags(self) -> Self

Disable all tags, except those explicitly whitelisted

Source

pub fn tag_name<N: Into<String>>(self, name: N) -> Self

Specifies the tag name of the wanted tags.

See Filter for usage information.

Trait Implementations§

Source§

impl Debug for Filter

Source§

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

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

impl Default for Filter

Source§

fn default() -> Filter

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

Auto Trait Implementations§

§

impl Freeze for Filter

§

impl RefUnwindSafe for Filter

§

impl Send for Filter

§

impl Sync for Filter

§

impl Unpin for Filter

§

impl UnwindSafe for Filter

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> 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, 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.