From fbfce2e73e4d5b8c08980bff92c16f6f756f3f20 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Wed, 31 May 2023 14:55:18 +0100 Subject: [PATCH] LibWeb: Add comparison operators to CSS numeric types (except Length) This is to make it easier to bounds-check their values during parsing. Length is left out because many length units are relative to the context in which they are used, and so we cannot easily compare `10px` and `1em`, for example. --- Userland/Libraries/LibWeb/CSS/Angle.h | 12 ++++++++++++ Userland/Libraries/LibWeb/CSS/Frequency.h | 12 ++++++++++++ Userland/Libraries/LibWeb/CSS/Number.h | 11 ++++++++++- Userland/Libraries/LibWeb/CSS/Percentage.h | 11 ++++++++++- Userland/Libraries/LibWeb/CSS/Resolution.h | 14 +++++++++++++- Userland/Libraries/LibWeb/CSS/Time.h | 12 ++++++++++++ 6 files changed, 69 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/Angle.h b/Userland/Libraries/LibWeb/CSS/Angle.h index f65cc602ef..e6d1c94459 100644 --- a/Userland/Libraries/LibWeb/CSS/Angle.h +++ b/Userland/Libraries/LibWeb/CSS/Angle.h @@ -38,6 +38,18 @@ public: return m_type == other.m_type && m_value == other.m_value; } + int operator<=>(Angle const& other) const + { + auto this_degrees = to_degrees(); + auto other_degrees = other.to_degrees(); + + if (this_degrees < other_degrees) + return -1; + if (this_degrees > other_degrees) + return 1; + return 0; + } + private: StringView unit_name() const; diff --git a/Userland/Libraries/LibWeb/CSS/Frequency.h b/Userland/Libraries/LibWeb/CSS/Frequency.h index 7e8075cda1..855aa1f798 100644 --- a/Userland/Libraries/LibWeb/CSS/Frequency.h +++ b/Userland/Libraries/LibWeb/CSS/Frequency.h @@ -35,6 +35,18 @@ public: return m_type == other.m_type && m_value == other.m_value; } + int operator<=>(Frequency const& other) const + { + auto this_hertz = to_hertz(); + auto other_hertz = other.to_hertz(); + + if (this_hertz < other_hertz) + return -1; + if (this_hertz > other_hertz) + return 1; + return 0; + } + private: StringView unit_name() const; diff --git a/Userland/Libraries/LibWeb/CSS/Number.h b/Userland/Libraries/LibWeb/CSS/Number.h index 1a9a9f3ea3..832a7d95fc 100644 --- a/Userland/Libraries/LibWeb/CSS/Number.h +++ b/Userland/Libraries/LibWeb/CSS/Number.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Sam Atkins + * Copyright (c) 2022-2023, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -82,6 +82,15 @@ public: return m_type == other.m_type && m_value == other.m_value; } + int operator<=>(Number const& other) const + { + if (m_value < other.m_value) + return -1; + if (m_value > other.m_value) + return 1; + return 0; + } + private: double m_value { 0 }; Type m_type; diff --git a/Userland/Libraries/LibWeb/CSS/Percentage.h b/Userland/Libraries/LibWeb/CSS/Percentage.h index 59566524af..4cd50abf85 100644 --- a/Userland/Libraries/LibWeb/CSS/Percentage.h +++ b/Userland/Libraries/LibWeb/CSS/Percentage.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Sam Atkins + * Copyright (c) 2022-2023, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -38,6 +38,15 @@ public: bool operator==(Percentage const& other) const { return m_value == other.m_value; } + int operator<=>(Percentage const& other) const + { + if (m_value < other.m_value) + return -1; + if (m_value > other.m_value) + return 1; + return 0; + } + private: double m_value; }; diff --git a/Userland/Libraries/LibWeb/CSS/Resolution.h b/Userland/Libraries/LibWeb/CSS/Resolution.h index 390f6b2aa8..6a693775d3 100644 --- a/Userland/Libraries/LibWeb/CSS/Resolution.h +++ b/Userland/Libraries/LibWeb/CSS/Resolution.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022, Sam Atkins + * Copyright (c) 2022-2023, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -32,6 +32,18 @@ public: return m_type == other.m_type && m_value == other.m_value; } + int operator<=>(Resolution const& other) const + { + auto this_dots_per_pixel = to_dots_per_pixel(); + auto other_dots_per_pixel = other.to_dots_per_pixel(); + + if (this_dots_per_pixel < other_dots_per_pixel) + return -1; + if (this_dots_per_pixel > other_dots_per_pixel) + return 1; + return 0; + } + private: StringView unit_name() const; diff --git a/Userland/Libraries/LibWeb/CSS/Time.h b/Userland/Libraries/LibWeb/CSS/Time.h index 7c1a00c2c0..dca75618b5 100644 --- a/Userland/Libraries/LibWeb/CSS/Time.h +++ b/Userland/Libraries/LibWeb/CSS/Time.h @@ -37,6 +37,18 @@ public: return m_type == other.m_type && m_value == other.m_value; } + int operator<=>(Time const& other) const + { + auto this_seconds = to_seconds(); + auto other_seconds = other.to_seconds(); + + if (this_seconds < other_seconds) + return -1; + if (this_seconds > other_seconds) + return 1; + return 0; + } + private: StringView unit_name() const;