LibWeb: Remove StyleValue::has/to_number()

Only NumericStyleValue holds numbers.

Renamed `to_number()` to `number()` because it's just a getter now.
This commit is contained in:
Sam Atkins 2023-05-26 17:27:32 +01:00 committed by Andreas Kling
parent 5f755d721e
commit 4ecf0b7768
Notes: sideshowbarker 2024-07-17 02:05:41 +09:00
4 changed files with 14 additions and 15 deletions

View File

@ -15,6 +15,7 @@
#include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
#include <LibWeb/CSS/StyleValues/NumericStyleValue.h>
#include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
#include <LibWeb/CSS/StyleValues/RectStyleValue.h>
#include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
@ -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<CSS::FlexBasisData> 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<CSS::Transformation> 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 {

View File

@ -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<ValueComparingNonnullRefPtr<StyleValue const>> 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<Layout::NodeWithStyle const&>) 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<String> to_string() const = 0;

View File

@ -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; },

View File

@ -10,6 +10,7 @@
#include <LibWeb/CSS/StyleValues/BackgroundSizeStyleValue.h>
#include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
#include <LibWeb/CSS/StyleValues/EdgeStyleValue.h>
#include <LibWeb/CSS/StyleValues/NumericStyleValue.h>
#include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
#include <LibWeb/CSS/StyleValues/StyleValueList.h>
#include <LibWeb/CSS/StyleValues/URLStyleValue.h>
@ -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())