1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 15:28:11 +00:00

LibWeb: Parse double-position linear-gradient() color stops

The only accepted syntax for these seems to be
<color> <length percentage> <length percentage>, no other order.

But that's just gathered from looking at other browsers as though
these are supported by all major browsers, they don't appear in
the W3C spec.
This commit is contained in:
MacDue 2022-08-22 21:37:53 +01:00 committed by Andreas Kling
parent ec3d8a7a18
commit 3a1f8d714a
4 changed files with 30 additions and 22 deletions

View file

@ -2509,15 +2509,16 @@ RefPtr<StyleValue> Parser::parse_linear_gradient_function(ComponentValue const&
auto& token = tokens.next_token();
Gfx::Color color;
Optional<LengthPercentage> length;
Optional<LengthPercentage> position;
Optional<LengthPercentage> second_position;
auto dimension = parse_dimension(token);
if (dimension.has_value() && dimension->is_length_percentage()) {
// [<length-percentage> <color>] or [<length-percentage>]
length = dimension->length_percentage();
position = dimension->length_percentage();
tokens.skip_whitespace();
// <length-percentage>
if (!tokens.has_next_token() || tokens.peek_token().is(Token::Type::Comma)) {
element.transition_hint = GradientColorHint { *length };
element.transition_hint = GradientColorHint { *position };
return ElementType::ColorHint;
}
// <length-percentage> <color>
@ -2532,16 +2533,21 @@ RefPtr<StyleValue> Parser::parse_linear_gradient_function(ComponentValue const&
return ElementType::Garbage;
color = *maybe_color;
tokens.skip_whitespace();
if (tokens.has_next_token() && !tokens.peek_token().is(Token::Type::Comma)) {
auto token = tokens.next_token();
auto dimension = parse_dimension(token);
if (!dimension.has_value() || !dimension->is_length_percentage())
return ElementType::Garbage;
length = dimension->length_percentage();
// Allow up to [<color> <length-percentage> <length-percentage>] (double-position color stops)
// Note: Double-position color stops only appear to be valid in this order.
for (auto stop_position : Array { &position, &second_position }) {
if (tokens.has_next_token() && !tokens.peek_token().is(Token::Type::Comma)) {
auto token = tokens.next_token();
auto dimension = parse_dimension(token);
if (!dimension.has_value() || !dimension->is_length_percentage())
return ElementType::Garbage;
*stop_position = dimension->length_percentage();
tokens.skip_whitespace();
}
}
}
element.color_stop = GradientColorStop { color, length };
element.color_stop = GradientColorStop { color, position, second_position };
return ElementType::ColorStop;
};