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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

//! [@supports rules](https://drafts.csswg.org/css-conditional-3/#at-supports)

use crate::font_face::{FontFaceSourceFormatKeyword, FontFaceSourceTechFlags};
use crate::parser::ParserContext;
use crate::properties::{PropertyDeclaration, PropertyId, SourcePropertyDeclaration};
use crate::selector_parser::{SelectorImpl, SelectorParser};
use crate::shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked};
use crate::shared_lock::{SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard};
use crate::str::CssStringWriter;
use crate::stylesheets::{CssRuleType, CssRules};
use cssparser::parse_important;
use cssparser::{Delimiter, Parser, SourceLocation, Token};
use cssparser::{ParseError as CssParseError, ParserInput};
#[cfg(feature = "gecko")]
use malloc_size_of::{MallocSizeOfOps, MallocUnconditionalShallowSizeOf};
use selectors::parser::{Selector, SelectorParseErrorKind};
use servo_arc::Arc;
use std::fmt::{self, Write};
use std::str;
use style_traits::{CssWriter, ParseError, StyleParseErrorKind, ToCss};

/// An [`@supports`][supports] rule.
///
/// [supports]: https://drafts.csswg.org/css-conditional-3/#at-supports
#[derive(Debug, ToShmem)]
pub struct SupportsRule {
    /// The parsed condition
    pub condition: SupportsCondition,
    /// Child rules
    pub rules: Arc<Locked<CssRules>>,
    /// The result of evaluating the condition
    pub enabled: bool,
    /// The line and column of the rule's source code.
    pub source_location: SourceLocation,
}

impl SupportsRule {
    /// Measure heap usage.
    #[cfg(feature = "gecko")]
    pub fn size_of(&self, guard: &SharedRwLockReadGuard, ops: &mut MallocSizeOfOps) -> usize {
        // Measurement of other fields may be added later.
        self.rules.unconditional_shallow_size_of(ops) +
            self.rules.read_with(guard).size_of(guard, ops)
    }
}

impl ToCssWithGuard for SupportsRule {
    fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut CssStringWriter) -> fmt::Result {
        dest.write_str("@supports ")?;
        self.condition.to_css(&mut CssWriter::new(dest))?;
        self.rules.read_with(guard).to_css_block(guard, dest)
    }
}

impl DeepCloneWithLock for SupportsRule {
    fn deep_clone_with_lock(
        &self,
        lock: &SharedRwLock,
        guard: &SharedRwLockReadGuard,
        params: &DeepCloneParams,
    ) -> Self {
        let rules = self.rules.read_with(guard);
        SupportsRule {
            condition: self.condition.clone(),
            rules: Arc::new(lock.wrap(rules.deep_clone_with_lock(lock, guard, params))),
            enabled: self.enabled,
            source_location: self.source_location.clone(),
        }
    }
}

/// An @supports condition
///
/// <https://drafts.csswg.org/css-conditional-3/#at-supports>
#[derive(Clone, Debug, ToShmem)]
pub enum SupportsCondition {
    /// `not (condition)`
    Not(Box<SupportsCondition>),
    /// `(condition)`
    Parenthesized(Box<SupportsCondition>),
    /// `(condition) and (condition) and (condition) ..`
    And(Vec<SupportsCondition>),
    /// `(condition) or (condition) or (condition) ..`
    Or(Vec<SupportsCondition>),
    /// `property-ident: value` (value can be any tokens)
    Declaration(Declaration),
    /// A `selector()` function.
    Selector(RawSelector),
    /// `font-format(<font-format>)`
    FontFormat(FontFaceSourceFormatKeyword),
    /// `font-tech(<font-tech>)`
    FontTech(FontFaceSourceTechFlags),
    /// `(any tokens)` or `func(any tokens)`
    FutureSyntax(String),
}

impl SupportsCondition {
    /// Parse a condition
    ///
    /// <https://drafts.csswg.org/css-conditional/#supports_condition>
    pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
        if input.try_parse(|i| i.expect_ident_matching("not")).is_ok() {
            let inner = SupportsCondition::parse_in_parens(input)?;
            return Ok(SupportsCondition::Not(Box::new(inner)));
        }

        let in_parens = SupportsCondition::parse_in_parens(input)?;

        let location = input.current_source_location();
        let (keyword, wrapper) = match input.next() {
            // End of input
            Err(..) => return Ok(in_parens),
            Ok(&Token::Ident(ref ident)) => {
                match_ignore_ascii_case! { &ident,
                    "and" => ("and", SupportsCondition::And as fn(_) -> _),
                    "or" => ("or", SupportsCondition::Or as fn(_) -> _),
                    _ => return Err(location.new_custom_error(SelectorParseErrorKind::UnexpectedIdent(ident.clone())))
                }
            },
            Ok(t) => return Err(location.new_unexpected_token_error(t.clone())),
        };

        let mut conditions = Vec::with_capacity(2);
        conditions.push(in_parens);
        loop {
            conditions.push(SupportsCondition::parse_in_parens(input)?);
            if input
                .try_parse(|input| input.expect_ident_matching(keyword))
                .is_err()
            {
                // Did not find the expected keyword.
                // If we found some other token, it will be rejected by
                // `Parser::parse_entirely` somewhere up the stack.
                return Ok(wrapper(conditions));
            }
        }
    }

    /// Parses a functional supports condition.
    fn parse_functional<'i, 't>(
        function: &str,
        input: &mut Parser<'i, 't>,
    ) -> Result<Self, ParseError<'i>> {
        match_ignore_ascii_case! { function,
            "selector" => {
                let pos = input.position();
                consume_any_value(input)?;
                Ok(SupportsCondition::Selector(RawSelector(
                    input.slice_from(pos).to_owned()
                )))
            },
            "font-format" if static_prefs::pref!("layout.css.font-tech.enabled") => {
                let kw = FontFaceSourceFormatKeyword::parse(input)?;
                Ok(SupportsCondition::FontFormat(kw))
            },
            "font-tech" if static_prefs::pref!("layout.css.font-tech.enabled") => {
                let flag = FontFaceSourceTechFlags::parse_one(input)?;
                Ok(SupportsCondition::FontTech(flag))
            },
            _ => {
                Err(input.new_custom_error(StyleParseErrorKind::UnspecifiedError))
            },
        }
    }

    /// Parses an `@import` condition as per
    /// https://drafts.csswg.org/css-cascade-5/#typedef-import-conditions
    pub fn parse_for_import<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
        input.expect_function_matching("supports")?;
        input.parse_nested_block(parse_condition_or_declaration)
    }

    /// <https://drafts.csswg.org/css-conditional-3/#supports_condition_in_parens>
    fn parse_in_parens<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Self, ParseError<'i>> {
        // Whitespace is normally taken care of in `Parser::next`, but we want to not include it in
        // `pos` for the SupportsCondition::FutureSyntax cases.
        input.skip_whitespace();
        let pos = input.position();
        let location = input.current_source_location();
        match *input.next()? {
            Token::ParenthesisBlock => {
                let nested = input
                    .try_parse(|input| input.parse_nested_block(parse_condition_or_declaration));
                if let Ok(nested) = nested {
                    return Ok(Self::Parenthesized(Box::new(nested)));
                }
            },
            Token::Function(ref ident) => {
                let ident = ident.clone();
                let nested = input.try_parse(|input| {
                    input.parse_nested_block(|input| {
                        SupportsCondition::parse_functional(&ident, input)
                    })
                });
                if nested.is_ok() {
                    return nested;
                }
            },
            ref t => return Err(location.new_unexpected_token_error(t.clone())),
        }
        input.parse_nested_block(consume_any_value)?;
        Ok(SupportsCondition::FutureSyntax(
            input.slice_from(pos).to_owned(),
        ))
    }

    /// Evaluate a supports condition
    pub fn eval(&self, cx: &ParserContext) -> bool {
        match *self {
            SupportsCondition::Not(ref cond) => !cond.eval(cx),
            SupportsCondition::Parenthesized(ref cond) => cond.eval(cx),
            SupportsCondition::And(ref vec) => vec.iter().all(|c| c.eval(cx)),
            SupportsCondition::Or(ref vec) => vec.iter().any(|c| c.eval(cx)),
            SupportsCondition::Declaration(ref decl) => decl.eval(cx),
            SupportsCondition::Selector(ref selector) => selector.eval(cx),
            SupportsCondition::FontFormat(ref format) => eval_font_format(format),
            SupportsCondition::FontTech(ref tech) => eval_font_tech(tech),
            SupportsCondition::FutureSyntax(_) => false,
        }
    }
}

#[cfg(feature = "gecko")]
fn eval_font_format(kw: &FontFaceSourceFormatKeyword) -> bool {
    use crate::gecko_bindings::bindings;
    unsafe { bindings::Gecko_IsFontFormatSupported(*kw) }
}

#[cfg(feature = "gecko")]
fn eval_font_tech(flag: &FontFaceSourceTechFlags) -> bool {
    use crate::gecko_bindings::bindings;
    unsafe { bindings::Gecko_IsFontTechSupported(*flag) }
}

#[cfg(feature = "servo")]
fn eval_font_format(_: &FontFaceSourceFormatKeyword) -> bool {
    false
}

#[cfg(feature = "servo")]
fn eval_font_tech(_: &FontFaceSourceTechFlags) -> bool {
    false
}

