#[derive(Debug, Clone, Default)]
pub struct CleaningOptions {
pub tags_to_remove: Vec<String>,
pub tags_to_strip: Vec<String>,
pub selectors_to_remove: Vec<String>,
pub prune_empty: bool,
pub empty_tags: Vec<String>,
pub normalize_whitespace: bool,
pub remove_comments: bool,
pub strip_attributes: bool,
pub preserved_attributes: Vec<String>,
}
impl CleaningOptions {
#[must_use]
pub fn builder() -> CleaningOptionsBuilder {
CleaningOptionsBuilder::default()
}
}
#[derive(Debug, Clone, Default)]
pub struct CleaningOptionsBuilder {
options: CleaningOptions,
}
impl CleaningOptionsBuilder {
#[must_use]
pub fn remove_tags(mut self, tags: &[&str]) -> Self {
self.options
.tags_to_remove
.extend(tags.iter().map(|s| (*s).to_string()));
self
}
#[must_use]
pub fn strip_tags(mut self, tags: &[&str]) -> Self {
self.options
.tags_to_strip
.extend(tags.iter().map(|s| (*s).to_string()));
self
}
#[must_use]
pub fn remove_selectors(mut self, selectors: &[&str]) -> Self {
self.options
.selectors_to_remove
.extend(selectors.iter().map(|s| (*s).to_string()));
self
}
#[must_use]
pub fn prune_empty(mut self, enabled: bool) -> Self {
self.options.prune_empty = enabled;
self
}
#[must_use]
pub fn empty_tags(mut self, tags: &[&str]) -> Self {
self.options.empty_tags = tags.iter().map(|s| (*s).to_string()).collect();
self
}
#[must_use]
pub fn normalize_whitespace(mut self, enabled: bool) -> Self {
self.options.normalize_whitespace = enabled;
self
}
#[must_use]
pub fn remove_comments(mut self, enabled: bool) -> Self {
self.options.remove_comments = enabled;
self
}
#[must_use]
pub fn strip_attributes(mut self, enabled: bool) -> Self {
self.options.strip_attributes = enabled;
self
}
#[must_use]
pub fn preserve_attributes(mut self, attrs: &[&str]) -> Self {
self.options.preserved_attributes = attrs.iter().map(|s| (*s).to_string()).collect();
self
}
#[must_use]
pub fn build(self) -> CleaningOptions {
self.options
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_options() {
let options = CleaningOptions::default();
assert!(options.tags_to_remove.is_empty());
assert!(!options.prune_empty);
}
#[test]
fn test_builder() {
let options = CleaningOptions::builder()
.remove_tags(&["script", "style"])
.prune_empty(true)
.build();
assert_eq!(options.tags_to_remove.len(), 2);
assert!(options.prune_empty);
}
}