mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 14:07:46 +00:00
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.
This commit is contained in:
parent
06617a982e
commit
fbfce2e73e
6 changed files with 69 additions and 3 deletions
|
@ -38,6 +38,18 @@ public:
|
||||||
return m_type == other.m_type && m_value == other.m_value;
|
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:
|
private:
|
||||||
StringView unit_name() const;
|
StringView unit_name() const;
|
||||||
|
|
||||||
|
|
|
@ -35,6 +35,18 @@ public:
|
||||||
return m_type == other.m_type && m_value == other.m_value;
|
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:
|
private:
|
||||||
StringView unit_name() const;
|
StringView unit_name() const;
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
* Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -82,6 +82,15 @@ public:
|
||||||
return m_type == other.m_type && m_value == other.m_value;
|
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:
|
private:
|
||||||
double m_value { 0 };
|
double m_value { 0 };
|
||||||
Type m_type;
|
Type m_type;
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
* Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -38,6 +38,15 @@ public:
|
||||||
|
|
||||||
bool operator==(Percentage const& other) const { return m_value == other.m_value; }
|
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:
|
private:
|
||||||
double m_value;
|
double m_value;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
|
* Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
|
||||||
*
|
*
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
@ -32,6 +32,18 @@ public:
|
||||||
return m_type == other.m_type && m_value == other.m_value;
|
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:
|
private:
|
||||||
StringView unit_name() const;
|
StringView unit_name() const;
|
||||||
|
|
||||||
|
|
|
@ -37,6 +37,18 @@ public:
|
||||||
return m_type == other.m_type && m_value == other.m_value;
|
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:
|
private:
|
||||||
StringView unit_name() const;
|
StringView unit_name() const;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue