summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2022-02-22 12:10:48 +0000
committerAndreas Kling <kling@serenityos.org>2022-02-24 08:04:25 +0100
commitf76a54181988d2d5213424061e8ef79e54659404 (patch)
treee35b62a1c551ea94e7abf0aa669920a0f83819cd /Userland
parent608bfac2a9dfa5f54a4584f044de2fa6eb2aaec2 (diff)
downloadserenity-f76a54181988d2d5213424061e8ef79e54659404.zip
LibWeb: Move length-unit-from-string code into Length
This means the units are defined in a single place instead of two. Also removed the verify that we didn't produce a bogus % dimension token in the Tokenizer, since this has never happened and the parser is not a tokenizer test suite. :^)
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibWeb/CSS/Length.cpp37
-rw-r--r--Userland/Libraries/LibWeb/CSS/Length.h2
-rw-r--r--Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp40
3 files changed, 41 insertions, 38 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/Length.cpp b/Userland/Libraries/LibWeb/CSS/Length.cpp
index badb21bbd8..b149bb1c2a 100644
--- a/Userland/Libraries/LibWeb/CSS/Length.cpp
+++ b/Userland/Libraries/LibWeb/CSS/Length.cpp
@@ -156,4 +156,41 @@ const char* Length::unit_name() const
VERIFY_NOT_REACHED();
}
+Optional<Length::Type> Length::unit_from_name(StringView name)
+{
+ if (name.equals_ignoring_case("px"sv)) {
+ return Length::Type::Px;
+ } else if (name.equals_ignoring_case("pt"sv)) {
+ return Length::Type::Pt;
+ } else if (name.equals_ignoring_case("pc"sv)) {
+ return Length::Type::Pc;
+ } else if (name.equals_ignoring_case("mm"sv)) {
+ return Length::Type::Mm;
+ } else if (name.equals_ignoring_case("rem"sv)) {
+ return Length::Type::Rem;
+ } else if (name.equals_ignoring_case("em"sv)) {
+ return Length::Type::Em;
+ } else if (name.equals_ignoring_case("ex"sv)) {
+ return Length::Type::Ex;
+ } else if (name.equals_ignoring_case("ch"sv)) {
+ return Length::Type::Ch;
+ } else if (name.equals_ignoring_case("vw"sv)) {
+ return Length::Type::Vw;
+ } else if (name.equals_ignoring_case("vh"sv)) {
+ return Length::Type::Vh;
+ } else if (name.equals_ignoring_case("vmax"sv)) {
+ return Length::Type::Vmax;
+ } else if (name.equals_ignoring_case("vmin"sv)) {
+ return Length::Type::Vmin;
+ } else if (name.equals_ignoring_case("cm"sv)) {
+ return Length::Type::Cm;
+ } else if (name.equals_ignoring_case("in"sv)) {
+ return Length::Type::In;
+ } else if (name.equals_ignoring_case("Q"sv)) {
+ return Length::Type::Q;
+ }
+
+ return {};
+}
+
}
diff --git a/Userland/Libraries/LibWeb/CSS/Length.h b/Userland/Libraries/LibWeb/CSS/Length.h
index 4b979eba82..65ebaa8128 100644
--- a/Userland/Libraries/LibWeb/CSS/Length.h
+++ b/Userland/Libraries/LibWeb/CSS/Length.h
@@ -34,6 +34,8 @@ public:
Vmin,
};
+ static Optional<Type> unit_from_name(StringView);
+
// We have a RefPtr<CalculatedStyleValue> member, but can't include the header StyleValue.h as it includes
// this file already. To break the cyclic dependency, we must move all method definitions out.
Length(int value, Type type);
diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp
index 16a943f7c0..a7653f18b9 100644
--- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp
+++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp
@@ -2165,45 +2165,9 @@ Optional<Parser::Dimension> Parser::parse_dimension(StyleComponentValueRule cons
if (component_value.is(Token::Type::Dimension)) {
float numeric_value = component_value.token().dimension_value();
auto unit_string = component_value.token().dimension_unit();
- Optional<Length::Type> length_type;
-
- if (unit_string.equals_ignoring_case("px"sv)) {
- length_type = Length::Type::Px;
- } else if (unit_string.equals_ignoring_case("pt"sv)) {
- length_type = Length::Type::Pt;
- } else if (unit_string.equals_ignoring_case("pc"sv)) {
- length_type = Length::Type::Pc;
- } else if (unit_string.equals_ignoring_case("mm"sv)) {
- length_type = Length::Type::Mm;
- } else if (unit_string.equals_ignoring_case("rem"sv)) {
- length_type = Length::Type::Rem;
- } else if (unit_string.equals_ignoring_case("em"sv)) {
- length_type = Length::Type::Em;
- } else if (unit_string.equals_ignoring_case("ex"sv)) {
- length_type = Length::Type::Ex;
- } else if (unit_string.equals_ignoring_case("ch"sv)) {
- length_type = Length::Type::Ch;
- } else if (unit_string.equals_ignoring_case("vw"sv)) {
- length_type = Length::Type::Vw;
- } else if (unit_string.equals_ignoring_case("vh"sv)) {
- length_type = Length::Type::Vh;
- } else if (unit_string.equals_ignoring_case("vmax"sv)) {
- length_type = Length::Type::Vmax;
- } else if (unit_string.equals_ignoring_case("vmin"sv)) {
- length_type = Length::Type::Vmin;
- } else if (unit_string.equals_ignoring_case("cm"sv)) {
- length_type = Length::Type::Cm;
- } else if (unit_string.equals_ignoring_case("in"sv)) {
- length_type = Length::Type::In;
- } else if (unit_string.equals_ignoring_case("Q"sv)) {
- length_type = Length::Type::Q;
- } else if (unit_string.equals_ignoring_case("%"sv)) {
- // A number followed by `%` must always result in a Percentage token.
- VERIFY_NOT_REACHED();
- }
- if (length_type.has_value())
- return Length { numeric_value, length_type.value() };
+ if (auto length_type = Length::unit_from_name(unit_string); length_type.has_value())
+ return Length { numeric_value, length_type.release_value() };
}
if (component_value.is(Token::Type::Percentage))