From d00a6ca11f5c4fe23c87141e2de32db7a72002d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?kleines=20Filmr=C3=B6llchen?= Date: Tue, 7 Feb 2023 21:10:15 +0100 Subject: [PATCH] AK+LibWeb: Implement Variant equality operator And make use of it for CSS StyleValues. --- AK/Variant.h | 9 ++++++ Userland/Libraries/LibWeb/CSS/StyleValue.cpp | 34 ++------------------ 2 files changed, 12 insertions(+), 31 deletions(-) diff --git a/AK/Variant.h b/AK/Variant.h index ed477b7bd4..46ed67c48b 100644 --- a/AK/Variant.h +++ b/AK/Variant.h @@ -418,6 +418,15 @@ public: return index_of() == m_index; } + bool operator==(Variant const& other) const + { + return this->visit([&](T const& self) { + if (auto const* p = other.get_pointer()) + return static_cast(self) == static_cast(*p); + return false; + }); + } + template ALWAYS_INLINE decltype(auto) visit(Fs&&... functions) { diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp index e158482b6b..ca42cb4d2b 100644 --- a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp +++ b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp @@ -1331,25 +1331,6 @@ static bool operator==(Filter::HueRotate const& a, Filter::HueRotate const& b) return a.angle == b.angle; } -static bool variant_equals(auto const& a, auto const& b) -{ - return a.visit([&](auto const& held_value) { - using HeldType = AK::Detail::Decay; - bool other_holds_same_type = b.template has(); - return other_holds_same_type && held_value == b.template get(); - }); -} - -static bool operator==(Filter::HueRotate::AngleOrZero const& a, Filter::HueRotate::AngleOrZero const& b) -{ - return variant_equals(a, b); -} - -static bool operator==(FilterFunction const& a, FilterFunction const& b) -{ - return variant_equals(a, b); -} - bool FilterValueListStyleValue::equals(StyleValue const& other) const { if (type() != other.type()) @@ -1867,15 +1848,6 @@ ErrorOr LinearGradientStyleValue::to_string() const return builder.to_string(); } -static bool operator==(LinearGradientStyleValue::GradientDirection const& a, LinearGradientStyleValue::GradientDirection const& b) -{ - if (a.has() && b.has()) - return a.get() == b.get(); - if (a.has() && b.has()) - return a.get() == b.get(); - return false; -} - static bool operator==(EdgeRect const& a, EdgeRect const& b) { return a.top_edge == b.top_edge && a.right_edge == b.right_edge && a.bottom_edge == b.bottom_edge && a.left_edge == b.left_edge; @@ -2043,8 +2015,8 @@ bool PositionValue::operator==(PositionValue const& other) const return ( x_relative_to == other.x_relative_to && y_relative_to == other.y_relative_to - && variant_equals(horizontal_position, other.horizontal_position) - && variant_equals(vertical_position, other.vertical_position)); + && horizontal_position == other.horizontal_position + && vertical_position == other.vertical_position); } ErrorOr RadialGradientStyleValue::to_string() const @@ -2240,7 +2212,7 @@ bool RadialGradientStyleValue::equals(StyleValue const& other) const return false; auto& other_gradient = other.as_radial_gradient(); return (m_ending_shape == other_gradient.m_ending_shape - && variant_equals(m_size, other_gradient.m_size) + && m_size == other_gradient.m_size && m_position == other_gradient.m_position && m_color_stop_list == other_gradient.m_color_stop_list); }