lewp_css/parsers/
parser_context.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 {super::ParsingMode, crate::domain::CssRuleType};
5
6/// The data that the parser needs from outside in order to parse a stylesheet.
7pub struct ParserContext {
8    /// The current rule type, if any.
9    pub(crate) rule_type: Option<CssRuleType>,
10
11    /// The mode to use when parsing.
12    pub(crate) parsing_mode: ParsingMode,
13}
14
15impl ParserContext {
16    // Creates a parser context based on a previous context, but with a modified rule type.
17    pub(crate) fn new_with_rule_type(
18        context: &ParserContext,
19        rule_type: CssRuleType,
20    ) -> ParserContext {
21        Self {
22            rule_type: Some(rule_type),
23            parsing_mode: context.parsing_mode,
24        }
25    }
26
27    #[inline(always)]
28    pub(crate) fn isInPageRule(&self) -> bool {
29        self.rule_type
30            .map_or(false, |rule_type| rule_type == CssRuleType::Page)
31    }
32
33    #[inline(always)]
34    pub(crate) fn isNotInPageRule(&self) -> bool {
35        !self.isInPageRule()
36    }
37
38    pub(crate) fn parsing_mode_allows_unitless_lengths(&self) -> bool {
39        self.parsing_mode.allows_unitless_lengths()
40    }
41}