diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2022-02-22 12:48:59 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-02-24 08:04:25 +0100 |
commit | 5c8ea81d21f573e19a0d1a398b187e8fe9e956c6 (patch) | |
tree | d4345f49986450bbff043d006edc4450ac4b5458 /Userland | |
parent | f76a54181988d2d5213424061e8ef79e54659404 (diff) | |
download | serenity-5c8ea81d21f573e19a0d1a398b187e8fe9e956c6.zip |
LibWeb: Parse Angle/Frequency/Resolution/Time types
Diffstat (limited to 'Userland')
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp | 112 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Parser/Parser.h | 47 |
2 files changed, 151 insertions, 8 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index a7653f18b9..66a1771a60 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -2168,6 +2168,18 @@ Optional<Parser::Dimension> Parser::parse_dimension(StyleComponentValueRule cons if (auto length_type = Length::unit_from_name(unit_string); length_type.has_value()) return Length { numeric_value, length_type.release_value() }; + + if (auto angle_type = Angle::unit_from_name(unit_string); angle_type.has_value()) + return Angle { numeric_value, angle_type.release_value() }; + + if (auto frequency_type = Frequency::unit_from_name(unit_string); frequency_type.has_value()) + return Frequency { numeric_value, frequency_type.release_value() }; + + if (auto resolution_type = Resolution::unit_from_name(unit_string); resolution_type.has_value()) + return Resolution { numeric_value, resolution_type.release_value() }; + + if (auto time_type = Time::unit_from_name(unit_string); time_type.has_value()) + return Time { numeric_value, time_type.release_value() }; } if (component_value.is(Token::Type::Percentage)) @@ -2222,10 +2234,18 @@ RefPtr<StyleValue> Parser::parse_dimension_value(StyleComponentValueRule const& if (!dimension.has_value()) return {}; + if (dimension->is_angle()) + return AngleStyleValue::create(dimension->angle()); + if (dimension->is_frequency()) + return FrequencyStyleValue::create(dimension->frequency()); if (dimension->is_length()) return LengthStyleValue::create(dimension->length()); if (dimension->is_percentage()) return PercentageStyleValue::create(dimension->percentage()); + if (dimension->is_resolution()) + return ResolutionStyleValue::create(dimension->resolution()); + if (dimension->is_time()) + return TimeStyleValue::create(dimension->time()); VERIFY_NOT_REACHED(); } @@ -4535,6 +4555,54 @@ RefPtr<StyleValue> Parser::parse_css_value(Badge<StyleComputer>, ParsingContext return result.release_value(); } +bool Parser::Dimension::is_angle() const +{ + return m_value.has<Angle>(); +} + +Angle Parser::Dimension::angle() const +{ + return m_value.get<Angle>(); +} + +bool Parser::Dimension::is_angle_percentage() const +{ + return is_angle() || is_percentage(); +} + +AnglePercentage Parser::Dimension::angle_percentage() const +{ + if (is_angle()) + return angle(); + if (is_percentage()) + return percentage(); + VERIFY_NOT_REACHED(); +} + +bool Parser::Dimension::is_frequency() const +{ + return m_value.has<Frequency>(); +} + +Frequency Parser::Dimension::frequency() const +{ + return m_value.get<Frequency>(); +} + +bool Parser::Dimension::is_frequency_percentage() const +{ + return is_frequency() || is_percentage(); +} + +FrequencyPercentage Parser::Dimension::frequency_percentage() const +{ + if (is_frequency()) + return frequency(); + if (is_percentage()) + return percentage(); + VERIFY_NOT_REACHED(); +} + bool Parser::Dimension::is_length() const { return m_value.has<Length>(); @@ -4545,6 +4613,20 @@ Length Parser::Dimension::length() const return m_value.get<Length>(); } +bool Parser::Dimension::is_length_percentage() const +{ + return is_length() || is_percentage(); +} + +LengthPercentage Parser::Dimension::length_percentage() const +{ + if (is_length()) + return length(); + if (is_percentage()) + return percentage(); + VERIFY_NOT_REACHED(); +} + bool Parser::Dimension::is_percentage() const { return m_value.has<Percentage>(); @@ -4555,15 +4637,35 @@ Percentage Parser::Dimension::percentage() const return m_value.get<Percentage>(); } -bool Parser::Dimension::is_length_percentage() const +bool Parser::Dimension::is_resolution() const { - return is_length() || is_percentage(); + return m_value.has<Resolution>(); } -LengthPercentage Parser::Dimension::length_percentage() const +Resolution Parser::Dimension::resolution() const { - if (is_length()) - return length(); + return m_value.get<Resolution>(); +} + +bool Parser::Dimension::is_time() const +{ + return m_value.has<Time>(); +} + +Time Parser::Dimension::time() const +{ + return m_value.get<Time>(); +} + +bool Parser::Dimension::is_time_percentage() const +{ + return is_time() || is_percentage(); +} + +TimePercentage Parser::Dimension::time_percentage() const +{ + if (is_time()) + return time(); if (is_percentage()) return percentage(); VERIFY_NOT_REACHED(); diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index 6a9e073c53..febe81110b 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -178,6 +178,16 @@ private: class Dimension { public: + Dimension(Angle&& value) + : m_value(move(value)) + { + } + + Dimension(Frequency&& value) + : m_value(move(value)) + { + } + Dimension(Length&& value) : m_value(move(value)) { @@ -187,17 +197,48 @@ private: { } + Dimension(Resolution&& value) + : m_value(move(value)) + { + } + + Dimension(Time&& value) + : m_value(move(value)) + { + } + + bool is_angle() const; + Angle angle() const; + + bool is_angle_percentage() const; + AnglePercentage angle_percentage() const; + + bool is_frequency() const; + Frequency frequency() const; + + bool is_frequency_percentage() const; + FrequencyPercentage frequency_percentage() const; + bool is_length() const; Length length() const; + bool is_length_percentage() const; + LengthPercentage length_percentage() const; + bool is_percentage() const; Percentage percentage() const; - bool is_length_percentage() const; - LengthPercentage length_percentage() const; + bool is_resolution() const; + Resolution resolution() const; + + bool is_time() const; + Time time() const; + + bool is_time_percentage() const; + TimePercentage time_percentage() const; private: - Variant<Length, Percentage> m_value; + Variant<Angle, Frequency, Length, Percentage, Resolution, Time> m_value; }; Optional<Dimension> parse_dimension(StyleComponentValueRule const&); Optional<Color> parse_color(StyleComponentValueRule const&); |