Skip to content

Parsing full html documents #202

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ pub struct Builder<'a> {
strip_comments: bool,
id_prefix: Option<&'a str>,
generic_attribute_prefixes: Option<HashSet<&'a str>>,
is_document: bool,
}

impl<'a> Default for Builder<'a> {
Expand Down Expand Up @@ -486,6 +487,7 @@ impl<'a> Default for Builder<'a> {
strip_comments: true,
id_prefix: None,
generic_attribute_prefixes: None,
is_document: false,
}
}
}
Expand Down Expand Up @@ -1705,6 +1707,17 @@ impl<'a> Builder<'a> {
}
}

/// Use this to parse a full document instead of a document fragment (like a div)
pub fn parse_as_document(&mut self) -> &mut Self {
// TODO: expand on this
self.add_tags(["html", "head", "link", "title", "meta", "body"])
.add_tag_attributes("meta", ["name", "content"])
.add_tag_attributes("html", ["lang"]);

self.is_document = true;
self
}

/// Sanitizes an HTML fragment in a string according to the configured options.
///
/// # Examples
Expand All @@ -1725,7 +1738,11 @@ impl<'a> Builder<'a> {
/// # }
/// # fn main() { do_main().unwrap() }
pub fn clean(&self, src: &str) -> Document {
let parser = Self::make_parser();
let parser = if self.is_document {
html::parse_document(RcDom::default(), html::ParseOpts::default())
} else {
Self::make_parser()
};
let dom = parser.one(src);
self.clean_dom(dom)
}
Expand Down Expand Up @@ -1788,7 +1805,10 @@ impl<'a> Builder<'a> {
.is_none());
}
for tag_name in &self.clean_content_tags {
assert!(!self.tags.contains(tag_name), "`{tag_name}` appears in `clean_content_tags` and in `tags` at the same time");
assert!(
!self.tags.contains(tag_name),
"`{tag_name}` appears in `clean_content_tags` and in `tags` at the same time"
);
assert!(!self.tag_attributes.contains_key(tag_name), "`{tag_name}` appears in `clean_content_tags` and in `tag_attributes` at the same time");
}
let body = {
Expand Down