pub enum Html {
Comment(String),
Doctype {
name: String,
attr: Option<String>,
},
Empty,
Tag {
tag: Tag,
child: Box<Self>,
},
Text(String),
Vec(Box<[Self]>),
}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::*;
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§
Comment(String)
Doctype
Fields
Empty
Empty html tree
Corresponds to an empty string
Tag
Fields
Text(String)
Vec(Box<[Self]>)
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
impl Html
Sourcepub fn to_filtered(&self, filter: &Filter) -> Self
pub fn to_filtered(&self, filter: &Filter) -> Self
Filters html based on a defined filter.
Equivalent of Html::filter when data is not owned.
Sourcepub fn to_found(&self, filter: &Filter) -> Self
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
impl Html
Sourcepub fn parse(html: &str) -> Result<Self, String>
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::*;
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_eq!(format!("{tree}"), html);Source§impl Html
impl Html
Sourcepub const fn as_comment(&self) -> Option<&str>
pub const fn as_comment(&self) -> Option<&str>
Returns the text of the comment, if this node is a comment.
§Examples
use html_filter::*;
assert_eq!(Html::parse("<!-- some comment -->").unwrap().as_comment(), Some(" some comment "));
assert_eq!(Html::parse("<div>a</div>").unwrap().as_comment(), None);
assert_eq!(Html::parse("not <!-- at --> top-level").unwrap().as_comment(), None);Sourcepub const fn as_doctype(&self) -> Option<(&str, Option<&str>)>
pub const fn as_doctype(&self) -> Option<(&str, Option<&str>)>
Returns the text of the doctype, if this node is a doctype.
§Examples
use html_filter::*;
assert_eq!(
Html::parse("<!doctype html>").unwrap().as_doctype(),
Some(("doctype", Some("html")))
);
assert_eq!(Html::parse("<!xml>").unwrap().as_doctype(), Some(("xml", None)));
assert_eq!(Html::parse("<div>a</div>").unwrap().as_doctype(), None);
assert_eq!(Html::parse("<!not at> top-level").unwrap().as_doctype(), None);Sourcepub const fn as_tag(&self) -> Option<(&Tag, &Self)>
pub const fn as_tag(&self) -> Option<(&Tag, &Self)>
Returns the tag, if this node is a tag.
§Examples
use html_filter::*;
let div = Html::parse(r#"<div href="/">a</div>"#).unwrap();
assert_eq!(div.as_tag().unwrap().0.as_name(), "div");
assert_eq!(div.as_tag().unwrap().0.find_attr_value("href"), Some(&"/".to_owned()));
assert_eq!(Html::parse("<div>a</div>").unwrap().as_tag().unwrap().1.as_text(), Some("a"));
assert_eq!(Html::parse("<p>a</p><p>b</p>").unwrap().as_tag(), None);Sourcepub const fn as_text(&self) -> Option<&str>
pub const fn as_text(&self) -> Option<&str>
Returns the text, if this node is a text.
§Examples
use html_filter::*;
assert_eq!(Html::parse("text").unwrap().as_text(), Some("text"));
assert_eq!(Html::parse("<div>a</div>").unwrap().as_text(), None);
assert_eq!(Html::parse("<div>a</div>").unwrap().as_tag().unwrap().1.as_text(), Some("a"));
assert_eq!(Html::parse("<p>a</p><p>b</p>").unwrap().as_text(), None);Sourcepub const fn as_vec(&self) -> Option<&[Self]>
pub const fn as_vec(&self) -> Option<&[Self]>
Returns the vec, if this isn’t a node but a list of nodes.
§Examples
use html_filter::*;
assert_eq!(Html::parse("<div>a</div>").unwrap().as_vec(), None);
let html = Html::parse("<p>a</p>text<!-- comment-->").unwrap();
let vec = html.as_vec().unwrap();
assert_eq!(vec[0].as_tag().unwrap().0.as_name(), "p");
assert_eq!(vec[0].as_tag().unwrap().1.as_text(), Some("a"));
assert_eq!(vec[1].as_text(), Some("text"));
assert_eq!(vec[2].as_comment(), Some(" comment"));