Skip to content

Commit

Permalink
Restructure CSS color parsing to be more declarative
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=273372

Reviewed by Tim Nguyen.

Restructures CSS color parsing and normalization to use be driven
by a set of constexpr descriptors of the various different color
function types. This reduces redundancy, but the main goal is to
make late resolution of relative colors easier, needed for currentColor
support, easier in a forthcoming change.

* Source/WebCore/WebCore.xcodeproj/project.pbxproj:
    - Added CSSColorDescriptors.h.

* Source/WebCore/css/color/CSSColorDescriptors.h: Added.
(WebCore::RGBFunctionLegacy):
(WebCore::RGBFunctionModernAbsolute):
(WebCore::RGBFunctionModernRelative):
(WebCore::HSLFunctionLegacy):
(WebCore::HSLFunctionModern):
(WebCore::HWBFunction):
(WebCore::LabFunction):
(WebCore::LCHFunction):
(WebCore::OKLabFunction):
(WebCore::OKLCHFunction):
(WebCore::ColorRGBFunction):
(WebCore::ColorXYZFunction):
    - Added descriptors for all the CSS color functions that directly
      describe a color. The descriptors contain enough information to
      parse and normalize the the absolute and relative forms.

* Source/WebCore/css/parser/CSSPropertyParserConsumer+Angle.h:
(WebCore::CSSPropertyParserHelpers::ConsumerLookup<AngleOrNumberOrNoneRaw>::operator()):
* Source/WebCore/css/parser/CSSPropertyParserConsumer+Meta.h:
* Source/WebCore/css/parser/CSSPropertyParserConsumer+Number.h:
(WebCore::CSSPropertyParserHelpers::ConsumerLookup<NumberRaw>::operator()):
* Source/WebCore/css/parser/CSSPropertyParserConsumer+Percent.cpp:
* Source/WebCore/css/parser/CSSPropertyParserConsumer+Percent.h:
(WebCore::CSSPropertyParserHelpers::consumeNumberOrPercentRaw):
(WebCore::CSSPropertyParserHelpers::consumeNumberOrPercentOrNoneRaw):
(WebCore::CSSPropertyParserHelpers::consumePercentOrNoneRaw):
(WebCore::CSSPropertyParserHelpers::ConsumerLookup<PercentRaw>::operator()):
(WebCore::CSSPropertyParserHelpers::ConsumerLookup<NumberOrPercentRaw>::operator()):
(WebCore::CSSPropertyParserHelpers::ConsumerLookup<NumberOrPercentOrNoneRaw>::operator()):
    - Add support for invoking a consumer from a generic context based on
      the result type. For instance, if you want to consume a NumberOrPercentRaw,
      a caller now use `ConsumerLookup<NumberOrPercentRaw>()(args)` to call
      `consumeNumberOrPercentRaw(args)`.

* Source/WebCore/css/parser/CSSPropertyParserConsumer+Color.cpp:
(WebCore::CSSPropertyParserHelpers::outsideSRGBGamut):
(WebCore::CSSPropertyParserHelpers::convertAbsoluteFunctionToColor):
(WebCore::CSSPropertyParserHelpers::convertRelativeFunctionToColor):
(WebCore::CSSPropertyParserHelpers::normalizeComponent):
(WebCore::CSSPropertyParserHelpers::normalizeAbsoluteComponents):
(WebCore::CSSPropertyParserHelpers::normalizeRelativeComponents):
(WebCore::CSSPropertyParserHelpers::consumeComponent):
(WebCore::CSSPropertyParserHelpers::consumeAlphaDelimiter):
(WebCore::CSSPropertyParserHelpers::consumeAbsoluteComponents):
(WebCore::CSSPropertyParserHelpers::consumeRelativeComponents):
(WebCore::CSSPropertyParserHelpers::consumeAndNormalizeAbsoluteComponents):
(WebCore::CSSPropertyParserHelpers::consumeAndNormalizeRelativeComponents):
(WebCore::CSSPropertyParserHelpers::parseGenericAbsoluteFunctionParametersRaw):
(WebCore::CSSPropertyParserHelpers::parseGenericRelativeFunctionParametersRaw):
(WebCore::CSSPropertyParserHelpers::parseGenericFunctionParametersRaw):
(WebCore::CSSPropertyParserHelpers::parseRGBFunctionParametersRaw):
(WebCore::CSSPropertyParserHelpers::parseHSLFunctionParametersRaw):
(WebCore::CSSPropertyParserHelpers::parseColorFunctionParametersRaw):
(WebCore::CSSPropertyParserHelpers::parseColorFunctionRaw):
(WebCore::CSSPropertyParserHelpers::parseColorFunction):
(WebCore::CSSPropertyParserHelpers::normalizeLightnessPercent): Deleted.
(WebCore::CSSPropertyParserHelpers::normalizeABPercent): Deleted.
(WebCore::CSSPropertyParserHelpers::normalizeChromaPercent): Deleted.
(WebCore::CSSPropertyParserHelpers::normalizeXYZPercent): Deleted.
(WebCore::CSSPropertyParserHelpers::normalizeRGBPercent): Deleted.
(WebCore::CSSPropertyParserHelpers::normalizeAlphaPercent): Deleted.
(WebCore::CSSPropertyParserHelpers::consumeOriginColorRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::consumeRGBOrHSLLegacyOptionalAlphaRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::consumeOptionalAlphaRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::consumeOptionalAlphaRawAllowingSymbolTableIdent): Deleted.
(WebCore::CSSPropertyParserHelpers::normalizeRGBFunctionComponent): Deleted.
(WebCore::CSSPropertyParserHelpers::normalizeRGBFunctionAlpha): Deleted.
(WebCore::CSSPropertyParserHelpers::normalizeRGBFunctionComponents): Deleted.
(WebCore::CSSPropertyParserHelpers::parseRelativeRGBParametersRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::parseNonRelativeRGBParametersLegacyRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::parseNonRelativeRGBParametersModernRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::parseNonRelativeRGBParametersRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::parseRGBParametersRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::colorByResolvingHSLComponentsModern): Deleted.
(WebCore::CSSPropertyParserHelpers::colorByResolvingHSLComponentsLegacy): Deleted.
(WebCore::CSSPropertyParserHelpers::parseRelativeHSLParametersRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::parseNonRelativeHSLParametersLegacyRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::parseNonRelativeHSLParametersModernRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::parseNonRelativeHSLParametersRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::parseHSLParametersRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::normalizeHWBParametersRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::parseHWBParametersRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::parseRelativeHWBParametersRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::parseNonRelativeHWBParametersRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::parseLabParametersRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::parseRelativeLabParametersRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::parseNonRelativeLabParametersRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::parseLCHParametersRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::parseRelativeLCHParametersRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::parseNonRelativeLCHParametersRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::parseColorFunctionForRGBTypesRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::parseRelativeColorFunctionForRGBTypes): Deleted.
(WebCore::CSSPropertyParserHelpers::parseColorFunctionForXYZTypesRaw): Deleted.
(WebCore::CSSPropertyParserHelpers::parseRelativeColorFunctionForXYZTypes): Deleted.
(WebCore::CSSPropertyParserHelpers::parseNonRelativeColorFunctionParameters): Deleted.
    - Merge most of the color function parsing and normalization into a
      shared set of "generic" parsers and normalizers. As the parsing is
      not entirely regular among all the types, we still have some code
      to start the parsing for the following cases:

      - `rgb()/rgba()` have their own entry point `parseRGBFunctionParametersRaw`
        to allow disambiguation between the modern and legacy syntaxes.
      - `hsl()/hsla()` have their own entry point `parseHSLFunctionParametersRaw`
        to allow disambiguation between the modern and legacy syntaxes.
        Sharing this with the `rgb()/rgba()` parser would be nice, but the
        `rgb()/rgba()` has an additional requirement that all the parameters
        have the same type when using the legacy form, and any sharing ended
        up being more code and more complicated.
      - `color()` (both rgb and xyz) have their own entry point `parseColorFunctionParametersRaw`
        to allow for the additional color space parameter.
      - Everything else (`hwb()`, `lab()`, `lch()`, `oklab()`, `oklch()`) uses
        `parseGenericFunctionParametersRaw` directly. The others end up calling
        the generic code after their disambiguation.

Canonical link: https://commits.webkit.org/278370@main
  • Loading branch information
weinig authored and Sam Weinig committed May 4, 2024
1 parent 13455c7 commit 9bcbb8f
Show file tree
Hide file tree
Showing 8 changed files with 773 additions and 1,165 deletions.
2 changes: 2 additions & 0 deletions Source/WebCore/WebCore.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -17430,6 +17430,7 @@
BC2441C30E8B65D00055320F /* ScrollView.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ScrollView.cpp; sourceTree = "<group>"; };
BC274B2E140EBEB200EADFA6 /* CSSBorderImageSliceValue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CSSBorderImageSliceValue.h; sourceTree = "<group>"; };
BC274B30140EBED800EADFA6 /* CSSBorderImageSliceValue.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = CSSBorderImageSliceValue.cpp; sourceTree = "<group>"; };
BC2795AF2BDD58AE00595448 /* CSSColorDescriptors.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = CSSColorDescriptors.h; sourceTree = "<group>"; };
BC29935C17A1DD5800BCE880 /* ColorInputType.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ColorInputType.cpp; sourceTree = "<group>"; };
BC2B410E2732EFE400A2D191 /* ModelPlayerClient.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ModelPlayerClient.h; sourceTree = "<group>"; };
BC2B41122732F03C00A2D191 /* ModelPlayerClient.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = ModelPlayerClient.cpp; sourceTree = "<group>"; };
Expand Down Expand Up @@ -33356,6 +33357,7 @@
BC29861A296B586100E4001F /* color */ = {
isa = PBXGroup;
children = (
BC2795AF2BDD58AE00595448 /* CSSColorDescriptors.h */,
492461E12AA51D420081471E /* CSSColorMixSerialization.h */,
BC04A7C6296E53410003F9DB /* CSSResolvedColorMix.cpp */,
BC04A7C7296E53410003F9DB /* CSSResolvedColorMix.h */,
Expand Down
273 changes: 273 additions & 0 deletions Source/WebCore/css/color/CSSColorDescriptors.h

Large diffs are not rendered by default.

14 changes: 14 additions & 0 deletions Source/WebCore/css/parser/CSSPropertyParserConsumer+Angle.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,5 +184,19 @@ auto consumeAngleOrNumberOrNoneRawAllowingSymbolTableIdent(CSSParserTokenRange&
return consumeMetaConsumer<AngleOrNumberOrNoneRawAllowingSymbolTableIdentConsumer<Transformer>>(range, symbolTable, ValueRange::All, parserMode, UnitlessQuirk::Forbid, UnitlessZeroQuirk::Forbid);
}

// MARK: Consumer Lookup

template<> struct ConsumerLookup<AngleOrNumberOrNoneRaw> {
std::optional<AngleOrNumberOrNoneRaw> operator()(CSSParserTokenRange& args, CSSParserMode parserMode)
{
return consumeAngleOrNumberOrNoneRaw(args, parserMode);
}

std::optional<AngleOrNumberOrNoneRaw> operator()(CSSParserTokenRange& args, CSSParserMode parserMode, const CSSCalcSymbolTable& symbolTable)
{
return consumeAngleOrNumberOrNoneRawAllowingSymbolTableIdent(args, symbolTable, parserMode);
}
};

} // namespace CSSPropertyParserHelpers
} // namespace WebCore
Loading

0 comments on commit 9bcbb8f

Please sign in to comment.