mod.rs (2296B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ 4 5 use cssparser::{Parser, ParserInput}; 6 use style::context::QuirksMode; 7 use style::parser::ParserContext; 8 use style::stylesheets::{CssRuleType, Origin}; 9 use style_traits::{ParseError, ParsingMode}; 10 11 fn parse<T, F>(f: F, s: &'static str) -> Result<T, ParseError<'static>> 12 where 13 F: for<'t> Fn(&ParserContext, &mut Parser<'static, 't>) -> Result<T, ParseError<'static>>, 14 { 15 let mut input = ParserInput::new(s); 16 parse_input(f, &mut input) 17 } 18 19 fn parse_input<'i: 't, 't, T, F>(f: F, input: &'t mut ParserInput<'i>) -> Result<T, ParseError<'i>> 20 where 21 F: Fn(&ParserContext, &mut Parser<'i, 't>) -> Result<T, ParseError<'i>>, 22 { 23 let url = ::servo_url::ServoUrl::parse("http://localhost").unwrap(); 24 let context = ParserContext::new( 25 Origin::Author, 26 &url, 27 Some(CssRuleType::Style), 28 ParsingMode::DEFAULT, 29 QuirksMode::NoQuirks, 30 None, 31 None, 32 ); 33 let mut parser = Parser::new(input); 34 f(&context, &mut parser) 35 } 36 37 macro_rules! assert_roundtrip_with_context { 38 ($fun:expr, $string:expr) => { 39 assert_roundtrip_with_context!($fun, $string, $string); 40 }; 41 ($fun:expr, $input:expr, $output:expr) => {{ 42 let serialized = parse( 43 |context, i| { 44 let parsed = $fun(context, i).expect(&format!("Failed to parse {}", $input)); 45 let serialized = ToCss::to_css_string(&parsed); 46 assert_eq!(serialized, $output); 47 Ok(serialized) 48 }, 49 $input, 50 ) 51 .unwrap(); 52 53 let mut input = ::cssparser::ParserInput::new(&serialized); 54 let unwrapped = parse_input( 55 |context, i| { 56 let re_parsed = 57 $fun(context, i).expect(&format!("Failed to parse serialization {}", $input)); 58 let re_serialized = ToCss::to_css_string(&re_parsed); 59 assert_eq!(serialized, re_serialized); 60 Ok(()) 61 }, 62 &mut input, 63 ) 64 .unwrap(); 65 unwrapped 66 }}; 67 } 68 69 mod scaffolding; 70 mod serialization;