Rename Traverser -> Transformer

This commit is contained in:
Tyler Hallada 2020-04-18 18:34:17 -04:00
parent 34ffccc512
commit 28caafb41c
3 changed files with 23 additions and 23 deletions

2
.gitignore vendored
View File

@ -2,3 +2,5 @@
**/*.rs.bk **/*.rs.bk
tags tags
tags.temp tags.temp
tags.lock
Session.vim

View File

@ -31,7 +31,7 @@ mod css_at_rule {
mod arena_dom; mod arena_dom;
mod config; mod config;
mod css_parser; mod css_parser;
mod traverser; mod transformer;
use arena_dom::{create_element, Arena, NodeData, Ref}; use arena_dom::{create_element, Arena, NodeData, Ref};
use config::permissive::{ADD_ATTRIBUTES, ALL_ATTRIBUTES, ATTRIBUTES, ELEMENTS, PROTOCOLS}; use config::permissive::{ADD_ATTRIBUTES, ALL_ATTRIBUTES, ATTRIBUTES, ELEMENTS, PROTOCOLS};
@ -39,10 +39,10 @@ use config::relaxed::{CSS_AT_RULES, CSS_PROPERTIES};
use css_at_rule::CssAtRule; use css_at_rule::CssAtRule;
use css_parser::{parse_css_style_attribute, parse_css_stylesheet, CssRule}; use css_parser::{parse_css_style_attribute, parse_css_stylesheet, CssRule};
use css_property::CssProperty; use css_property::CssProperty;
use traverser::Traverser; use transformer::Transformer;
fn main() { fn main() {
let traverser = Traverser::new( let transformer = Transformer::new(
&should_unwrap_node, &should_unwrap_node,
vec![ vec![
&sanitize_style_tag_css, &sanitize_style_tag_css,
@ -53,8 +53,8 @@ fn main() {
&add_single_elements_around_ul, &add_single_elements_around_ul,
], ],
); );
let root = traverser.parse(&mut io::stdin()).unwrap(); let root = transformer.parse(&mut io::stdin()).unwrap();
traverser.traverse(root); transformer.traverse(root);
serialize(&mut io::stdout(), root, Default::default()).expect("serialization failed") serialize(&mut io::stdout(), root, Default::default()).expect("serialization failed")
} }

View File

@ -4,32 +4,30 @@ use std::io::{Error, Read};
use crate::arena_dom::{html5ever_parse_slice_into_arena, Arena, Node, Ref}; use crate::arena_dom::{html5ever_parse_slice_into_arena, Arena, Node, Ref};
// TODO: I don't love the "Traverser" name. Should maybe come up with something else.
// (it also unwraps nodes and calls transformer functions... does a lot more than traverse)
// TODO: What are the performance implications of using a vec of boxed closures instead of one // TODO: What are the performance implications of using a vec of boxed closures instead of one
// transformer function who's size is known at compile time (U: Fn(Ref<'arena>) -> bool)? // transformer function who's size is known at compile time (U: Fn(Ref<'arena>) -> bool)?
// TODO: how to integrate CSS parsing and transforming? // TODO: how to integrate CSS parsing and transforming?
pub struct Traverser<'arena, T> pub struct Transformer<'arena, T>
where where
T: Fn(Ref) -> bool, T: Fn(Ref) -> bool,
{ {
arena: typed_arena::Arena<Node<'arena>>, arena: typed_arena::Arena<Node<'arena>>,
should_unwrap: T, should_unwrap: T,
transformers: Vec<&'arena dyn Fn(Ref<'arena>, Arena<'arena>)>, transformer_fns: Vec<&'arena dyn Fn(Ref<'arena>, Arena<'arena>)>,
} }
impl<'arena, T> Traverser<'arena, T> impl<'arena, T> Transformer<'arena, T>
where where
T: Fn(Ref) -> bool, T: Fn(Ref) -> bool,
{ {
pub fn new( pub fn new(
should_unwrap: T, should_unwrap: T,
transformers: Vec<&'arena dyn Fn(Ref<'arena>, Arena<'arena>)>, transformers: Vec<&'arena dyn Fn(Ref<'arena>, Arena<'arena>)>,
) -> Traverser<'arena, T> { ) -> Transformer<'arena, T> {
Traverser { Transformer {
arena: typed_arena::Arena::new(), arena: typed_arena::Arena::new(),
should_unwrap, should_unwrap,
transformers, transformer_fns: transformers,
} }
} }
@ -48,7 +46,7 @@ where
} }
} }
for transformer in self.transformers.iter() { for transformer in self.transformer_fns.iter() {
transformer(node, &self.arena); transformer(node, &self.arena);
} }
@ -137,16 +135,16 @@ mod test {
#[test] #[test]
fn traversal() { fn traversal() {
let traverser = Traverser::new(|_| false, vec![&|_, _| {}]); let transformer = Transformer::new(|_| false, vec![&|_, _| {}]);
let mut mock_data = MockRead::new("<div></div>"); let mut mock_data = MockRead::new("<div></div>");
let root = traverser.parse(&mut mock_data).unwrap(); let root = transformer.parse(&mut mock_data).unwrap();
traverser.traverse(root); transformer.traverse(root);
assert_serialized_html_eq(root, "<html><head></head><body><div></div></body></html>"); assert_serialized_html_eq(root, "<html><head></head><body><div></div></body></html>");
} }
#[test] #[test]
fn unwraps_element() { fn unwraps_element() {
let traverser = Traverser::new( let transformer = Transformer::new(
|node| { |node| {
if let NodeData::Element { ref name, .. } = node.data { if let NodeData::Element { ref name, .. } = node.data {
return name.local == local_name!("div"); return name.local == local_name!("div");
@ -156,14 +154,14 @@ mod test {
vec![&|_, _| {}], vec![&|_, _| {}],
); );
let mut mock_data = MockRead::new("<div></div>"); let mut mock_data = MockRead::new("<div></div>");
let root = traverser.parse(&mut mock_data).unwrap(); let root = transformer.parse(&mut mock_data).unwrap();
traverser.traverse(root); transformer.traverse(root);
assert_serialized_html_eq(root, "<html><head></head><body></body></html>"); assert_serialized_html_eq(root, "<html><head></head><body></body></html>");
} }
#[test] #[test]
fn adds_element() { fn adds_element() {
let traverser = Traverser::new( let transformer = Transformer::new(
|_| false, |_| false,
vec![&|node, arena| { vec![&|node, arena| {
if let NodeData::Element { ref name, .. } = node.data { if let NodeData::Element { ref name, .. } = node.data {
@ -174,8 +172,8 @@ mod test {
}], }],
); );
let mut mock_data = MockRead::new("<div></div>"); let mut mock_data = MockRead::new("<div></div>");
let root = traverser.parse(&mut mock_data).unwrap(); let root = transformer.parse(&mut mock_data).unwrap();
traverser.traverse(root); transformer.traverse(root);
assert_serialized_html_eq( assert_serialized_html_eq(
root, root,
"<html><head></head><body><div></div><span></span></body></html>", "<html><head></head><body><div></div><span></span></body></html>",