1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-30 23:58:12 +00:00

LibWeb: Add helper for creating resolved values for sided shorthands

...and use it for `margin` and `padding`.

So now, if all four margins are 12px, we output "12px" instead of "12px
12px 12px 12px". This matches the spec's requirement to use the smallest
non-ambiguous representation.
This commit is contained in:
Sam Atkins 2023-05-27 16:24:33 +01:00 committed by Andreas Kling
parent e8dc8f697b
commit abdd4a8733

View file

@ -227,6 +227,23 @@ static ErrorOr<NonnullRefPtr<StyleValue const>> style_value_for_size(Size const&
TODO(); TODO();
} }
static ErrorOr<NonnullRefPtr<StyleValue const>> style_value_for_sided_shorthand(ValueComparingNonnullRefPtr<StyleValue const> top, ValueComparingNonnullRefPtr<StyleValue const> right, ValueComparingNonnullRefPtr<StyleValue const> bottom, ValueComparingNonnullRefPtr<StyleValue const> left)
{
bool top_and_bottom_same = top == bottom;
bool left_and_right_same = left == right;
if (top_and_bottom_same && left_and_right_same && top == left)
return top;
if (top_and_bottom_same && left_and_right_same)
return StyleValueList::create(StyleValueVector { move(top), move(right) }, StyleValueList::Separator::Space);
if (left_and_right_same)
return StyleValueList::create(StyleValueVector { move(top), move(right), move(bottom) }, StyleValueList::Separator::Space);
return StyleValueList::create(StyleValueVector { move(top), move(right), move(bottom), move(left) }, StyleValueList::Separator::Space);
}
ErrorOr<RefPtr<StyleValue const>> ResolvedCSSStyleDeclaration::style_value_for_property(Layout::NodeWithStyle const& layout_node, PropertyID property_id) const ErrorOr<RefPtr<StyleValue const>> ResolvedCSSStyleDeclaration::style_value_for_property(Layout::NodeWithStyle const& layout_node, PropertyID property_id) const
{ {
switch (property_id) { switch (property_id) {
@ -608,13 +625,11 @@ ErrorOr<RefPtr<StyleValue const>> ResolvedCSSStyleDeclaration::style_value_for_p
return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().list_style_type())); return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().list_style_type()));
case PropertyID::Margin: { case PropertyID::Margin: {
auto margin = layout_node.computed_values().margin(); auto margin = layout_node.computed_values().margin();
auto values = StyleValueVector {}; auto top = TRY(style_value_for_length_percentage(margin.top()));
TRY(values.try_ensure_capacity(4)); auto right = TRY(style_value_for_length_percentage(margin.right()));
values.unchecked_append(TRY(style_value_for_length_percentage(margin.top()))); auto bottom = TRY(style_value_for_length_percentage(margin.bottom()));
values.unchecked_append(TRY(style_value_for_length_percentage(margin.right()))); auto left = TRY(style_value_for_length_percentage(margin.left()));
values.unchecked_append(TRY(style_value_for_length_percentage(margin.bottom()))); return style_value_for_sided_shorthand(move(top), move(right), move(bottom), move(left));
values.unchecked_append(TRY(style_value_for_length_percentage(margin.left())));
return StyleValueList::create(move(values), StyleValueList::Separator::Space);
} }
case PropertyID::MarginBottom: case PropertyID::MarginBottom:
return style_value_for_length_percentage(layout_node.computed_values().margin().bottom()); return style_value_for_length_percentage(layout_node.computed_values().margin().bottom());
@ -642,13 +657,11 @@ ErrorOr<RefPtr<StyleValue const>> ResolvedCSSStyleDeclaration::style_value_for_p
return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().overflow_y())); return IdentifierStyleValue::create(to_value_id(layout_node.computed_values().overflow_y()));
case PropertyID::Padding: { case PropertyID::Padding: {
auto padding = layout_node.computed_values().padding(); auto padding = layout_node.computed_values().padding();
auto values = StyleValueVector {}; auto top = TRY(style_value_for_length_percentage(padding.top()));
TRY(values.try_ensure_capacity(4)); auto right = TRY(style_value_for_length_percentage(padding.right()));
values.unchecked_append(TRY(style_value_for_length_percentage(padding.top()))); auto bottom = TRY(style_value_for_length_percentage(padding.bottom()));
values.unchecked_append(TRY(style_value_for_length_percentage(padding.right()))); auto left = TRY(style_value_for_length_percentage(padding.left()));
values.unchecked_append(TRY(style_value_for_length_percentage(padding.bottom()))); return style_value_for_sided_shorthand(move(top), move(right), move(bottom), move(left));
values.unchecked_append(TRY(style_value_for_length_percentage(padding.left())));
return StyleValueList::create(move(values), StyleValueList::Separator::Space);
} }
case PropertyID::PaddingBottom: case PropertyID::PaddingBottom:
return style_value_for_length_percentage(layout_node.computed_values().padding().bottom()); return style_value_for_length_percentage(layout_node.computed_values().padding().bottom());