From 04282ff04c4bf2c632f37d25cf980e13cb7c8c86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Wed, 31 May 2023 11:39:33 +0200 Subject: [PATCH] style: Allow matches() / querySelector() / etc on chrome docs to access chrome-only selectors. r=boris Without this some tests fail with the previous patch because code like: https://searchfox.org/mozilla-central/rev/267682a8f45221bf0bfe999d4a0239706a43bc56/browser/base/content/browser-gestureSupport.js#651 starts throwing. Unfortunately I had missed that on my try run, because the error message didn't include that exception (it seemed like an intermittent browser-chrome failure instead). We could expose a ChromeOnly API for this, but this seems better. This fixes it trivially, and also removes the "no url data" situation from the selector parser, which is nice. Differential Revision: https://phabricator.services.mozilla.com/D130818 --- components/style/selector_parser.rs | 13 +++++++------ components/style/stylesheets/rule_parser.rs | 2 +- components/style/stylesheets/supports_rule.rs | 2 +- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/components/style/selector_parser.rs b/components/style/selector_parser.rs index 67d4d4f6981..8e0e3412d4b 100644 --- a/components/style/selector_parser.rs +++ b/components/style/selector_parser.rs @@ -46,7 +46,7 @@ pub struct SelectorParser<'a> { pub namespaces: &'a Namespaces, /// The extra URL data of the stylesheet, which is used to look up /// whether we are parsing a chrome:// URL style sheet. - pub url_data: Option<&'a UrlExtraData>, + pub url_data: &'a UrlExtraData, } impl<'a> SelectorParser<'a> { @@ -54,14 +54,15 @@ impl<'a> SelectorParser<'a> { /// account namespaces. /// /// This is used for some DOM APIs like `querySelector`. - pub fn parse_author_origin_no_namespace( - input: &str, - ) -> Result, ParseError> { + pub fn parse_author_origin_no_namespace<'i>( + input: &'i str, + url_data: &UrlExtraData, + ) -> Result, ParseError<'i>> { let namespaces = Namespaces::default(); let parser = SelectorParser { stylesheet_origin: Origin::Author, namespaces: &namespaces, - url_data: None, + url_data, }; let mut input = ParserInput::new(input); SelectorList::parse(&parser, &mut CssParser::new(&mut input)) @@ -75,7 +76,7 @@ impl<'a> SelectorParser<'a> { /// Whether we're parsing selectors in a stylesheet that has chrome /// privilege. pub fn chrome_rules_enabled(&self) -> bool { - self.url_data.map_or(false, |d| d.chrome_rules_enabled()) || + self.url_data.chrome_rules_enabled() || self.stylesheet_origin == Origin::User } } diff --git a/components/style/stylesheets/rule_parser.rs b/components/style/stylesheets/rule_parser.rs index 7e3e4f9f86c..995ee801e40 100644 --- a/components/style/stylesheets/rule_parser.rs +++ b/components/style/stylesheets/rule_parser.rs @@ -710,7 +710,7 @@ impl<'a, 'b, 'i> QualifiedRuleParser<'i> for NestedRuleParser<'a, 'b> { let selector_parser = SelectorParser { stylesheet_origin: self.context.stylesheet_origin, namespaces: self.namespaces, - url_data: Some(self.context.url_data), + url_data: self.context.url_data, }; let selectors = SelectorList::parse(&selector_parser, input)?; if self.context.error_reporting_enabled() { diff --git a/components/style/stylesheets/supports_rule.rs b/components/style/stylesheets/supports_rule.rs index d0aaa0a03d8..23d79fd1cc2 100644 --- a/components/style/stylesheets/supports_rule.rs +++ b/components/style/stylesheets/supports_rule.rs @@ -335,7 +335,7 @@ impl RawSelector { let parser = SelectorParser { namespaces, stylesheet_origin: context.stylesheet_origin, - url_data: Some(context.url_data), + url_data: context.url_data, }; #[allow(unused_variables)]