Rename Traverser -> Transformer
This commit is contained in:
parent
34ffccc512
commit
28caafb41c
2
.gitignore
vendored
2
.gitignore
vendored
@ -2,3 +2,5 @@
|
||||
**/*.rs.bk
|
||||
tags
|
||||
tags.temp
|
||||
tags.lock
|
||||
Session.vim
|
||||
|
10
src/main.rs
10
src/main.rs
@ -31,7 +31,7 @@ mod css_at_rule {
|
||||
mod arena_dom;
|
||||
mod config;
|
||||
mod css_parser;
|
||||
mod traverser;
|
||||
mod transformer;
|
||||
|
||||
use arena_dom::{create_element, Arena, NodeData, Ref};
|
||||
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_parser::{parse_css_style_attribute, parse_css_stylesheet, CssRule};
|
||||
use css_property::CssProperty;
|
||||
use traverser::Traverser;
|
||||
use transformer::Transformer;
|
||||
|
||||
fn main() {
|
||||
let traverser = Traverser::new(
|
||||
let transformer = Transformer::new(
|
||||
&should_unwrap_node,
|
||||
vec![
|
||||
&sanitize_style_tag_css,
|
||||
@ -53,8 +53,8 @@ fn main() {
|
||||
&add_single_elements_around_ul,
|
||||
],
|
||||
);
|
||||
let root = traverser.parse(&mut io::stdin()).unwrap();
|
||||
traverser.traverse(root);
|
||||
let root = transformer.parse(&mut io::stdin()).unwrap();
|
||||
transformer.traverse(root);
|
||||
serialize(&mut io::stdout(), root, Default::default()).expect("serialization failed")
|
||||
}
|
||||
|
||||
|
@ -4,32 +4,30 @@ use std::io::{Error, Read};
|
||||
|
||||
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
|
||||
// transformer function who's size is known at compile time (U: Fn(Ref<'arena>) -> bool)?
|
||||
// TODO: how to integrate CSS parsing and transforming?
|
||||
pub struct Traverser<'arena, T>
|
||||
pub struct Transformer<'arena, T>
|
||||
where
|
||||
T: Fn(Ref) -> bool,
|
||||
{
|
||||
arena: typed_arena::Arena<Node<'arena>>,
|
||||
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
|
||||
T: Fn(Ref) -> bool,
|
||||
{
|
||||
pub fn new(
|
||||
should_unwrap: T,
|
||||
transformers: Vec<&'arena dyn Fn(Ref<'arena>, Arena<'arena>)>,
|
||||
) -> Traverser<'arena, T> {
|
||||
Traverser {
|
||||
) -> Transformer<'arena, T> {
|
||||
Transformer {
|
||||
arena: typed_arena::Arena::new(),
|
||||
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);
|
||||
}
|
||||
|
||||
@ -137,16 +135,16 @@ mod test {
|
||||
|
||||
#[test]
|
||||
fn traversal() {
|
||||
let traverser = Traverser::new(|_| false, vec![&|_, _| {}]);
|
||||
let transformer = Transformer::new(|_| false, vec![&|_, _| {}]);
|
||||
let mut mock_data = MockRead::new("<div></div>");
|
||||
let root = traverser.parse(&mut mock_data).unwrap();
|
||||
traverser.traverse(root);
|
||||
let root = transformer.parse(&mut mock_data).unwrap();
|
||||
transformer.traverse(root);
|
||||
assert_serialized_html_eq(root, "<html><head></head><body><div></div></body></html>");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn unwraps_element() {
|
||||
let traverser = Traverser::new(
|
||||
let transformer = Transformer::new(
|
||||
|node| {
|
||||
if let NodeData::Element { ref name, .. } = node.data {
|
||||
return name.local == local_name!("div");
|
||||
@ -156,14 +154,14 @@ mod test {
|
||||
vec![&|_, _| {}],
|
||||
);
|
||||
let mut mock_data = MockRead::new("<div></div>");
|
||||
let root = traverser.parse(&mut mock_data).unwrap();
|
||||
traverser.traverse(root);
|
||||
let root = transformer.parse(&mut mock_data).unwrap();
|
||||
transformer.traverse(root);
|
||||
assert_serialized_html_eq(root, "<html><head></head><body></body></html>");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn adds_element() {
|
||||
let traverser = Traverser::new(
|
||||
let transformer = Transformer::new(
|
||||
|_| false,
|
||||
vec![&|node, arena| {
|
||||
if let NodeData::Element { ref name, .. } = node.data {
|
||||
@ -174,8 +172,8 @@ mod test {
|
||||
}],
|
||||
);
|
||||
let mut mock_data = MockRead::new("<div></div>");
|
||||
let root = traverser.parse(&mut mock_data).unwrap();
|
||||
traverser.traverse(root);
|
||||
let root = transformer.parse(&mut mock_data).unwrap();
|
||||
transformer.traverse(root);
|
||||
assert_serialized_html_eq(
|
||||
root,
|
||||
"<html><head></head><body><div></div><span></span></body></html>",
|
Loading…
Reference in New Issue
Block a user