Clean up css_parser and main example

This commit is contained in:
Tyler Hallada 2020-04-25 20:37:07 -04:00
parent 5f57b390e2
commit fce50554a3
3 changed files with 10 additions and 32 deletions

View File

@ -1,6 +1,6 @@
use cssparser::{ use cssparser::{
AtRuleParser, AtRuleType, CowRcStr, DeclarationListParser, DeclarationParser, ParseError, AtRuleParser, AtRuleType, CowRcStr, DeclarationListParser, DeclarationParser, ParseError,
Parser, ParserInput, QualifiedRuleParser, RuleListParser, SourceLocation, ToCss, Token, Parser, ParserInput, QualifiedRuleParser, RuleListParser, SourceLocation, ToCss,
TokenSerializationType, TokenSerializationType,
}; };
use std::convert::Into; use std::convert::Into;
@ -220,31 +220,10 @@ impl<'i> DeclarationParser<'i> for CssDeclarationParser {
} }
impl<'i> AtRuleParser<'i> for CssDeclarationParser { impl<'i> AtRuleParser<'i> for CssDeclarationParser {
type PreludeBlock = CssAtRulePrelude; type PreludeBlock = ();
type PreludeNoBlock = CssAtRulePrelude; type PreludeNoBlock = ();
type AtRule = Vec<CssDeclaration>; type AtRule = Vec<CssDeclaration>;
type Error = CssError; type Error = CssError;
fn parse_prelude<'t>(
&mut self,
name: CowRcStr<'i>,
input: &mut Parser<'i, 't>,
) -> Result<AtRuleType<Self::PreludeNoBlock, Self::PreludeBlock>, CssParseError<'i>> {
let mut prelude = String::new();
Ok(AtRuleType::WithBlock(CssAtRulePrelude {
name: name.to_string(),
prelude,
}))
}
fn parse_block<'t>(
&mut self,
prelude: Self::PreludeBlock,
_location: SourceLocation,
input: &mut Parser<'i, 't>,
) -> Result<Self::AtRule, CssParseError<'i>> {
Ok(vec![])
}
} }
pub fn parse_declarations<'i>( pub fn parse_declarations<'i>(

View File

@ -31,13 +31,12 @@ use config::default::DEFAULT_CONFIG;
use sanitizer::Sanitizer; use sanitizer::Sanitizer;
fn main() { fn main() {
let sanitizer = Sanitizer::new(&DEFAULT_CONFIG, vec![&add_single_elements_around_ul]); let sanitizer = Sanitizer::new(&DEFAULT_CONFIG, vec![&add_spacer_elements_around_ul]);
sanitizer sanitizer
.sanitize_fragment(&mut io::stdin(), &mut io::stdout()) .sanitize_fragment(&mut io::stdin(), &mut io::stdout())
.unwrap(); .unwrap();
} }
// TODO: make separate rich and plain transformers
// DONE: add whitelist of tags, remove any not in it // DONE: add whitelist of tags, remove any not in it
// DONE: add whitelist of attributes, remove any not in it // DONE: add whitelist of attributes, remove any not in it
// DONE: add map of tags to attributes, remove any on tag not in the mapped value // DONE: add map of tags to attributes, remove any on tag not in the mapped value
@ -45,16 +44,14 @@ fn main() {
// DONE: strip comments // DONE: strip comments
// DONE: parse style tags and attributes // DONE: parse style tags and attributes
// DONE: add whitelist of CSS properties, remove any not in it // DONE: add whitelist of CSS properties, remove any not in it
// TODO: scope selectors in rich formatter
// TODO: add class attributes to elements in rich formatter
// DONE: separate this out into multiple separate transformers // DONE: separate this out into multiple separate transformers
// TODO: find a way to avoid passing the arena to transformer functions. It's an implementation // TODO: find a way to avoid passing the arena to transformer functions. It's an implementation
// detail that doesn't need to be exposed. Also, it's only needed for creating new elements. // detail that doesn't need to be exposed. Also, it's only needed for creating new elements.
fn add_single_elements_around_ul<'arena>(node: Ref<'arena>, arena: Arena<'arena>) { fn add_spacer_elements_around_ul<'arena>(node: Ref<'arena>, arena: Arena<'arena>) {
if let NodeData::Element { ref name, .. } = node.data { if let NodeData::Element { ref name, .. } = node.data {
if let local_name!("ul") = name.local { if let local_name!("ul") = name.local {
node.insert_before(create_element(arena, "single")); node.insert_before(create_element(arena, "spacer"));
node.insert_after(create_element(arena, "single")); node.insert_after(create_element(arena, "spacer"));
} }
} }
} }

View File

@ -122,9 +122,11 @@ impl<'arena> Sanitizer<'arena> {
self.remove_attributes(node); self.remove_attributes(node);
self.add_attributes(node); self.add_attributes(node);
self.sanitize_attribute_protocols(node); self.sanitize_attribute_protocols(node);
// TODO: save the parsed CSS syntax tree from these methods onto the arena dom so that
// user-created transformers below will have access to modify them without having to
// re-parse.
self.sanitize_style_tag_css(node); self.sanitize_style_tag_css(node);
self.sanitize_style_attribute_css(node); self.sanitize_style_attribute_css(node);
// self.serialize_css_test(node);
for transformer in self.transformers.iter() { for transformer in self.transformers.iter() {
transformer(node, &self.arena); transformer(node, &self.arena);