1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:47:36 +00:00

LibWeb: Use new StyleValue parsing for background-position[-x,-y]

This commit is contained in:
Sam Atkins 2023-05-18 15:46:49 +01:00 committed by Andreas Kling
parent b0fe07cba3
commit a0ec05ef81

View file

@ -4461,15 +4461,12 @@ ErrorOr<RefPtr<StyleValue>> Parser::parse_single_background_position_value(Token
if (seen_items == 2) if (seen_items == 2)
break; break;
auto const& token = tokens.peek_token(); auto maybe_value = TRY(parse_css_value_for_property(PropertyID::BackgroundPosition, tokens));
auto maybe_value = TRY(parse_css_value(token)); if (!maybe_value)
if (!maybe_value || !property_accepts_value(PropertyID::BackgroundPosition, *maybe_value))
break; break;
tokens.next_token();
auto value = maybe_value.release_nonnull(); auto value = maybe_value.release_nonnull();
auto offset = style_value_to_length_percentage(value); if (auto offset = style_value_to_length_percentage(value); offset.has_value()) {
if (offset.has_value()) {
if (!horizontal.has_value()) { if (!horizontal.has_value()) {
horizontal = EdgeOffset { PositionEdge::Left, *offset, false, true }; horizontal = EdgeOffset { PositionEdge::Left, *offset, false, true };
} else if (!vertical.has_value()) { } else if (!vertical.has_value()) {
@ -4481,15 +4478,15 @@ ErrorOr<RefPtr<StyleValue>> Parser::parse_single_background_position_value(Token
} }
auto try_parse_offset = [&](bool& offset_provided) -> ErrorOr<LengthPercentage> { auto try_parse_offset = [&](bool& offset_provided) -> ErrorOr<LengthPercentage> {
auto transaction = tokens.begin_transaction();
if (tokens.has_next_token()) { if (tokens.has_next_token()) {
auto& token = tokens.peek_token(); auto maybe_value = TRY(parse_css_value_for_property(PropertyID::BackgroundPosition, tokens));
auto maybe_value = TRY(parse_css_value(token));
if (!maybe_value) if (!maybe_value)
return zero_offset; return zero_offset;
auto offset = style_value_to_length_percentage(maybe_value.release_nonnull()); auto offset = style_value_to_length_percentage(maybe_value.release_nonnull());
if (offset.has_value()) { if (offset.has_value()) {
offset_provided = true; offset_provided = true;
tokens.next_token(); transaction.commit();
return *offset; return *offset;
} }
} }
@ -4574,14 +4571,10 @@ ErrorOr<RefPtr<StyleValue>> Parser::parse_single_background_position_x_or_y_valu
if (!tokens.has_next_token()) if (!tokens.has_next_token())
return nullptr; return nullptr;
auto parse_value = [&](auto& token) -> ErrorOr<RefPtr<StyleValue>> { auto value = TRY(parse_css_value_for_property(property, tokens));
auto maybe_value = TRY(parse_css_value(token)); if (!value)
if (!maybe_value || !property_accepts_value(property, *maybe_value)) return nullptr;
return nullptr;
return maybe_value.release_nonnull();
};
auto value = TRY(parse_value(tokens.next_token()));
if (value->is_identifier()) { if (value->is_identifier()) {
auto identifier = value->to_identifier(); auto identifier = value->to_identifier();
if (identifier == ValueID::Center) { if (identifier == ValueID::Center) {
@ -4594,12 +4587,11 @@ ErrorOr<RefPtr<StyleValue>> Parser::parse_single_background_position_x_or_y_valu
return nullptr; return nullptr;
} }
if (tokens.has_next_token()) { if (tokens.has_next_token()) {
value = TRY(parse_value(tokens.peek_token())); value = TRY(parse_css_value_for_property(property, tokens));
if (!value) { if (!value) {
transaction.commit(); transaction.commit();
return EdgeStyleValue::create(relative_edge, Length::make_px(0)); return EdgeStyleValue::create(relative_edge, Length::make_px(0));
} }
tokens.next_token();
} }
} }