From 45b837399fc2b47f7e9b6acce97ed69b2f54a890 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Sun, 17 Jan 2021 14:44:41 +0000 Subject: [PATCH] style: Add dark mode to plaintext.css, and a document rule to target plaintext documents. We add two @-moz-document functions: `plain-text-document()`, matching the obvious, and `unobservable-document()`, which matches a top-level document with no opener. This is the equivalent check we do for automatic darkening of `about:blank` here: https://searchfox.org/mozilla-central/rev/014fe72eaba26dcf6082fb9bbaf208f97a38594e/layout/base/PresShell.cpp#5282 The former we don't need to use, but it's nice to let user stylesheets target plaintext documents properly (rather than relying on extensions or what not). Note that these are not content-observable. Add two tests: One showing that we produce different rendering when on dark mode, and one showing that we produce the same one from an iframe, regardless of dark mode. Depends on D101517 Differential Revision: https://phabricator.services.mozilla.com/D101518 --- components/style/stylesheets/document_rule.rs | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/components/style/stylesheets/document_rule.rs b/components/style/stylesheets/document_rule.rs index ce326808b63..736dddb502e 100644 --- a/components/style/stylesheets/document_rule.rs +++ b/components/style/stylesheets/document_rule.rs @@ -110,6 +110,13 @@ pub enum DocumentMatchingFunction { /// Matching function for a media document. #[css(function)] MediaDocument(MediaDocumentKind), + /// Matching function for a plain-text document. + #[css(function)] + PlainTextDocument(()), + /// Matching function for a document that can be observed by other content + /// documents. + #[css(function)] + UnobservableDocument(()), } macro_rules! parse_quoted_or_unquoted_string { @@ -161,6 +168,21 @@ impl DocumentMatchingFunction { Ok(DocumentMatchingFunction::MediaDocument(kind)) }) }, + + "plain-text-document" => { + input.parse_nested_block(|input| { + input.expect_exhausted()?; + Ok(DocumentMatchingFunction::PlainTextDocument(())) + }) + }, + + "unobservable-document" => { + input.parse_nested_block(|input| { + input.expect_exhausted()?; + Ok(DocumentMatchingFunction::UnobservableDocument(())) + }) + }, + _ => { Err(location.new_custom_error( StyleParseErrorKind::UnexpectedFunction(function.clone()) @@ -184,6 +206,8 @@ impl DocumentMatchingFunction { DocumentMatchingFunction::MediaDocument(_) => { GeckoDocumentMatchingFunction::MediaDocument }, + DocumentMatchingFunction::PlainTextDocument(..) => GeckoDocumentMatchingFunction::PlainTextDocument, + DocumentMatchingFunction::UnobservableDocument(..) => GeckoDocumentMatchingFunction::UnobservableDocument, }; let pattern = nsCStr::from(match *self { @@ -197,6 +221,8 @@ impl DocumentMatchingFunction { MediaDocumentKind::Plugin => "plugin", MediaDocumentKind::Video => "video", }, + DocumentMatchingFunction::PlainTextDocument(()) | + DocumentMatchingFunction::UnobservableDocument(()) => "", }); unsafe { Gecko_DocumentRule_UseForPresentation(device.document(), &*pattern, func) } } @@ -253,10 +279,9 @@ impl DocumentCondition { #[cfg(feature = "gecko")] fn allowed_in(&self, context: &ParserContext) -> bool { - use crate::stylesheets::Origin; use static_prefs::pref; - if context.stylesheet_origin != Origin::Author { + if context.in_ua_or_chrome_sheet() { return true; }