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