diff options
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Length.cpp | 37 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Length.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp | 40 |
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)) |