Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement forced-colors media query.
https://bugs.webkit.org/show_bug.cgi?id=225281
rdar://77715265

Reviewed by Tim Nguyen and Aditya Keerthi.

MQ always matchs "none", as there is no concept of "forced colors" in
Cocoa.
https://www.w3.org/TR/mediaqueries-5/#forced-colors

* LayoutTests/imported/w3c/web-platform-tests/css/mediaqueries/forced-colors-expected.txt:
* Source/WebCore/css/CSSValueKeywords.in:
* Source/WebCore/css/MediaFeatureNames.h:
* Source/WebCore/css/MediaQueryEvaluator.cpp:
(WebCore::forcedColorsEvaluate):
* Source/WebCore/css/MediaQueryExpression.cpp:
(WebCore::isValidValueForIdentMediaFeature):
(WebCore::featureWithValidIdent):
(WebCore::isFeatureValidWithoutValue):

Canonical link: https://commits.webkit.org/253290@main
  • Loading branch information
mdubet authored and nt1m committed Aug 10, 2022
1 parent 8a44e02 commit 46513cc
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 5 deletions.
@@ -1,7 +1,7 @@

FAIL Should be known: '(forced-colors)' assert_true: Can parse with JS expected true got false
FAIL Should be known: '(forced-colors: none)' assert_true: Can parse with JS expected true got false
FAIL Should be known: '(forced-colors: active)' assert_true: Can parse with JS expected true got false
PASS Should be known: '(forced-colors)'
PASS Should be known: '(forced-colors: none)'
PASS Should be known: '(forced-colors: active)'
FAIL Should be parseable: '(forced-colors: 0)' assert_true: Can parse with JS expected true got false
PASS Should be unknown: '(forced-colors: 0)'
FAIL Should be parseable: '(forced-colors: no-preference)' assert_true: Can parse with JS expected true got false
Expand All @@ -14,5 +14,5 @@ FAIL Should be parseable: '(forced-colors: none active)' assert_true: Can parse
PASS Should be unknown: '(forced-colors: none active)'
FAIL Should be parseable: '(forced-colors: active/none)' assert_true: Can parse with JS expected true got false
PASS Should be unknown: '(forced-colors: active/none)'
FAIL Check that none evaluates to false in the boolean context assert_equals: expected true but got false
PASS Check that none evaluates to false in the boolean context

4 changes: 4 additions & 0 deletions Source/WebCore/css/CSSValueKeywords.in
Expand Up @@ -1172,6 +1172,10 @@ hover
progressive
interlace

// (forced-colors:) media feature.
// none
active

// blend modes
// normal
multiply
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/css/MediaFeatureNames.h
Expand Up @@ -48,6 +48,7 @@
macro(devicePixelRatio, "-webkit-device-pixel-ratio") \
macro(deviceWidth, "device-width") \
macro(dynamicRange, "dynamic-range") \
macro(forcedColors, "forced-colors") \
macro(grid, "grid") \
macro(height, "height") \
macro(hover, "hover") \
Expand Down
10 changes: 10 additions & 0 deletions Source/WebCore/css/MediaQueryEvaluator.cpp
Expand Up @@ -489,6 +489,16 @@ static bool scanEvaluate(CSSValue* value, const CSSToLengthConversionData&, Fram
return primitiveValue->valueID() == CSSValueProgressive;
}

static bool forcedColorsEvaluate(CSSValue* value, const CSSToLengthConversionData&, Frame&, MediaFeaturePrefix)
{
auto* primitiveValue = dynamicDowncast<CSSPrimitiveValue>(value);
if (!primitiveValue)
return false;

// On Cocoa platforms, there is no concept of "forced colors".
return primitiveValue->valueID() == CSSValueNone;
}

static bool gridEvaluate(CSSValue* value, const CSSToLengthConversionData&, Frame&, MediaFeaturePrefix op)
{
return zeroEvaluate(value, op);
Expand Down
6 changes: 5 additions & 1 deletion Source/WebCore/css/MediaQueryExpression.cpp
Expand Up @@ -69,6 +69,8 @@ static inline bool isValidValueForIdentMediaFeature(const AtomString& feature, c
return valueID == CSSValueHigh || valueID == CSSValueStandard;
if (feature == MediaFeatureNames::scan)
return valueID == CSSValueProgressive || valueID == CSSValueInterlace;
if (feature == MediaFeatureNames::forcedColors)
return valueID == CSSValueNone || valueID == CSSValueActive;

return false;
}
Expand All @@ -95,7 +97,8 @@ static inline bool featureWithValidIdent(const AtomString& mediaFeature, const C
|| mediaFeature == MediaFeatureNames::prefersReducedMotion
|| (mediaFeature == MediaFeatureNames::prefersDarkInterface && (context.useSystemAppearance || isUASheetBehavior(context.mode)))
|| mediaFeature == MediaFeatureNames::dynamicRange
|| mediaFeature == MediaFeatureNames::scan;
|| mediaFeature == MediaFeatureNames::scan
|| mediaFeature == MediaFeatureNames::forcedColors;
}

static inline bool featureWithValidDensity(const String& mediaFeature, const CSSPrimitiveValue& value)
Expand Down Expand Up @@ -214,6 +217,7 @@ static inline bool isFeatureValidWithoutValue(const AtomString& mediaFeature, co
|| mediaFeature == MediaFeatureNames::displayMode
#endif
|| mediaFeature == MediaFeatureNames::scan
|| mediaFeature == MediaFeatureNames::forcedColors
|| mediaFeature == MediaFeatureNames::videoPlayableInline;
}

Expand Down

0 comments on commit 46513cc

Please sign in to comment.