diff options
author | Emilio Cobos Álvarez <emilio@crisal.io> | 2023-05-16 18:02:52 +0000 |
---|---|---|
committer | Martin Robinson <mrobinson@igalia.com> | 2023-11-24 08:57:14 +0100 |
commit | 920a1c1f087854dfad786b821494e6a0d52d4a56 (patch) | |
tree | 6fbbad3a935c68570015113244aefc33b553eca1 /components/style/parser.rs | |
parent | 32f1989a5caa3c63c2bf4ffadc3f159c79bae173 (diff) | |
download | servo-920a1c1f087854dfad786b821494e6a0d52d4a56.tar.gz servo-920a1c1f087854dfad786b821494e6a0d52d4a56.zip |
style: [css-nesting] Update cssparser again
This changes the cssparser setup to:
* Avoid having to do copies of the ParsingContext all over the place,
which is useful because I plan to stash more nesting state in there.
* Use the new RuleBodyParser which allows parsing qualified rules,
declarations, and so on. Though we still don't use this anywhere.
The next step is to join NestedRuleParser and PropertyDeclarationParser,
so that we can parse declarations in a lot of the nested rules as well.
Differential Revision: https://phabricator.services.mozilla.com/D178053
Diffstat (limited to 'components/style/parser.rs')
-rw-r--r-- | components/style/parser.rs | 30 |
1 files changed, 10 insertions, 20 deletions
diff --git a/components/style/parser.rs b/components/style/parser.rs index 837b9740024..11ef4b51a68 100644 --- a/components/style/parser.rs +++ b/components/style/parser.rs @@ -9,6 +9,7 @@ use crate::error_reporting::{ContextualParseError, ParseErrorReporter}; use crate::stylesheets::{CssRuleType, Namespaces, Origin, UrlExtraData}; use crate::use_counters::UseCounters; use cssparser::{Parser, SourceLocation, UnicodeRange}; +use std::borrow::Cow; use style_traits::{OneOrMoreSeparated, ParseError, ParsingMode, Separator}; /// Asserts that all ParsingMode flags have a matching ParsingMode value in gecko. @@ -53,7 +54,7 @@ pub struct ParserContext<'a> { /// The active error reporter, or none if error reporting is disabled. error_reporter: Option<&'a dyn ParseErrorReporter>, /// The currently active namespaces. - pub namespaces: Option<&'a Namespaces>, + pub namespaces: Cow<'a, Namespaces>, /// The use counters we want to record while parsing style rules, if any. pub use_counters: Option<&'a UseCounters>, } @@ -67,6 +68,7 @@ impl<'a> ParserContext<'a> { rule_type: Option<CssRuleType>, parsing_mode: ParsingMode, quirks_mode: QuirksMode, + namespaces: Cow<'a, Namespaces>, error_reporter: Option<&'a dyn ParseErrorReporter>, use_counters: Option<&'a UseCounters>, ) -> Self { @@ -77,29 +79,17 @@ impl<'a> ParserContext<'a> { parsing_mode, quirks_mode, error_reporter, - namespaces: None, + namespaces, use_counters, } } - /// Create a parser context based on a previous context, but with a modified - /// rule type. - #[inline] - pub fn new_with_rule_type( - context: &'a ParserContext, - rule_type: CssRuleType, - namespaces: &'a Namespaces, - ) -> ParserContext<'a> { - Self { - stylesheet_origin: context.stylesheet_origin, - url_data: context.url_data, - rule_type: Some(rule_type), - parsing_mode: context.parsing_mode, - quirks_mode: context.quirks_mode, - namespaces: Some(namespaces), - error_reporter: context.error_reporter, - use_counters: context.use_counters, - } + /// Temporarily sets the rule_type and executes the callback function, returning its result. + pub fn nest_for_rule<R>(&mut self, rule_type: CssRuleType, cb: impl FnOnce(&mut Self) -> R) -> R { + let old_rule_type = std::mem::replace(&mut self.rule_type, Some(rule_type)); + let r = cb(self); + self.rule_type = old_rule_type; + r } /// Whether we're in a @page rule. |