Bug 1551040 - Add dark mode to plaintext.css, and a document rule to target plaintext documents. r=boris

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
This commit is contained in:
Emilio Cobos Álvarez 2021-01-16 10:53:09 +00:00
Родитель b4e254b836
Коммит fcace619be
11 изменённых файлов: 70 добавлений и 2 удалений

Просмотреть файл

@ -68,6 +68,8 @@ class nsHTMLDocument : public mozilla::dom::Document {
nsContentList* GetExistingForms() const { return mForms; }
bool IsPlainText() const { return mIsPlainText; }
// Returns whether an object was found for aName.
bool ResolveName(JSContext* aCx, const nsAString& aName,
JS::MutableHandle<JS::Value> aRetval,

Просмотреть файл

@ -0,0 +1 @@
<iframe src="1551040.txt"></iframe>

Просмотреть файл

@ -0,0 +1 @@
Foobar

Просмотреть файл

@ -2079,6 +2079,8 @@ skip-if(!asyncPan) == 1544895.html 1544895-ref.html
== 1546856-2.html 1546856-ref.html
== 1547759-1.html 1547759-1-ref.html
== 1548809.html 1548809-ref.html
test-pref(ui.systemUsesDarkTheme,1) != 1551040.txt 1551040.txt
test-pref(ui.systemUsesDarkTheme,1) == 1551040.html 1551040.html
!= 1552789-1.html 1552789-ref-1.html
pref(image.downscale-during-decode.enabled,true) skip-if(Android&&webrender) == 1553571-1.html 1553571-1-ref.html
== 1558937-1.html 1558937-1-ref.html

Просмотреть файл

@ -6,8 +6,11 @@
#include "mozilla/dom/CSSMozDocumentRule.h"
#include "mozilla/dom/CSSMozDocumentRuleBinding.h"
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/ServoBindings.h"
#include "nsContentUtils.h"
#include "nsHTMLDocument.h"
namespace mozilla {
namespace dom {
@ -67,6 +70,12 @@ bool CSSMozDocumentRule::Match(const Document* aDoc, nsIURI* aDocURI,
return nsContentUtils::IsPatternMatching(spec, regex, aDoc)
.valueOr(false);
}
case DocumentMatchingFunction::PlainTextDocument:
return aDoc->IsHTMLOrXHTML() && aDoc->AsHTMLDocument()->IsPlainText();
case DocumentMatchingFunction::UnobservableDocument: {
const BrowsingContext* bc = aDoc->GetBrowsingContext();
return bc && bc->IsTop() && !bc->HasOpener();
}
}
MOZ_ASSERT_UNREACHABLE("Unknown matching function");
return false;

Просмотреть файл

@ -20,6 +20,8 @@ enum class DocumentMatchingFunction {
Domain,
RegExp,
MediaDocument,
PlainTextDocument,
UnobservableDocument,
};
} // namespace css

Просмотреть файл

@ -17,6 +17,16 @@ html:not([dir]) pre { /* Not a UA sheet, so doesn't use :-moz-has-dir-attr */
unicode-bidi: plaintext;
}
@-moz-document unobservable-document() {
@media (prefers-color-scheme: dark) {
:root {
/* in-content-page-{color, background} for dark theme. */
background: #2a2a2e;
color: rgb(249, 249, 250);
}
}
}
/* NOTE(emilio): For some reason some pages, mainly bing.com, load a bunch of
* scripts in zero-size <object> elements, see bug 1548449.
*

Просмотреть файл

@ -12,6 +12,8 @@ https://bugzilla.mozilla.org/show_bug.cgi?id=814907
@-moz-document media-document(image) {}
@-moz-document media-document(plugin) {}
@-moz-document media-document(all) {}
@-moz-document plain-text-document() {}
@-moz-document unobservable-document() {}
@-moz-document url(http://www.example.com/) {}
@-moz-document url('http://www.example.com/') {}
@ -60,6 +62,8 @@ function runTest()
"media-document(image)",
"media-document(plugin)",
"media-document(all)",
"plain-text-document()",
"unobservable-document()",
"url(\"http://www.example.com/\")",
"url(\"http://www.example.com/\")",
"url(\"http://www.example.com/\")",

Просмотреть файл

@ -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) }
}
@ -256,7 +282,7 @@ impl DocumentCondition {
use crate::stylesheets::Origin;
use static_prefs::pref;
if context.stylesheet_origin != Origin::Author {
if context.in_ua_or_chrome_sheet() {
return true;
}

Просмотреть файл

@ -118,6 +118,16 @@ where
}
}
impl ToCss for () {
#[inline]
fn to_css<W>(&self, _: &mut CssWriter<W>) -> fmt::Result
where
W: Write,
{
Ok(())
}
}
/// A writer tailored for serialising CSS.
///
/// Coupled with SequenceWriter, this allows callers to transparently handle

Просмотреть файл

@ -104,9 +104,10 @@
@media (prefers-color-scheme: dark) {
:root {
/* Keep this in sync with layout/base/PresShell.cpp! */
/* Keep these in sync with layout/base/PresShell.cpp, and plaintext.css */
--in-content-page-background: #2A2A2E;
--in-content-page-color: rgb(249, 249, 250);
--in-content-text-color: var(--in-content-page-color);
--in-content-deemphasized-text: var(--grey-40);
--in-content-box-background: #202023;