Revert "For mozcentral use Firefox color theme instead of system theme." since -moz-toolbar-prefers-color-scheme was removed

Reverts mozilla/pdf.js#13314, see https://groups.google.com/g/firefox-dev/c/vajhbYKDpPM

Given that `-moz-toolbar-prefers-color-scheme` was removed in https://bugzilla.mozilla.org/show_bug.cgi?id=1736038, unless we fix this before the next PDF.js update in mozilla-central we'll thus break dark mode in the Firefox built-in PDF Viewer.
This commit is contained in:
Jonas Jenwald 2021-10-17 12:17:42 +02:00
parent 52fce0d17b
commit aae8a21286
2 changed files with 8 additions and 16 deletions

View File

@ -270,12 +270,6 @@ function preprocessCSS(mode, source, destination) {
content = expandImports(content, source); content = expandImports(content, source);
if (mode === "mozcentral") { if (mode === "mozcentral") {
content = removePrefixed(content, hasPrefixedMozcentral); content = removePrefixed(content, hasPrefixedMozcentral);
// In the mozcentral version the color theme should be based on the Firefox
// theme instead of the system theme.
content = content.replace(
"prefers-color-scheme",
"-moz-toolbar-prefers-color-scheme"
);
} }
fs.writeFileSync(destination, content); fs.writeFileSync(destination, content);
} }

View File

@ -431,23 +431,21 @@ const PDFViewerApplication = {
try { try {
const styleSheet = document.styleSheets[0]; const styleSheet = document.styleSheets[0];
const cssRules = styleSheet?.cssRules || []; const cssRules = styleSheet?.cssRules || [];
const mediaMatcher =
typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")
? "-moz-toolbar-prefers-color-scheme"
: "prefers-color-scheme";
const mediaRule = `(${mediaMatcher}: dark)`;
const mediaRegex = new RegExp(
`^@media \\(${mediaMatcher}: dark\\) {\\n\\s*([\\w\\s-.,:;/\\\\{}()]+)\\n}$`
);
for (let i = 0, ii = cssRules.length; i < ii; i++) { for (let i = 0, ii = cssRules.length; i < ii; i++) {
const rule = cssRules[i]; const rule = cssRules[i];
if (rule instanceof CSSMediaRule && rule.media?.[0] === mediaRule) { if (
rule instanceof CSSMediaRule &&
rule.media?.[0] === "(prefers-color-scheme: dark)"
) {
if (cssTheme === ViewerCssTheme.LIGHT) { if (cssTheme === ViewerCssTheme.LIGHT) {
styleSheet.deleteRule(i); styleSheet.deleteRule(i);
return; return;
} }
// cssTheme === ViewerCssTheme.DARK // cssTheme === ViewerCssTheme.DARK
const darkRules = mediaRegex.exec(rule.cssText); const darkRules =
/^@media \(prefers-color-scheme: dark\) {\n\s*([\w\s-.,:;/\\{}()]+)\n}$/.exec(
rule.cssText
);
if (darkRules?.[1]) { if (darkRules?.[1]) {
styleSheet.deleteRule(i); styleSheet.deleteRule(i);
styleSheet.insertRule(darkRules[1], i); styleSheet.insertRule(darkRules[1], i);