LibWeb: Store BackgroundSizeStyleValue sub-values directly

Storing these within LengthPercentage is unnecessary
This commit is contained in:
Callum Law 2025-10-02 16:50:43 +13:00 committed by Sam Atkins
commit 2ebf446cbf
Notes: github-actions[bot] 2025-10-07 09:51:23 +00:00
5 changed files with 21 additions and 52 deletions

View file

@ -1364,9 +1364,9 @@ static RefPtr<StyleValue const> interpolate_value_impl(DOM::Element& element, Ca
return AngleStyleValue::create(Angle::make_degrees(interpolated_value));
}
case StyleValue::Type::BackgroundSize: {
auto interpolated_x = interpolate_length_percentage_or_auto(calculation_context, from.as_background_size().size_x(), to.as_background_size().size_x(), delta);
auto interpolated_y = interpolate_length_percentage_or_auto(calculation_context, from.as_background_size().size_y(), to.as_background_size().size_y(), delta);
if (!interpolated_x.has_value() || !interpolated_y.has_value())
auto interpolated_x = interpolate_value(element, calculation_context, from.as_background_size().size_x(), to.as_background_size().size_x(), delta, allow_discrete);
auto interpolated_y = interpolate_value(element, calculation_context, from.as_background_size().size_y(), to.as_background_size().size_y(), delta, allow_discrete);
if (!interpolated_x || !interpolated_y)
return {};
return BackgroundSizeStyleValue::create(*interpolated_x, *interpolated_y);

View file

@ -1623,48 +1623,23 @@ RefPtr<StyleValue const> Parser::parse_single_background_size_value(PropertyID p
{
auto transaction = tokens.begin_transaction();
auto get_length_percentage_or_auto = [](StyleValue const& style_value) -> Optional<LengthPercentageOrAuto> {
if (style_value.has_auto())
return LengthPercentageOrAuto::make_auto();
if (style_value.is_percentage())
return LengthPercentage { style_value.as_percentage().percentage() };
if (style_value.is_length())
return LengthPercentage { style_value.as_length().length() };
if (style_value.is_calculated())
return LengthPercentage { style_value.as_calculated() };
return {};
};
auto maybe_x_value = parse_css_value_for_property(property, tokens);
if (!maybe_x_value)
return nullptr;
auto x_value = maybe_x_value.release_nonnull();
if (x_value->to_keyword() == Keyword::Cover || x_value->to_keyword() == Keyword::Contain) {
if (maybe_x_value->to_keyword() == Keyword::Cover || maybe_x_value->to_keyword() == Keyword::Contain) {
transaction.commit();
return x_value;
return maybe_x_value;
}
auto maybe_y_value = parse_css_value_for_property(property, tokens);
if (!maybe_y_value) {
auto y_value = LengthPercentageOrAuto::make_auto();
auto x_size = get_length_percentage_or_auto(*x_value);
if (!x_size.has_value())
return nullptr;
transaction.commit();
return BackgroundSizeStyleValue::create(x_size.value(), y_value);
return BackgroundSizeStyleValue::create(maybe_x_value.release_nonnull(), KeywordStyleValue::create(Keyword::Auto));
}
auto y_value = maybe_y_value.release_nonnull();
auto x_size = get_length_percentage_or_auto(*x_value);
auto y_size = get_length_percentage_or_auto(*y_value);
if (!x_size.has_value() || !y_size.has_value())
return nullptr;
transaction.commit();
return BackgroundSizeStyleValue::create(x_size.release_value(), y_size.release_value());
return BackgroundSizeStyleValue::create(maybe_x_value.release_nonnull(), maybe_y_value.release_nonnull());
}
// https://drafts.csswg.org/css-backgrounds-3/#propdef-border

View file

@ -11,7 +11,7 @@
namespace Web::CSS {
BackgroundSizeStyleValue::BackgroundSizeStyleValue(LengthPercentageOrAuto size_x, LengthPercentageOrAuto size_y)
BackgroundSizeStyleValue::BackgroundSizeStyleValue(ValueComparingNonnullRefPtr<StyleValue const> size_x, ValueComparingNonnullRefPtr<StyleValue const> size_y)
: StyleValueWithDefaultOperators(Type::BackgroundSize)
, m_properties { .size_x = move(size_x), .size_y = move(size_y) }
{
@ -21,21 +21,15 @@ BackgroundSizeStyleValue::~BackgroundSizeStyleValue() = default;
String BackgroundSizeStyleValue::to_string(SerializationMode mode) const
{
if (m_properties.size_x.is_auto() && m_properties.size_y.is_auto())
if (m_properties.size_x->has_auto() && m_properties.size_y->has_auto())
return "auto"_string;
return MUST(String::formatted("{} {}", m_properties.size_x.to_string(mode), m_properties.size_y.to_string(mode)));
return MUST(String::formatted("{} {}", m_properties.size_x->to_string(mode), m_properties.size_y->to_string(mode)));
}
ValueComparingNonnullRefPtr<StyleValue const> BackgroundSizeStyleValue::absolutized(ComputationContext const& computation_context) const
{
auto absolutize = [&](auto& size) -> LengthPercentageOrAuto {
if (size.is_auto())
return size;
return size.length_percentage().absolutized(computation_context);
};
auto absolutized_size_x = absolutize(m_properties.size_x);
auto absolutized_size_y = absolutize(m_properties.size_y);
auto absolutized_size_x = m_properties.size_x->absolutized(computation_context);
auto absolutized_size_y = m_properties.size_y->absolutized(computation_context);
if (absolutized_size_x == m_properties.size_x && absolutized_size_y == m_properties.size_y)
return *this;

View file

@ -18,14 +18,14 @@ namespace Web::CSS {
// NOTE: This is not used for identifier sizes, like `cover` and `contain`.
class BackgroundSizeStyleValue final : public StyleValueWithDefaultOperators<BackgroundSizeStyleValue> {
public:
static ValueComparingNonnullRefPtr<BackgroundSizeStyleValue const> create(LengthPercentageOrAuto size_x, LengthPercentageOrAuto size_y)
static ValueComparingNonnullRefPtr<BackgroundSizeStyleValue const> create(ValueComparingNonnullRefPtr<StyleValue const> size_x, ValueComparingNonnullRefPtr<StyleValue const> size_y)
{
return adopt_ref(*new (nothrow) BackgroundSizeStyleValue(size_x, size_y));
return adopt_ref(*new (nothrow) BackgroundSizeStyleValue(move(size_x), move(size_y)));
}
virtual ~BackgroundSizeStyleValue() override;
LengthPercentageOrAuto size_x() const { return m_properties.size_x; }
LengthPercentageOrAuto size_y() const { return m_properties.size_y; }
ValueComparingNonnullRefPtr<StyleValue const> size_x() const { return m_properties.size_x; }
ValueComparingNonnullRefPtr<StyleValue const> size_y() const { return m_properties.size_y; }
virtual String to_string(SerializationMode) const override;
virtual ValueComparingNonnullRefPtr<StyleValue const> absolutized(ComputationContext const&) const override;
@ -33,11 +33,11 @@ public:
bool properties_equal(BackgroundSizeStyleValue const& other) const { return m_properties == other.m_properties; }
private:
BackgroundSizeStyleValue(LengthPercentageOrAuto size_x, LengthPercentageOrAuto size_y);
BackgroundSizeStyleValue(ValueComparingNonnullRefPtr<StyleValue const> size_x, ValueComparingNonnullRefPtr<StyleValue const> size_y);
struct Properties {
LengthPercentageOrAuto size_x;
LengthPercentageOrAuto size_y;
ValueComparingNonnullRefPtr<StyleValue const> size_x;
ValueComparingNonnullRefPtr<StyleValue const> size_y;
bool operator==(Properties const&) const = default;
} m_properties;
};

View file

@ -492,8 +492,8 @@ void NodeWithStyle::apply_style(CSS::ComputedProperties const& computed_style)
if (size_value->is_background_size()) {
auto& size = size_value->as_background_size();
layer.size_type = CSS::BackgroundSize::LengthPercentage;
layer.size_x = size.size_x();
layer.size_y = size.size_y();
layer.size_x = CSS::LengthPercentageOrAuto::from_style_value(size.size_x());
layer.size_y = CSS::LengthPercentageOrAuto::from_style_value(size.size_y());
} else if (size_value->is_keyword()) {
switch (size_value->to_keyword()) {
case CSS::Keyword::Contain: