1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:48:12 +00:00

LibWeb: Move color stop list parsing to standalone functions

This makes these slightly less clunky to use for other gradient types.
This commit is contained in:
MacDue 2022-11-08 21:33:00 +01:00 committed by Sam Atkins
parent c02163c31f
commit f1f1977e2d
2 changed files with 29 additions and 26 deletions

View file

@ -2386,6 +2386,30 @@ static Optional<Vector<TElement>> parse_color_stop_list(auto& tokens, auto is_po
return color_stops;
}
Optional<Vector<LinearColorStopListElement>> Parser::parse_linear_color_stop_list(TokenStream<ComponentValue>& tokens)
{
// <color-stop-list> =
// <linear-color-stop> , [ <linear-color-hint>? , <linear-color-stop> ]#
return parse_color_stop_list<LinearColorStopListElement>(
tokens,
[](Dimension& dimension) { return dimension.is_length_percentage(); },
[](Dimension& dimension) { return dimension.length_percentage(); },
[&](auto& token) { return parse_color(token); },
[&](auto& token) { return parse_dimension(token); });
}
Optional<Vector<AngularColorStopListElement>> Parser::parse_angular_color_stop_list(TokenStream<ComponentValue>& tokens)
{
// <angular-color-stop-list> =
// <angular-color-stop> , [ <angular-color-hint>? , <angular-color-stop> ]#
return parse_color_stop_list<AngularColorStopListElement>(
tokens,
[](Dimension& dimension) { return dimension.is_angle_percentage(); },
[](Dimension& dimension) { return dimension.angle_percentage(); },
[&](auto& token) { return parse_color(token); },
[&](auto& token) { return parse_dimension(token); });
}
static StringView consume_if_starts_with(StringView str, StringView start, auto found_callback)
{
if (str.starts_with(start, CaseSensitivity::CaseInsensitive)) {
@ -2516,19 +2540,7 @@ RefPtr<StyleValue> Parser::parse_linear_gradient_function(ComponentValue const&
if (has_direction_param && !tokens.next_token().is(Token::Type::Comma))
return {};
// <color-stop-list> =
// <linear-color-stop> , [ <linear-color-hint>? , <linear-color-stop> ]#
auto is_length_percentage = [](Dimension& dimension) {
return dimension.is_length_percentage();
};
auto get_length_percentage = [](Dimension& dimension) {
return dimension.length_percentage();
};
auto color_stops = parse_color_stop_list<LinearColorStopListElement>(
tokens, is_length_percentage, get_length_percentage,
[&](auto& token) { return parse_color(token); },
[&](auto& token) { return parse_dimension(token); });
auto color_stops = parse_linear_color_stop_list(tokens);
if (!color_stops.has_value())
return {};
@ -2625,19 +2637,7 @@ RefPtr<StyleValue> Parser::parse_conic_gradient_function(ComponentValue const& c
if ((got_from_angle || got_at_position || got_color_interpolation_method) && !tokens.next_token().is(Token::Type::Comma))
return {};
// <angular-color-stop-list> =
// <angular-color-stop> , [ <angular-color-hint>? , <angular-color-stop> ]#
auto is_angle_percentage = [](Dimension& dimension) {
return dimension.is_angle_percentage();
};
auto get_angle_percentage = [](Dimension& dimension) {
return dimension.angle_percentage();
};
auto color_stops = parse_color_stop_list<AngularColorStopListElement>(
tokens, is_angle_percentage, get_angle_percentage,
[&](auto& token) { return parse_color(token); },
[&](auto& token) { return parse_dimension(token); });
auto color_stops = parse_angular_color_stop_list(tokens);
if (!color_stops.has_value())
return {};