# HG changeset patch
# User Matt A. Tobin <email@mattatobin.com>
# Date 1722133404 18000
# Node ID da8cbca75ca260bf71ec5c25cc2ac643a4d597c1
# Parent  3bcdb48f9c502425d0ea515aea7051e8e6a1eb24
Add support for the CSS forced-colors media feature.

diff --git a/dom/base/nsGkAtomList.h b/dom/base/nsGkAtomList.h
--- a/dom/base/nsGkAtomList.h
+++ b/dom/base/nsGkAtomList.h
@@ -2265,16 +2265,17 @@ GK_ATOM(scrollbar_start_forward, "scroll
 GK_ATOM(scrollbar_end_backward, "scrollbar-end-backward")
 GK_ATOM(scrollbar_end_forward, "scrollbar-end-forward")
 GK_ATOM(scrollbar_thumb_proportional, "scrollbar-thumb-proportional")
 GK_ATOM(overlay_scrollbars, "overlay-scrollbars")
 GK_ATOM(prefers_color_scheme, "prefers-color-scheme")
 GK_ATOM(prefers_contrast, "prefers-contrast")
 GK_ATOM(prefers_reduced_motion, "prefers-reduced-motion")
 GK_ATOM(prefers_reduced_transparency, "prefers-reduced-transparency")
+GK_ATOM(forced_colors, "forced-colors")
 GK_ATOM(windows_accent_color_in_titlebar, "windows-accent-color-in-titlebar")
 GK_ATOM(windows_default_theme, "windows-default-theme")
 GK_ATOM(mac_graphite_theme, "mac-graphite-theme")
 GK_ATOM(mac_yosemite_theme, "mac-yosemite-theme")
 GK_ATOM(windows_compositor, "windows-compositor")
 GK_ATOM(windows_glass, "windows-glass")
 GK_ATOM(touch_enabled, "touch-enabled")
 GK_ATOM(menubar_drag, "menubar-drag")
diff --git a/layout/style/nsMediaFeatures.cpp b/layout/style/nsMediaFeatures.cpp
--- a/layout/style/nsMediaFeatures.cpp
+++ b/layout/style/nsMediaFeatures.cpp
@@ -71,16 +71,22 @@ static const nsCSSProps::KTableEntry kPr
 };
 
 static const nsCSSProps::KTableEntry kPrefersReducedTransparencyKeywords[] = {
   { eCSSKeyword_no_preference,          NS_STYLE_PREFERS_REDUCED_TRANSPARENCY_NO_PREFERENCE },
   { eCSSKeyword_reduce,                 NS_STYLE_PREFERS_REDUCED_TRANSPARENCY_REDUCE },
   { eCSSKeyword_UNKNOWN,                -1 },
 };
 
+static const nsCSSProps::KTableEntry kForcedColorsKeywords[] = {
+  { eCSSKeyword_no_preference,          NS_STYLE_FORCED_COLORS_NONE },
+  { eCSSKeyword_active,                 NS_STYLE_FORCED_COLORS_ACTIVE },
+  { eCSSKeyword_UNKNOWN,                -1 },
+};
+
 #ifdef XP_WIN
 struct WindowsThemeName {
   LookAndFeel::WindowsTheme id;
   const wchar_t* name;
 };
 
 // Windows theme identities used in the -moz-windows-theme media query.
 const WindowsThemeName themeStrings[] = {
@@ -607,16 +613,29 @@ GetPrefersReducedTransparency(nsIDocumen
   }
   else {
     aResult.SetIntValue(NS_STYLE_PREFERS_REDUCED_TRANSPARENCY_NO_PREFERENCE,
                         eCSSUnit_Enumerated);
   }
 }
 
 static void
+GetForcedColors(nsIDocument* aDocument, const nsMediaFeature* aFeature,
+                nsCSSValue& aResult)
+{
+  if (Preferences::GetInt("browser.display.document_color_use", 0) == 2 ||
+      LookAndFeel::GetInt(LookAndFeel::eIntID_UseAccessibilityTheme, 0)) {
+    aResult.SetIntValue(NS_STYLE_FORCED_COLORS_ACTIVE, eCSSUnit_Enumerated);
+  }
+  else {
+    aResult.SetIntValue(NS_STYLE_FORCED_COLORS_NONE, eCSSUnit_Enumerated);
+  }
+}
+
+static void
 GetIsGlyph(nsIDocument* aDocument, const nsMediaFeature* aFeature,
            nsCSSValue& aResult)
 {
   MOZ_ASSERT(aFeature->mReqFlags & nsMediaFeature::eUserAgentAndChromeOnly);
   aResult.SetIntValue(aDocument->IsSVGGlyphsDocument() ? 1 : 0, eCSSUnit_Integer);
 }
 
 /*
@@ -738,16 +757,24 @@ nsMediaFeatures::features[] = {
     &nsGkAtoms::prefers_reduced_transparency,
     nsMediaFeature::eMinMaxNotAllowed,
     nsMediaFeature::eEnumerated,
     nsMediaFeature::eNoRequirements,
     { kPrefersReducedTransparencyKeywords },
     GetPrefersReducedTransparency
   },
   {
+    &nsGkAtoms::forced_colors,
+    nsMediaFeature::eMinMaxNotAllowed,
+    nsMediaFeature::eEnumerated,
+    nsMediaFeature::eNoRequirements,
+    { kForcedColorsKeywords },
+    GetForcedColors
+  },
+  {
     &nsGkAtoms::resolution,
     nsMediaFeature::eMinMaxAllowed,
     nsMediaFeature::eResolution,
     nsMediaFeature::eNoRequirements,
     { nullptr },
     GetResolution
   },
   {
diff --git a/layout/style/nsStyleConsts.h b/layout/style/nsStyleConsts.h
--- a/layout/style/nsStyleConsts.h
+++ b/layout/style/nsStyleConsts.h
@@ -1234,11 +1234,15 @@ enum class StyleOverscrollBehavior : uin
 // prefers-reduced-motion
 #define NS_STYLE_PREFERS_REDUCED_MOTION_NO_PREFERENCE   0
 #define NS_STYLE_PREFERS_REDUCED_MOTION_REDUCE          1
 
 // prefers-reduced-transparency
 #define NS_STYLE_PREFERS_REDUCED_TRANSPARENCY_NO_PREFERENCE   0
 #define NS_STYLE_PREFERS_REDUCED_TRANSPARENCY_REDUCE          1
 
+// forced-colors
+#define NS_STYLE_FORCED_COLORS_NONE             0
+#define NS_STYLE_FORCED_COLORS_ACTIVE           1
+
 } // namespace mozilla
 
 #endif /* nsStyleConsts_h___ */
