lewp_css/domain/at_rules/media/
media_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::MediaList,
6    crate::domain::{CssRule, CssRules, HasCssRules},
7    cssparser::ToCss,
8    std::fmt,
9};
10
11/// An [`@media`][media] url.
12///
13/// [media]: <https://drafts.csswg.org/css-conditional/#at-ruledef-media>
14#[derive(Debug, Clone)]
15pub struct MediaAtRule {
16    /// The list of media queries used by this media rule.
17    pub media_queries: MediaList,
18
19    /// The nested rules to this media rule.
20    pub rules: CssRules,
21}
22
23impl HasCssRules for MediaAtRule {
24    #[inline(always)]
25    fn css_rules(&self) -> &CssRules {
26        &self.rules
27    }
28
29    #[inline(always)]
30    fn css_rules_mut(&mut self) -> &mut CssRules {
31        &mut self.rules
32    }
33
34    #[inline(always)]
35    fn css_rules_slice(&self) -> &[CssRule] {
36        &self.rules.0[..]
37    }
38
39    #[inline(always)]
40    fn css_rules_vec(&self) -> &Vec<CssRule> {
41        &self.rules.0
42    }
43
44    #[inline(always)]
45    fn css_rules_vec_mut(&mut self) -> &mut Vec<CssRule> {
46        &mut self.rules.0
47    }
48}
49
50impl ToCss for MediaAtRule {
51    // https://drafts.csswg.org/cssom/#serialize-a-css-rule CSSMediaRule
52    fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
53        dest.write_str("@media ")?;
54        self.media_queries.to_css(dest)?;
55        dest.write_char('{')?;
56        self.rules.to_css(dest)?;
57        dest.write_char('}')
58    }
59}