From 4ecf0b7768a7f00b2e93058d1d551ba45187c6b9 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Fri, 26 May 2023 17:27:32 +0100 Subject: [PATCH] LibWeb: Remove StyleValue::has/to_number() Only NumericStyleValue holds numbers. Renamed `to_number()` to `number()` because it's just a getter now. --- .../Libraries/LibWeb/CSS/StyleProperties.cpp | 19 ++++++++++--------- Userland/Libraries/LibWeb/CSS/StyleValue.h | 2 -- .../CSS/StyleValues/NumericStyleValue.h | 5 ++--- Userland/Libraries/LibWeb/Layout/Node.cpp | 3 ++- 4 files changed, 14 insertions(+), 15 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp b/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp index 9da166f6381..b888b44af0b 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -170,7 +171,7 @@ CSSPixels StyleProperties::line_height(CSSPixelRect const& viewport_rect, Length } if (line_height->is_numeric()) - return Length(line_height->to_number(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics); + return Length(line_height->as_numeric().number(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics); if (line_height->is_percentage()) { // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage @@ -200,7 +201,7 @@ CSSPixels StyleProperties::line_height(Layout::Node const& layout_node) const } if (line_height->is_numeric()) - return Length(line_height->to_number(), Length::Type::Em).to_px(layout_node); + return Length(line_height->as_numeric().number(), Length::Type::Em).to_px(layout_node); if (line_height->is_percentage()) { // Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage @@ -242,8 +243,8 @@ static float resolve_opacity_value(CSS::StyleValue const& value) { float unclamped_opacity = 1.0f; - if (value.has_number()) { - unclamped_opacity = value.to_number(); + if (value.is_numeric()) { + unclamped_opacity = value.as_numeric().number(); } else if (value.is_calculated()) { auto& calculated = value.as_calculated(); if (calculated.resolved_type() == CalculatedStyleValue::ResolvedType::Percentage) { @@ -327,17 +328,17 @@ Optional StyleProperties::flex_basis() const float StyleProperties::flex_grow() const { auto value = property(CSS::PropertyID::FlexGrow); - if (!value->has_number()) + if (!value->is_numeric()) return 0; - return value->to_number(); + return value->as_numeric().number(); } float StyleProperties::flex_shrink() const { auto value = property(CSS::PropertyID::FlexShrink); - if (!value->has_number()) + if (!value->is_numeric()) return 1; - return value->to_number(); + return value->as_numeric().number(); } int StyleProperties::order() const @@ -395,7 +396,7 @@ Vector StyleProperties::transformations() const } else if (transformation_value->is_percentage()) { values.append({ transformation_value->as_percentage().percentage() }); } else if (transformation_value->is_numeric()) { - values.append({ transformation_value->to_number() }); + values.append({ transformation_value->as_numeric().number() }); } else if (transformation_value->is_angle()) { values.append({ transformation_value->as_angle().angle() }); } else { diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValue.h index c9cf4ca8ddf..16297dfb323 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.h @@ -294,7 +294,6 @@ public: bool has_auto() const; virtual bool has_color() const { return false; } virtual bool has_length() const { return false; } - virtual bool has_number() 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; @@ -302,7 +301,6 @@ public: virtual Color to_color(Optional) const { return {}; } ValueID to_identifier() const; virtual Length to_length() const { VERIFY_NOT_REACHED(); } - virtual float to_number() const { return 0; } virtual float to_integer() const { return 0; } virtual ErrorOr to_string() const = 0; diff --git a/Userland/Libraries/LibWeb/CSS/StyleValues/NumericStyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValues/NumericStyleValue.h index 59e6b86063b..c435454f805 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValues/NumericStyleValue.h +++ b/Userland/Libraries/LibWeb/CSS/StyleValues/NumericStyleValue.h @@ -25,11 +25,10 @@ public: return adopt_nonnull_ref_or_enomem(new (nothrow) NumericStyleValue(value)); } - virtual bool has_length() const override { return to_number() == 0; } + virtual bool has_length() const override { return number() == 0; } virtual Length to_length() const override { return Length::make_px(0); } - virtual bool has_number() const override { return true; } - virtual float to_number() const override + float number() const { return m_value.visit( [](float value) { return value; }, diff --git a/Userland/Libraries/LibWeb/Layout/Node.cpp b/Userland/Libraries/LibWeb/Layout/Node.cpp index 956e56120f6..a0a79224948 100644 --- a/Userland/Libraries/LibWeb/Layout/Node.cpp +++ b/Userland/Libraries/LibWeb/Layout/Node.cpp @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -676,7 +677,7 @@ void NodeWithStyle::apply_style(const CSS::StyleProperties& computed_style) // FIXME: Converting to pixels isn't really correct - values should be in "user units" // https://svgwg.org/svg2-draft/coords.html#TermUserUnits if (stroke_width->is_numeric()) - computed_values.set_stroke_width(CSS::Length::make_px(stroke_width->to_number())); + computed_values.set_stroke_width(CSS::Length::make_px(stroke_width->as_numeric().number())); else if (stroke_width->is_length()) computed_values.set_stroke_width(stroke_width->to_length()); else if (stroke_width->is_percentage())