|
| 1 | +use cssparser::*; |
| 2 | +use crate::traits::{Parse, ToCss}; |
| 3 | +use crate::printer::Printer; |
| 4 | +use std::fmt::Write; |
| 5 | +use crate::parser::CssRule; |
| 6 | + |
| 7 | +#[derive(Debug, PartialEq)] |
| 8 | +pub struct SupportsRule { |
| 9 | + pub condition: SupportsCondition, |
| 10 | + pub rules: Vec<CssRule> |
| 11 | +} |
| 12 | + |
| 13 | +impl ToCss for SupportsRule { |
| 14 | + fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write { |
| 15 | + dest.write_str("@supports ")?; |
| 16 | + self.condition.to_css(dest)?; |
| 17 | + dest.whitespace()?; |
| 18 | + dest.write_char('{')?; |
| 19 | + dest.indent(); |
| 20 | + for rule in self.rules.iter() { |
| 21 | + dest.newline()?; |
| 22 | + rule.to_css(dest)?; |
| 23 | + } |
| 24 | + dest.dedent(); |
| 25 | + dest.newline()?; |
| 26 | + dest.write_char('}') |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +#[derive(Debug, PartialEq, Clone)] |
| 31 | +pub enum SupportsCondition { |
| 32 | + Not(Box<SupportsCondition>), |
| 33 | + And(Vec<SupportsCondition>), |
| 34 | + Or(Vec<SupportsCondition>), |
| 35 | + Declaration(String), |
| 36 | + Selector(String), |
| 37 | + // FontTechnology() |
| 38 | + Parens(Box<SupportsCondition>), |
| 39 | + Unknown(String) |
| 40 | +} |
| 41 | + |
| 42 | +impl Parse for SupportsCondition { |
| 43 | + fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ()>> { |
| 44 | + if input.try_parse(|input| input.expect_ident_matching("not")).is_ok() { |
| 45 | + let in_parens = Self::parse_in_parens(input)?; |
| 46 | + return Ok(SupportsCondition::Not(Box::new(in_parens))) |
| 47 | + } |
| 48 | + |
| 49 | + let in_parens = Self::parse_in_parens(input)?; |
| 50 | + let mut expected_type = None; |
| 51 | + let mut conditions = Vec::new(); |
| 52 | + |
| 53 | + loop { |
| 54 | + let condition = input.try_parse(|input| { |
| 55 | + let location = input.current_source_location(); |
| 56 | + let s = input.expect_ident()?; |
| 57 | + let found_type = match_ignore_ascii_case! { &s, |
| 58 | + "and" => 1, |
| 59 | + "or" => 2, |
| 60 | + _ => return Err(location.new_unexpected_token_error( |
| 61 | + cssparser::Token::Ident(s.clone()) |
| 62 | + )) |
| 63 | + }; |
| 64 | + |
| 65 | + if let Some(expected) = expected_type { |
| 66 | + if found_type != expected { |
| 67 | + return Err(location.new_unexpected_token_error( |
| 68 | + cssparser::Token::Ident(s.clone()) |
| 69 | + )) |
| 70 | + } |
| 71 | + } else { |
| 72 | + expected_type = Some(found_type); |
| 73 | + } |
| 74 | + |
| 75 | + Self::parse_in_parens(input) |
| 76 | + }); |
| 77 | + |
| 78 | + if let Ok(condition) = condition { |
| 79 | + if conditions.is_empty() { |
| 80 | + conditions.push(in_parens.clone()) |
| 81 | + } |
| 82 | + conditions.push(condition) |
| 83 | + } else { |
| 84 | + break |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + match expected_type { |
| 89 | + Some(1) => Ok(SupportsCondition::And(conditions)), |
| 90 | + Some(2) => Ok(SupportsCondition::Or(conditions)), |
| 91 | + _ => Ok(in_parens) |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +impl SupportsCondition { |
| 97 | + fn parse_in_parens<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i, ()>> { |
| 98 | + input.skip_whitespace(); |
| 99 | + let location = input.current_source_location(); |
| 100 | + let pos = input.position(); |
| 101 | + match input.next()? { |
| 102 | + Token::Function(ref f) => { |
| 103 | + match_ignore_ascii_case! { &*f, |
| 104 | + "selector" => { |
| 105 | + let res = input.try_parse(|input| { |
| 106 | + input.parse_nested_block(|input| { |
| 107 | + let pos = input.position(); |
| 108 | + input.expect_no_error_token()?; |
| 109 | + Ok(SupportsCondition::Selector(input.slice_from(pos).to_owned())) |
| 110 | + }) |
| 111 | + }); |
| 112 | + if res.is_ok() { |
| 113 | + return res |
| 114 | + } |
| 115 | + }, |
| 116 | + _ => {} |
| 117 | + } |
| 118 | + } |
| 119 | + Token::ParenthesisBlock => { |
| 120 | + let res = input.try_parse(|input| { |
| 121 | + input.parse_nested_block(|input| { |
| 122 | + if let Ok(condition) = input.try_parse(SupportsCondition::parse) { |
| 123 | + return Ok(SupportsCondition::Parens(Box::new(condition))) |
| 124 | + } |
| 125 | + |
| 126 | + let pos = input.position(); |
| 127 | + input.expect_ident()?; |
| 128 | + input.expect_colon()?; |
| 129 | + input.expect_no_error_token()?; |
| 130 | + Ok(SupportsCondition::Declaration(input.slice_from(pos).to_owned())) |
| 131 | + }) |
| 132 | + }); |
| 133 | + if res.is_ok() { |
| 134 | + return res |
| 135 | + } |
| 136 | + } |
| 137 | + t => return Err(location.new_unexpected_token_error(t.clone())), |
| 138 | + }; |
| 139 | + |
| 140 | + input.parse_nested_block(|input| input.expect_no_error_token().map_err(|err| err.into()))?; |
| 141 | + Ok(SupportsCondition::Unknown(input.slice_from(pos).to_owned())) |
| 142 | + } |
| 143 | +} |
| 144 | + |
| 145 | +impl ToCss for SupportsCondition { |
| 146 | + fn to_css<W>(&self, dest: &mut Printer<W>) -> std::fmt::Result where W: std::fmt::Write { |
| 147 | + match self { |
| 148 | + SupportsCondition::Not(condition) => { |
| 149 | + dest.write_str("not ")?; |
| 150 | + condition.to_css(dest) |
| 151 | + } |
| 152 | + SupportsCondition::And(conditions) => { |
| 153 | + let mut first = true; |
| 154 | + for condition in conditions { |
| 155 | + if first { |
| 156 | + first = false; |
| 157 | + } else { |
| 158 | + dest.write_str(" and ")?; |
| 159 | + } |
| 160 | + condition.to_css(dest)?; |
| 161 | + } |
| 162 | + Ok(()) |
| 163 | + } |
| 164 | + SupportsCondition::Or(conditions) => { |
| 165 | + let mut first = true; |
| 166 | + for condition in conditions { |
| 167 | + if first { |
| 168 | + first = false; |
| 169 | + } else { |
| 170 | + dest.write_str(" or ")?; |
| 171 | + } |
| 172 | + condition.to_css(dest)?; |
| 173 | + } |
| 174 | + Ok(()) |
| 175 | + } |
| 176 | + SupportsCondition::Parens(condition) => { |
| 177 | + dest.write_char('(')?; |
| 178 | + condition.to_css(dest)?; |
| 179 | + dest.write_char(')') |
| 180 | + } |
| 181 | + SupportsCondition::Declaration(decl) => { |
| 182 | + dest.write_char('(')?; |
| 183 | + dest.write_str(&decl)?; |
| 184 | + dest.write_char(')') |
| 185 | + } |
| 186 | + SupportsCondition::Selector(sel) => { |
| 187 | + dest.write_str("selector(")?; |
| 188 | + dest.write_str(sel)?; |
| 189 | + dest.write_char(')') |
| 190 | + } |
| 191 | + SupportsCondition::Unknown(unknown) => { |
| 192 | + dest.write_str(&unknown) |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments