Bug 247326 - Add helper function to match identifiers and return a corresponding enum mapped enum value for CSS property parser
Summary: Add helper function to match identifiers and return a corresponding enum mapp...
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: CSS (show other bugs)
Version: WebKit Local Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Sam Weinig
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2022-11-01 11:07 PDT by Sam Weinig
Modified: 2022-11-03 13:25 PDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sam Weinig 2022-11-01 11:07:09 PDT
Add helper function to match identifiers and return a corresponding enum mapped enum value for CSS property parser
Comment 1 Sam Weinig 2022-11-01 20:37:43 PDT
One idea is to just use SortedArrayMap + a helper. For example, the helper "consumeIdentUsingMapping" and and example use:

```
template<typename Map>
static std::optional<typename Map::ValueType> consumeIdentUsingMapping(CSSParserTokenRange& range, Map& map)
{
    if (auto value = map.tryGet(range.peek().id())) {
        range.consumeIncludingWhitespace();
        return std::make_optional(*value);
    }
    return std::nullopt;
}

static std::optional<HueInterpolationMethod> consumeHueInterpolationMethod(CSSParserTokenRange& range)
{
    static constexpr std::pair<CSSValueID, HueInterpolationMethod> hueInterpolationMethodMappings[] {
        { CSSValueShorter, HueInterpolationMethod::Shorter },
        { CSSValueLonger, HueInterpolationMethod::Longer },
        { CSSValueIncreasing, HueInterpolationMethod::Increasing },
        { CSSValueDecreasing, HueInterpolationMethod::Decreasing },
        { CSSValueSpecified, HueInterpolationMethod::Specified },
    };
    static constexpr SortedArrayMap hueInterpolationMethodMap { hueInterpolationMethodMappings };

    return consumeIdentUsingMapping(range, hueInterpolationMethodMap);
}
```
Comment 2 Sam Weinig 2022-11-01 20:38:59 PDT
An alternative that doesn't require declaring the static constexprs would be to use initializer_list and a linear search. For example:

```
template<typename MappedType>
static std::optional<typename Map::ValueType> consumeIdentUsingMappingTo(CSSParserTokenRange& range, std::initializer_list<std::pair<CSSValueID, MappedType> list)
{
    auto key = range.peek().id();
    for (auto [valueID, mapping] : list) {
        if (key == valueID)
            return std::make_optional(mapping);
    }
    return std::nullopt;
}

static std::optional<HueInterpolationMethod> consumeHueInterpolationMethod(CSSParserTokenRange& range)
{
    return consumeIdentifierMappingTo<HueInterpolationMethod>(range, {
        { CSSValueShorter, HueInterpolationMethod::Shorter },
        { CSSValueLonger, HueInterpolationMethod::Longer },
        { CSSValueIncreasing, HueInterpolationMethod::Increasing },
        { CSSValueDecreasing, HueInterpolationMethod::Decreasing },
        { CSSValueSpecified, HueInterpolationMethod::Specified },
    });
}
```
Comment 3 Sam Weinig 2022-11-01 20:41:41 PDT
Yet another alternative would be to use the template arguments themselves (I haven't written out this version yet, but it would be a recursive walk through the argument pairs). The caller might look like this:

```
static std::optional<HueInterpolationMethod> consumeHueInterpolationMethod(CSSParserTokenRange& range)
{
    return consumeIdentifierMappingTo<HueInterpolationMethod,
        CSSValueShorter, HueInterpolationMethod::Shorter,
        CSSValueLonger, HueInterpolationMethod::Longer,
        CSSValueIncreasing, HueInterpolationMethod::Increasing,
        CSSValueDecreasing, HueInterpolationMethod::Decreasing,
        CSSValueSpecified, HueInterpolationMethod::Specified,
    >(range);
}
```
Comment 4 Radar WebKit Bug Importer 2022-11-02 14:11:57 PDT
<rdar://problem/101878937>
Comment 5 Sam Weinig 2022-11-02 14:15:37 PDT
Pull request: https://github.com/WebKit/WebKit/pull/6050
Comment 6 EWS 2022-11-03 13:24:59 PDT
Committed 256290@main (dcf98ac5a51f): <https://commits.webkit.org/256290@main>

Reviewed commits have been landed. Closing PR #6050 and removing active labels.