From 5cbf6eb930206ea5ea0d490acb57204795d68f3e Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 26 May 2023 17:43:20 +0100 Subject: [PATCH] LibWeb: Remove StyleValue::has/to_integer() Only NumericStyleValue holds integers. I'm not sure our current distinction between NumericStyleValue holding an integer or non-integer is useful given it always returns a float. :thonk: --- Userland/Libraries/LibWeb/CSS/StyleProperties.cpp | 8 ++++---- Userland/Libraries/LibWeb/CSS/StyleValue.cpp | 4 ++-- Userland/Libraries/LibWeb/CSS/StyleValue.h | 2 -- .../Libraries/LibWeb/CSS/StyleValues/NumericStyleValue.h | 4 ++-- Userland/Libraries/LibWeb/Layout/Node.cpp | 2 +- 5 files changed, 9 insertions(+), 11 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp b/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp index b888b44af0..6e083fed7f 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp @@ -226,10 +226,10 @@ Optional StyleProperties::z_index() const auto value = property(CSS::PropertyID::ZIndex); if (value->has_auto()) return {}; - if (value->has_integer()) { + if (value->is_numeric() && value->as_numeric().has_integer()) { // Clamp z-index to the range of a signed 32-bit integer for consistency with other engines. // NOTE: Casting between 32-bit float and 32-bit integer is finicky here, since INT32_MAX is not representable as a 32-bit float! - auto integer = value->to_integer(); + auto integer = value->as_numeric().integer(); if (integer >= static_cast(NumericLimits::max())) return NumericLimits::max(); if (integer <= static_cast(NumericLimits::min())) @@ -344,9 +344,9 @@ float StyleProperties::flex_shrink() const int StyleProperties::order() const { auto value = property(CSS::PropertyID::Order); - if (!value->has_integer()) + if (!value->is_numeric() || !value->as_numeric().has_integer()) return 0; - return value->to_integer(); + return value->as_numeric().integer(); } Optional StyleProperties::image_rendering() const diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp index 60937e2df9..a97fa95d2b 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp @@ -395,8 +395,8 @@ int StyleValue::to_font_weight() const return Gfx::FontWeight::Regular; } } - if (has_integer()) { - return to_integer(); + if (is_numeric() && as_numeric().has_integer()) { + return as_numeric().integer(); } if (is_calculated()) { auto maybe_weight = const_cast(as_calculated()).resolve_integer(); diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValue.h index 16297dfb32..5e10039398 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.h @@ -294,14 +294,12 @@ public: bool has_auto() const; virtual bool has_color() const { return false; } virtual bool has_length() const { return false; } - virtual bool has_integer() const { return false; } virtual ErrorOr> absolutized(CSSPixelRect const& viewport_rect, Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const; virtual Color to_color(Optional) const { return {}; } ValueID to_identifier() const; virtual Length to_length() const { VERIFY_NOT_REACHED(); } - virtual float to_integer() const { return 0; } virtual ErrorOr to_string() const = 0; [[nodiscard]] int to_font_weight() const; diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/NumericStyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValues/NumericStyleValue.h index c435454f80..7895ed422e 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/NumericStyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/NumericStyleValue.h @@ -35,8 +35,8 @@ public: [](i64 value) { return (float)value; }); } - virtual bool has_integer() const override { return m_value.has(); } - virtual float to_integer() const override { return m_value.get(); } + bool has_integer() const { return m_value.has(); } + float integer() const { return m_value.get(); } virtual ErrorOr to_string() const override; diff --git a/Userland/Libraries/LibWeb/Layout/Node.cpp b/Userland/Libraries/LibWeb/Layout/Node.cpp index a0a7922494..6804d63ed7 100644 --- a/Userland/Libraries/LibWeb/Layout/Node.cpp +++ b/Userland/Libraries/LibWeb/Layout/Node.cpp @@ -295,7 +295,7 @@ void NodeWithStyle::apply_style(const CSS::StyleProperties& computed_style) // That's why it has to be set before everything else. m_font = computed_style.computed_font(); computed_values.set_font_size(computed_style.property(CSS::PropertyID::FontSize)->to_length().to_px(*this).value()); - computed_values.set_font_weight(computed_style.property(CSS::PropertyID::FontWeight)->to_integer()); + computed_values.set_font_weight(computed_style.property(CSS::PropertyID::FontWeight)->as_numeric().integer()); m_line_height = computed_style.line_height(*this); computed_values.set_vertical_align(computed_style.vertical_align());