/// supports_condition | declaration
/// <https://drafts.csswg.org/css-conditional/#dom-css-supports-conditiontext-conditiontext>
pub fn parse_condition_or_declaration<'i, 't>(
    input: &mut Parser<'i, 't>,
) -> Result<SupportsCondition, ParseError<'i>> {
    if let Ok(condition) = input.try_parse(SupportsCondition::parse) {
        Ok(condition)
    } else {
        Declaration::parse(input).map(SupportsCondition::Declaration)
    }
}

impl ToCss for SupportsCondition {
    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
    where
        W: Write,
    {
        match *self {
            SupportsCondition::Not(ref cond) => {
                dest.write_str("not ")?;
                cond.to_css(dest)
            },
            SupportsCondition::Parenthesized(ref cond) => {
                dest.write_char('(')?;
                cond.to_css(dest)?;
                dest.write_char(')')
            },
            SupportsCondition::And(ref vec) => {
                let mut first = true;
                for cond in vec {
                    if !first {
                        dest.write_str(" and ")?;
                    }
                    first = false;
                    cond.to_css(dest)?;
                }
                Ok(())
            },
            SupportsCondition::Or(ref vec) => {
                let mut first = true;
                for cond in vec {
                    if !first {
                        dest.write_str(" or ")?;
                    }
                    first = false;
                    cond.to_css(dest)?;
                }
                Ok(())
            },
            SupportsCondition::Declaration(ref decl) => decl.to_css(dest),
            SupportsCondition::Selector(ref selector) => {
                dest.write_str("selector(")?;
                selector.to_css(dest)?;
                dest.write_char(')')
            },
            SupportsCondition::FontFormat(ref kw) => {
                dest.write_str("font-format(")?;
                kw.to_css(dest)?;
                dest.write_char(')')
            },
            SupportsCondition::FontTech(ref flag) => {
                dest.write_str("font-tech(")?;
                flag.to_css(dest)?;
                dest.write_char(')')
            },
            SupportsCondition::FutureSyntax(ref s) => dest.write_str(&s),
        }
    }
}

#[derive(Clone, Debug, ToShmem)]
/// A possibly-invalid CSS selector.
pub struct RawSelector(pub String);

impl ToCss for RawSelector {
    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
    where
        W: Write,
    {
        dest.write_str(&self.0)
    }
}

impl RawSelector {
    /// Tries to evaluate a `selector()` function.
    pub fn eval(&self, context: &ParserContext) -> bool {
        let mut input = ParserInput::new(&self.0);
        let mut input = Parser::new(&mut input);
        input
            .parse_entirely(|input| -> Result<(), CssParseError<()>> {
                let parser = SelectorParser {
                    namespaces: &context.namespaces,
                    stylesheet_origin: context.stylesheet_origin,
                    url_data: context.url_data,
                    for_supports_rule: true,
                };

                Selector::<SelectorImpl>::parse(&parser, input)
                    .map_err(|_| input.new_custom_error(()))?;

                Ok(())
            })
            .is_ok()
    }
}

#[derive(Clone, Debug, ToShmem)]
/// A possibly-invalid property declaration
pub struct Declaration(pub String);

impl ToCss for Declaration {
    fn to_css<W>(&self, dest: &mut CssWriter<W>) -> fmt::Result
    where
        W: Write,
    {
        dest.write_str(&self.0)
    }
}

/// <https://drafts.csswg.org/css-syntax-3/#typedef-any-value>
fn consume_any_value<'i, 't>(input: &mut Parser<'i, 't>) -> Result<(), ParseError<'i>> {
    input.expect_no_error_token().map_err(|err| err.into())
}

impl Declaration {
    /// Parse a declaration
    pub fn parse<'i, 't>(input: &mut Parser<'i, 't>) -> Result<Declaration, ParseError<'i>> {
        let pos = input.position();
        input.expect_ident()?;
        input.expect_colon()?;
        consume_any_value(input)?;
        Ok(Declaration(input.slice_from(pos).to_owned()))
    }

    /// Determine if a declaration parses
    ///
    /// <https://drafts.csswg.org/css-conditional-3/#support-definition>
    pub fn eval(&self, context: &ParserContext) -> bool {
        debug_assert!(context.rule_types().contains(CssRuleType::Style));

        let mut input = ParserInput::new(&self.0);
        let mut input = Parser::new(&mut input);
        input
            .parse_entirely(|input| -> Result<(), CssParseError<()>> {
                let prop = input.expect_ident_cloned().unwrap();
                input.expect_colon().unwrap();

                let id =
                    PropertyId::parse(&prop, context).map_err(|_| input.new_custom_error(()))?;

                let mut declarations = SourcePropertyDeclaration::default();
                input.parse_until_before(Delimiter::Bang, |input| {
                    PropertyDeclaration::parse_into(&mut declarations, id, &context, input)
                        .map_err(|_| input.new_custom_error(()))
                })?;
                let _ = input.try_parse(parse_important);
                Ok(())
            })
            .is_ok()
    }
}