mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-10-16 21:20:18 +00:00
LibWeb: Store BackgroundSizeStyleValue
sub-values directly
Storing these within LengthPercentage is unnecessary
This commit is contained in:
parent
309ff33278
commit
2ebf446cbf
Notes:
github-actions[bot]
2025-10-07 09:51:23 +00:00
Author: https://github.com/Calme1709
Commit: 2ebf446cbf
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/6375
Reviewed-by: https://github.com/AtkinsSJ ✅
5 changed files with 21 additions and 52 deletions
|
@ -1364,9 +1364,9 @@ static RefPtr<StyleValue const> interpolate_value_impl(DOM::Element& element, Ca
|
||||||
return AngleStyleValue::create(Angle::make_degrees(interpolated_value));
|
return AngleStyleValue::create(Angle::make_degrees(interpolated_value));
|
||||||
}
|
}
|
||||||
case StyleValue::Type::BackgroundSize: {
|
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_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_length_percentage_or_auto(calculation_context, from.as_background_size().size_y(), to.as_background_size().size_y(), delta);
|
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.has_value() || !interpolated_y.has_value())
|
if (!interpolated_x || !interpolated_y)
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
return BackgroundSizeStyleValue::create(*interpolated_x, *interpolated_y);
|
return BackgroundSizeStyleValue::create(*interpolated_x, *interpolated_y);
|
||||||
|
|
|
@ -1623,48 +1623,23 @@ RefPtr<StyleValue const> Parser::parse_single_background_size_value(PropertyID p
|
||||||
{
|
{
|
||||||
auto transaction = tokens.begin_transaction();
|
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);
|
auto maybe_x_value = parse_css_value_for_property(property, tokens);
|
||||||
if (!maybe_x_value)
|
if (!maybe_x_value)
|
||||||
return nullptr;
|
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();
|
transaction.commit();
|
||||||
return x_value;
|
return maybe_x_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto maybe_y_value = parse_css_value_for_property(property, tokens);
|
auto maybe_y_value = parse_css_value_for_property(property, tokens);
|
||||||
if (!maybe_y_value) {
|
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();
|
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();
|
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
|
// https://drafts.csswg.org/css-backgrounds-3/#propdef-border
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
|
|
||||||
namespace Web::CSS {
|
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)
|
: StyleValueWithDefaultOperators(Type::BackgroundSize)
|
||||||
, m_properties { .size_x = move(size_x), .size_y = move(size_y) }
|
, 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
|
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 "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
|
ValueComparingNonnullRefPtr<StyleValue const> BackgroundSizeStyleValue::absolutized(ComputationContext const& computation_context) const
|
||||||
{
|
{
|
||||||
auto absolutize = [&](auto& size) -> LengthPercentageOrAuto {
|
auto absolutized_size_x = m_properties.size_x->absolutized(computation_context);
|
||||||
if (size.is_auto())
|
auto absolutized_size_y = m_properties.size_y->absolutized(computation_context);
|
||||||
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);
|
|
||||||
|
|
||||||
if (absolutized_size_x == m_properties.size_x && absolutized_size_y == m_properties.size_y)
|
if (absolutized_size_x == m_properties.size_x && absolutized_size_y == m_properties.size_y)
|
||||||
return *this;
|
return *this;
|
||||||
|
|
|
@ -18,14 +18,14 @@ namespace Web::CSS {
|
||||||
// NOTE: This is not used for identifier sizes, like `cover` and `contain`.
|
// NOTE: This is not used for identifier sizes, like `cover` and `contain`.
|
||||||
class BackgroundSizeStyleValue final : public StyleValueWithDefaultOperators<BackgroundSizeStyleValue> {
|
class BackgroundSizeStyleValue final : public StyleValueWithDefaultOperators<BackgroundSizeStyleValue> {
|
||||||
public:
|
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;
|
virtual ~BackgroundSizeStyleValue() override;
|
||||||
|
|
||||||
LengthPercentageOrAuto size_x() const { return m_properties.size_x; }
|
ValueComparingNonnullRefPtr<StyleValue const> size_x() const { return m_properties.size_x; }
|
||||||
LengthPercentageOrAuto size_y() const { return m_properties.size_y; }
|
ValueComparingNonnullRefPtr<StyleValue const> size_y() const { return m_properties.size_y; }
|
||||||
|
|
||||||
virtual String to_string(SerializationMode) const override;
|
virtual String to_string(SerializationMode) const override;
|
||||||
virtual ValueComparingNonnullRefPtr<StyleValue const> absolutized(ComputationContext const&) 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; }
|
bool properties_equal(BackgroundSizeStyleValue const& other) const { return m_properties == other.m_properties; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
BackgroundSizeStyleValue(LengthPercentageOrAuto size_x, LengthPercentageOrAuto size_y);
|
BackgroundSizeStyleValue(ValueComparingNonnullRefPtr<StyleValue const> size_x, ValueComparingNonnullRefPtr<StyleValue const> size_y);
|
||||||
|
|
||||||
struct Properties {
|
struct Properties {
|
||||||
LengthPercentageOrAuto size_x;
|
ValueComparingNonnullRefPtr<StyleValue const> size_x;
|
||||||
LengthPercentageOrAuto size_y;
|
ValueComparingNonnullRefPtr<StyleValue const> size_y;
|
||||||
bool operator==(Properties const&) const = default;
|
bool operator==(Properties const&) const = default;
|
||||||
} m_properties;
|
} m_properties;
|
||||||
};
|
};
|
||||||
|
|
|
@ -492,8 +492,8 @@ void NodeWithStyle::apply_style(CSS::ComputedProperties const& computed_style)
|
||||||
if (size_value->is_background_size()) {
|
if (size_value->is_background_size()) {
|
||||||
auto& size = size_value->as_background_size();
|
auto& size = size_value->as_background_size();
|
||||||
layer.size_type = CSS::BackgroundSize::LengthPercentage;
|
layer.size_type = CSS::BackgroundSize::LengthPercentage;
|
||||||
layer.size_x = size.size_x();
|
layer.size_x = CSS::LengthPercentageOrAuto::from_style_value(size.size_x());
|
||||||
layer.size_y = size.size_y();
|
layer.size_y = CSS::LengthPercentageOrAuto::from_style_value(size.size_y());
|
||||||
} else if (size_value->is_keyword()) {
|
} else if (size_value->is_keyword()) {
|
||||||
switch (size_value->to_keyword()) {
|
switch (size_value->to_keyword()) {
|
||||||
case CSS::Keyword::Contain:
|
case CSS::Keyword::Contain:
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue