lewp_css/domain/at_rules/document/
document_at_rule.rs

1// This file is part of css. It is subject to the license terms in the COPYRIGHT file found in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT. No part of predicator, including this file, may be copied, modified, propagated, or distributed except according to the terms contained in the COPYRIGHT file.
2// Copyright © 2017 The developers of css. See the COPYRIGHT file in the top-level directory of this distribution and at https://raw.githubusercontent.com/lemonrock/css/master/COPYRIGHT.
3
4use {
5    super::{Document, DocumentCondition},
6    crate::domain::{
7        at_rules::VendorPrefixedAtRule,
8        CssRule,
9        CssRules,
10        HasCssRules,
11        HasVendorPrefix,
12        VendorPrefix,
13    },
14    cssparser::ToCss,
15    std::fmt,
16};
17
18/// A @document rule
19#[derive(Debug, Clone)]
20pub struct DocumentAtRule {
21    pub vendor_prefix: Option<VendorPrefix>,
22
23    /// The parsed condition
24    pub condition: DocumentCondition,
25
26    /// Child rules
27    pub rules: CssRules,
28}
29
30impl HasCssRules for DocumentAtRule {
31    #[inline(always)]
32    fn css_rules(&self) -> &CssRules {
33        &self.rules
34    }
35
36    #[inline(always)]
37    fn css_rules_mut(&mut self) -> &mut CssRules {
38        &mut self.rules
39    }
40
41    #[inline(always)]
42    fn css_rules_slice(&self) -> &[CssRule] {
43        &self.rules.0[..]
44    }
45
46    #[inline(always)]
47    fn css_rules_vec(&self) -> &Vec<CssRule> {
48        &self.rules.0
49    }
50
51    #[inline(always)]
52    fn css_rules_vec_mut(&mut self) -> &mut Vec<CssRule> {
53        &mut self.rules.0
54    }
55}
56
57impl ToCss for DocumentAtRule {
58    fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
59        dest.write_str("@")?;
60        if let Some(ref vendor_prefix) = self.vendor_prefix {
61            vendor_prefix.to_css(dest)?;
62        }
63        dest.write_str("document ")?;
64        self.condition.to_css(dest)?;
65        dest.write_char('{')?;
66        self.rules.to_css(dest)?;
67        dest.write_char('}')
68    }
69}
70
71impl HasVendorPrefix for DocumentAtRule {
72    #[inline(always)]
73    fn isNotVendorPrefixed(&self) -> bool {
74        self.vendor_prefix.is_none()
75    }
76}
77
78impl VendorPrefixedAtRule for DocumentAtRule {}
79
80impl DocumentAtRule {
81    /// Evaluate a document condition.
82    pub fn evaluate<D: Document>(&self, document: &D) -> bool {
83        self.condition.evaluate(document)
84    }
85}