lewp_css/domain/at_rules/viewport/
viewport_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::ViewportDescriptorDeclaration,
6    crate::{
7        domain::{
8            at_rules::VendorPrefixedAtRule,
9            HasVendorPrefix,
10            VendorPrefix,
11        },
12        parsers::{
13            viewport_at_rule_parser::ViewportAtRuleParser,
14            ParserContext,
15        },
16        CustomParseError,
17    },
18    cssparser::{DeclarationListParser, ParseError, Parser, ToCss},
19    std::fmt,
20};
21
22/// A `@viewport` rule.
23#[derive(Clone, Debug, PartialEq)]
24pub struct ViewportAtRule {
25    pub vendor_prefix: Option<VendorPrefix>,
26
27    /// The declarations contained in this @viewport rule.
28    pub declarations: Vec<ViewportDescriptorDeclaration>,
29}
30
31impl ViewportAtRule {
32    /// Parse a single @viewport rule.
33    pub(crate) fn parse_body<'i, 't>(
34        vendor_prefix: Option<VendorPrefix>,
35        context: &ParserContext,
36        input: &mut Parser<'i, 't>,
37    ) -> Result<Self, ParseError<'i, CustomParseError<'i>>> {
38        let parser = ViewportAtRuleParser { context };
39
40        let mut declarations = Vec::new();
41        let parser = DeclarationListParser::new(input, parser);
42        for result in parser {
43            match result {
44                Ok(viewportDescriptorDeclaration) => {
45                    declarations.push(viewportDescriptorDeclaration);
46                }
47
48                Err(preciseParseError) => return Err(preciseParseError.0),
49            }
50        }
51        Ok(Self {
52            vendor_prefix,
53            declarations,
54        })
55    }
56}
57
58impl ToCss for ViewportAtRule {
59    fn to_css<W: fmt::Write>(&self, dest: &mut W) -> fmt::Result {
60        dest.write_char('@')?;
61        if let Some(ref vendorPrefix) = self.vendor_prefix {
62            vendorPrefix.to_css(dest)?;
63        }
64        dest.write_str("viewport{")?;
65
66        let length = self.declarations.len();
67        if length != 0 {
68            for index in 0..(length - 1) {
69                (unsafe { self.declarations.get_unchecked(index) })
70                    .to_css(dest)?;
71            }
72
73            (unsafe { self.declarations.get_unchecked(length - 1) })
74                .to_css_without_trailing_semicolon(dest)?
75        }
76
77        dest.write_char('}')
78    }
79}
80
81impl HasVendorPrefix for ViewportAtRule {
82    #[inline(always)]
83    fn isNotVendorPrefixed(&self) -> bool {
84        self.vendor_prefix.is_none()
85    }
86}
87
88impl VendorPrefixedAtRule for ViewportAtRule {}