LibWeb: Parse background-position as ShorthandStyleValue

This makes us consistent with how we handle this value within the
`background` shorthand and allows us to remove the special handling in
`StyleComputer::for_each_property_expanding_shorthands`
This commit is contained in:
Callum Law 2025-12-07 19:02:28 +13:00 committed by Sam Atkins
parent 540b26e426
commit 020c4aadff
Notes: github-actions[bot] 2025-12-08 11:42:25 +00:00
3 changed files with 22 additions and 32 deletions

View file

@ -461,6 +461,7 @@ private:
RefPtr<StyleValue const> parse_aspect_ratio_value(TokenStream<ComponentValue>&);
RefPtr<StyleValue const> parse_animation_value(TokenStream<ComponentValue>&);
RefPtr<StyleValue const> parse_background_value(TokenStream<ComponentValue>&);
RefPtr<StyleValue const> parse_background_position_value(TokenStream<ComponentValue>&);
RefPtr<StyleValue const> parse_single_background_position_x_or_y_value(TokenStream<ComponentValue>&, PropertyID);
RefPtr<StyleValue const> parse_single_background_size_value(PropertyID, TokenStream<ComponentValue>&);
RefPtr<StyleValue const> parse_border_value(PropertyID, TokenStream<ComponentValue>&);

View file

@ -529,9 +529,7 @@ Parser::ParseErrorOr<NonnullRefPtr<StyleValue const>> Parser::parse_css_value(Pr
case PropertyID::Background:
return parse_all_as(tokens, [this](auto& tokens) { return parse_background_value(tokens); });
case PropertyID::BackgroundPosition:
return parse_all_as(tokens, [this](auto& tokens) {
return parse_comma_separated_value_list(tokens, [this](auto& tokens) { return parse_position_value(tokens, PositionParsingMode::BackgroundPosition); });
});
return parse_all_as(tokens, [this](auto& tokens) { return parse_background_position_value(tokens); });
case PropertyID::BackgroundPositionX:
case PropertyID::BackgroundPositionY:
return parse_all_as(tokens, [this, property_id](auto& tokens) {
@ -1175,6 +1173,26 @@ RefPtr<StyleValue const> Parser::parse_animation_value(TokenStream<ComponentValu
return parse_coordinating_value_list_shorthand(tokens, PropertyID::Animation, longhand_ids, { PropertyID::AnimationTimeline });
}
RefPtr<StyleValue const> Parser::parse_background_position_value(TokenStream<ComponentValue>& tokens)
{
auto const& background_position_value = parse_comma_separated_value_list(tokens, [this](auto& tokens) { return parse_position_value(tokens, PositionParsingMode::BackgroundPosition); });
if (!background_position_value)
return nullptr;
StyleValueVector background_position_x_values;
StyleValueVector background_position_y_values;
for (auto const& background_position : background_position_value->values()) {
background_position_x_values.append(background_position->as_position().edge_x());
background_position_y_values.append(background_position->as_position().edge_y());
}
return ShorthandStyleValue::create(PropertyID::BackgroundPosition,
{ PropertyID::BackgroundPositionX, PropertyID::BackgroundPositionY },
{ StyleValueList::create(move(background_position_x_values), StyleValueList::Separator::Comma), StyleValueList::create(move(background_position_y_values), StyleValueList::Separator::Comma) });
}
RefPtr<StyleValue const> Parser::parse_background_value(TokenStream<ComponentValue>& tokens)
{
auto transaction = tokens.begin_transaction();

View file

@ -399,35 +399,6 @@ void StyleComputer::for_each_property_expanding_shorthands(PropertyID property_i
return;
}
// FIXME: We should parse BackgroundPosition as a ShorthandStyleValue instead
if (property_id == CSS::PropertyID::BackgroundPosition) {
if (value.is_value_list()) {
// Expand background-position layer list into separate lists for x and y positions:
auto const& values_list = value.as_value_list();
StyleValueVector x_positions {};
StyleValueVector y_positions {};
x_positions.ensure_capacity(values_list.size());
y_positions.ensure_capacity(values_list.size());
for (auto& layer : values_list.values()) {
if (layer->is_position()) {
auto const& position = layer->as_position();
x_positions.unchecked_append(position.edge_x());
y_positions.unchecked_append(position.edge_y());
} else {
x_positions.unchecked_append(layer);
y_positions.unchecked_append(layer);
}
}
set_longhand_property(CSS::PropertyID::BackgroundPositionX, StyleValueList::create(move(x_positions), values_list.separator()));
set_longhand_property(CSS::PropertyID::BackgroundPositionY, StyleValueList::create(move(y_positions), values_list.separator()));
} else {
set_longhand_property(CSS::PropertyID::BackgroundPositionX, value);
set_longhand_property(CSS::PropertyID::BackgroundPositionY, value);
}
return;
}
if (property_is_shorthand(property_id)) {
// ShorthandStyleValue was handled already, as were unresolved shorthands.
// That means the only values we should see are the CSS-wide keywords, or the guaranteed-invalid value.