From 5f2f780662a493bf33d2fd799483a2ff2c62f5ea Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Tue, 11 Apr 2023 12:57:32 +0100 Subject: [PATCH] LibWeb: Expose type and raw values of basic CSS types This makes it possible to do arithmetic on them without having to resolve to their canonical unit, which often requires context information that is not available until the last minute. For example, a Length cannot be resolved to px without knowing the font size, parent element's size, etc. Only Length currently requires such context, but treating all these types the same means that code that manipulates them does not need to know or care if a new unit gets added that does require contextual information. --- Userland/Libraries/LibWeb/CSS/Angle.h | 3 +++ Userland/Libraries/LibWeb/CSS/Frequency.h | 3 +++ Userland/Libraries/LibWeb/CSS/Length.h | 1 + Userland/Libraries/LibWeb/CSS/Number.h | 1 + Userland/Libraries/LibWeb/CSS/Time.h | 3 +++ 5 files changed, 11 insertions(+) diff --git a/Userland/Libraries/LibWeb/CSS/Angle.h b/Userland/Libraries/LibWeb/CSS/Angle.h index 740110ba76..b7f7e1c3fa 100644 --- a/Userland/Libraries/LibWeb/CSS/Angle.h +++ b/Userland/Libraries/LibWeb/CSS/Angle.h @@ -30,6 +30,9 @@ public: ErrorOr to_string() const; float to_degrees() const; + Type type() const { return m_type; } + float raw_value() const { return m_value; } + bool operator==(Angle const& other) const { return m_type == other.m_type && m_value == other.m_value; diff --git a/Userland/Libraries/LibWeb/CSS/Frequency.h b/Userland/Libraries/LibWeb/CSS/Frequency.h index 15f3786515..b98b42df1f 100644 --- a/Userland/Libraries/LibWeb/CSS/Frequency.h +++ b/Userland/Libraries/LibWeb/CSS/Frequency.h @@ -27,6 +27,9 @@ public: ErrorOr to_string() const; float to_hertz() const; + Type type() const { return m_type; } + float raw_value() const { return m_value; } + bool operator==(Frequency const& other) const { return m_type == other.m_type && m_value == other.m_value; diff --git a/Userland/Libraries/LibWeb/CSS/Length.h b/Userland/Libraries/LibWeb/CSS/Length.h index d9f5a0af2d..1e7add791f 100644 --- a/Userland/Libraries/LibWeb/CSS/Length.h +++ b/Userland/Libraries/LibWeb/CSS/Length.h @@ -79,6 +79,7 @@ public: || m_type == Type::Rlh; } + Type type() const { return m_type; } float raw_value() const { return m_value; } CSSPixels to_px(Layout::Node const&) const; diff --git a/Userland/Libraries/LibWeb/CSS/Number.h b/Userland/Libraries/LibWeb/CSS/Number.h index d90d01db61..a9d4b1303e 100644 --- a/Userland/Libraries/LibWeb/CSS/Number.h +++ b/Userland/Libraries/LibWeb/CSS/Number.h @@ -31,6 +31,7 @@ public: { } + Type type() const { return m_type; } float value() const { return m_value; } i64 integer_value() const { diff --git a/Userland/Libraries/LibWeb/CSS/Time.h b/Userland/Libraries/LibWeb/CSS/Time.h index fc1768731b..6a4ade9b40 100644 --- a/Userland/Libraries/LibWeb/CSS/Time.h +++ b/Userland/Libraries/LibWeb/CSS/Time.h @@ -28,6 +28,9 @@ public: ErrorOr to_string() const; float to_seconds() const; + Type type() const { return m_type; } + float raw_value() const { return m_value; } + bool operator==(Time const& other) const { return m_type == other.m_type && m_value == other.m_value;