|
| 1 | +pub struct CSSRuleListParser; |
| 2 | +struct CSSDeclarationListParser; |
| 3 | + |
| 4 | +pub type Declaration = (String, String); |
| 5 | +pub type QualifiedRule = (String, Vec<Declaration>); |
| 6 | + |
| 7 | +fn exhaust(input: &mut cssparser::Parser) -> String { |
| 8 | + let start = input.position(); |
| 9 | + while input.next().is_ok() {} |
| 10 | + input.slice_from(start).to_string() |
| 11 | +} |
| 12 | + |
| 13 | +impl<'i> cssparser::QualifiedRuleParser<'i> for CSSRuleListParser { |
| 14 | + type Prelude = String; |
| 15 | + type QualifiedRule = QualifiedRule; |
| 16 | + type Error = (); |
| 17 | + |
| 18 | + fn parse_prelude<'t>( |
| 19 | + &mut self, |
| 20 | + input: &mut cssparser::Parser<'i, 't>, |
| 21 | + ) -> Result<Self::Prelude, cssparser::ParseError<'i, Self::Error>> { |
| 22 | + let _ = input; |
| 23 | + Ok(exhaust(input)) |
| 24 | + } |
| 25 | + |
| 26 | + fn parse_block<'t>( |
| 27 | + &mut self, |
| 28 | + prelude: Self::Prelude, |
| 29 | + _: cssparser::SourceLocation, |
| 30 | + input: &mut cssparser::Parser<'i, 't>, |
| 31 | + ) -> Result<Self::QualifiedRule, cssparser::ParseError<'i, Self::Error>> { |
| 32 | + let parser = cssparser::DeclarationListParser::new(input, CSSDeclarationListParser); |
| 33 | + let mut declarations = vec![]; |
| 34 | + |
| 35 | + for item in parser { |
| 36 | + if let Ok(declaration) = item { |
| 37 | + declarations.push(declaration); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + Ok((prelude, declarations)) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +impl<'i> cssparser::DeclarationParser<'i> for CSSDeclarationListParser { |
| 46 | + type Declaration = Declaration; |
| 47 | + type Error = (); |
| 48 | + |
| 49 | + fn parse_value<'t>( |
| 50 | + &mut self, |
| 51 | + name: cssparser::CowRcStr<'i>, |
| 52 | + input: &mut cssparser::Parser<'i, 't>, |
| 53 | + ) -> Result<Self::Declaration, cssparser::ParseError<'i, Self::Error>> { |
| 54 | + Ok((name.to_string(), exhaust(input))) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +impl cssparser::AtRuleParser<'_> for CSSRuleListParser { |
| 59 | + type PreludeNoBlock = String; |
| 60 | + type PreludeBlock = String; |
| 61 | + type AtRule = QualifiedRule; |
| 62 | + type Error = (); |
| 63 | +} |
| 64 | + |
| 65 | +impl cssparser::AtRuleParser<'_> for CSSDeclarationListParser { |
| 66 | + type PreludeNoBlock = String; |
| 67 | + type PreludeBlock = String; |
| 68 | + type AtRule = Declaration; |
| 69 | + type Error = (); |
| 70 | +} |
| 71 | + |
| 72 | +pub struct CSSParser<'i, 't> { |
| 73 | + input: cssparser::Parser<'i, 't>, |
| 74 | +} |
| 75 | + |
| 76 | +impl<'i: 't, 't> CSSParser<'i, 't> { |
| 77 | + pub fn new(css: &'t mut cssparser::ParserInput<'i>) -> CSSParser<'i, 't> { |
| 78 | + CSSParser { |
| 79 | + input: cssparser::Parser::new(css), |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + pub fn parse<'a>(&'a mut self) -> cssparser::RuleListParser<'i, 't, 'a, CSSRuleListParser> { |
| 84 | + cssparser::RuleListParser::new_for_stylesheet(&mut self.input, CSSRuleListParser) |
| 85 | + } |
| 86 | +} |
0 commit comments