1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-16 19:45:07 +00:00

LibWeb: Resolve more background-related properties

This change adds rules for background-attachment, background-clip,
background-image, background-origin, and background-repeat.

As a result, Window.getComputedStyle() will no longer return empty
strings on these properties after a page is loaded, and the background
shorthand will show the resolved values instead of the default ones for
these values. :^)
This commit is contained in:
Karol Kosek 2023-03-30 23:26:28 +02:00 committed by Sam Atkins
parent e32ab161ae
commit e8fe35b1e5

View file

@ -70,6 +70,19 @@ DeprecatedString ResolvedCSSStyleDeclaration::item(size_t index) const
return {};
}
static NonnullRefPtr<StyleValue const> style_value_for_background_property(Layout::NodeWithStyle const& layout_node, Function<NonnullRefPtr<StyleValue const>(BackgroundLayerData const&)> callback, Function<NonnullRefPtr<StyleValue const>()> default_value)
{
auto const& background_layers = layout_node.background_layers();
if (background_layers.is_empty())
return default_value();
if (background_layers.size() == 1)
return callback(background_layers.first());
StyleValueVector values;
for (auto const& layer : background_layers)
values.append(callback(layer));
return StyleValueList::create(move(values), StyleValueList::Separator::Comma);
}
static RefPtr<StyleValue> style_value_for_display(CSS::Display display)
{
if (display.is_none())
@ -203,8 +216,43 @@ RefPtr<StyleValue const> ResolvedCSSStyleDeclaration::style_value_for_property(L
value_or_default(maybe_background_origin, IdentifierStyleValue::create(CSS::ValueID::PaddingBox)),
value_or_default(maybe_background_clip, IdentifierStyleValue::create(CSS::ValueID::BorderBox)));
}
case CSS::PropertyID::BackgroundAttachment:
return style_value_for_background_property(
layout_node,
[](auto& layer) { return IdentifierStyleValue::create(to_value_id(layer.attachment)); },
[] { return IdentifierStyleValue::create(CSS::ValueID::Scroll); });
case CSS::PropertyID::BackgroundClip:
return style_value_for_background_property(
layout_node,
[](auto& layer) { return IdentifierStyleValue::create(to_value_id(layer.clip)); },
[] { return IdentifierStyleValue::create(CSS::ValueID::BorderBox); });
case PropertyID::BackgroundColor:
return ColorStyleValue::create(layout_node.computed_values().background_color());
case CSS::PropertyID::BackgroundImage:
return style_value_for_background_property(
layout_node,
[](auto& layer) -> NonnullRefPtr<StyleValue const> {
if (layer.background_image)
return *layer.background_image;
return IdentifierStyleValue::create(CSS::ValueID::None);
},
[] { return IdentifierStyleValue::create(CSS::ValueID::None); });
case CSS::PropertyID::BackgroundOrigin:
return style_value_for_background_property(
layout_node,
[](auto& layer) { return IdentifierStyleValue::create(to_value_id(layer.origin)); },
[] { return IdentifierStyleValue::create(CSS::ValueID::PaddingBox); });
case CSS::PropertyID::BackgroundRepeat:
return style_value_for_background_property(
layout_node,
[](auto& layer) {
StyleValueVector repeat {
IdentifierStyleValue::create(to_value_id(layer.repeat_x)),
IdentifierStyleValue::create(to_value_id(layer.repeat_y)),
};
return StyleValueList::create(move(repeat), StyleValueList::Separator::Space);
},
[] { return BackgroundRepeatStyleValue::create(CSS::Repeat::Repeat, CSS::Repeat::Repeat); });
case CSS::PropertyID::BorderBottom: {
auto border = layout_node.computed_values().border_bottom();
auto width = LengthStyleValue::create(Length::make_px(border.width));