#[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
- remove some nodes: use the
Self::comment
(to remove all comments of the form<!-- comment -->
) orSelf::doctype
(to remove all doctype type nodes, such as<!DOCTYPE html>
) methods. - select some nodes, by searching them with their name (with the
Self::tag_name
method) or attribute.s (with theSelf::attribute_name
andSelf::attribute_value
methods). - select those nodes and their parents, up to a certain generation (cf.
Self::depth
method).
§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.)
impl Filter
Public API for Filter
on node-type-filters (texts, doctypes, comments,
etc.)
Sourcepub const fn all_except_comment(self) -> Self
pub const fn all_except_comment(self) -> Self
Removes the comments
Doctypes and texts are kept, unless said otherwise by the user.
Sourcepub const fn all_except_doctype(self) -> Self
pub const fn all_except_doctype(self) -> Self
Removes the doctypes
Comments and texts are kept, unless said otherwise by the user.
Sourcepub const fn all_except_text(self) -> Self
pub const fn all_except_text(self) -> Self
Removes the texts
Comments and doctypes are kept, unless said otherwise by the user.
Sourcepub const fn comment(self, comment: bool) -> Self
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.
Sourcepub const fn doctype(self, doctype: bool) -> Self
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.
Sourcepub const fn none_except_comment(self) -> Self
pub const fn none_except_comment(self) -> Self
Keeps only the comments
Doctypes and texts are removed, unless said otherwise by the user.
Sourcepub const fn none_except_doctype(self) -> Self
pub const fn none_except_doctype(self) -> Self
Keeps only the doctypes
Comments and texts are removed, unless said otherwise by the user.
Sourcepub const fn none_except_text(self) -> Self
pub const fn none_except_text(self) -> Self
Keeps only the texts
Comments and doctypes are removed, unless said otherwise by the user.
Source§impl Filter
Public API for Filter
on tags and attributes
impl Filter
Public API for Filter
on tags and attributes
Sourcepub fn attribute_name<N: Into<String>>(self, name: N) -> Self
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.
Sourcepub fn attribute_value<N: Into<String>, V: Into<String>>(
self,
name: N,
value: V,
) -> Self
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.
Sourcepub const fn depth(self, depth: usize) -> Self
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>
Sourcepub fn except_attribute_name<N: Into<String>>(self, name: N) -> Self
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.
Sourcepub fn except_attribute_value<N, V>(self, name: N, value: V) -> Self
pub fn except_attribute_value<N, V>(self, name: N, value: V) -> Self
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.
Sourcepub fn except_tag_name<N: Into<String>>(self, name: N) -> Self
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.
Disable all tags, except those explicitly whitelisted