diff options
author | Sam Atkins <atkinssj@serenityos.org> | 2022-04-27 14:58:25 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-04-29 00:07:31 +0200 |
commit | 28278efafbe0a9c1f177a09ab18d73f15452496e (patch) | |
tree | 04524a7f9057ccd666bd0c929a3d75cb671c65bd /Userland/Libraries/LibWeb/CSS | |
parent | 1f114eaebecd215e58bae2b4c1c5cdf7966aecdc (diff) | |
download | serenity-28278efafbe0a9c1f177a09ab18d73f15452496e.zip |
LibWeb: Extract MediaType to/from_string() logic into functions
Diffstat (limited to 'Userland/Libraries/LibWeb/CSS')
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/MediaQuery.cpp | 92 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/MediaQuery.h | 3 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp | 24 |
3 files changed, 62 insertions, 57 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/MediaQuery.cpp b/Userland/Libraries/LibWeb/CSS/MediaQuery.cpp index af2c4c8f8b..417a1c388d 100644 --- a/Userland/Libraries/LibWeb/CSS/MediaQuery.cpp +++ b/Userland/Libraries/LibWeb/CSS/MediaQuery.cpp @@ -326,41 +326,7 @@ String MediaQuery::to_string() const builder.append("not "); if (m_negated || m_media_type != MediaType::All || !m_media_condition) { - switch (m_media_type) { - case MediaType::All: - builder.append("all"); - break; - case MediaType::Aural: - builder.append("aural"); - break; - case MediaType::Braille: - builder.append("braille"); - break; - case MediaType::Embossed: - builder.append("embossed"); - break; - case MediaType::Handheld: - builder.append("handheld"); - break; - case MediaType::Print: - builder.append("print"); - break; - case MediaType::Projection: - builder.append("projection"); - break; - case MediaType::Screen: - builder.append("screen"); - break; - case MediaType::Speech: - builder.append("speech"); - break; - case MediaType::TTY: - builder.append("tty"); - break; - case MediaType::TV: - builder.append("tv"); - break; - } + builder.append(CSS::to_string(m_media_type)); if (m_media_condition) builder.append(" and "); } @@ -478,4 +444,60 @@ bool is_media_feature_name(StringView name) return false; } +Optional<MediaQuery::MediaType> media_type_from_string(StringView name) +{ + if (name.equals_ignoring_case("all"sv)) + return MediaQuery::MediaType::All; + if (name.equals_ignoring_case("aural"sv)) + return MediaQuery::MediaType::Aural; + if (name.equals_ignoring_case("braille"sv)) + return MediaQuery::MediaType::Braille; + if (name.equals_ignoring_case("embossed"sv)) + return MediaQuery::MediaType::Embossed; + if (name.equals_ignoring_case("handheld"sv)) + return MediaQuery::MediaType::Handheld; + if (name.equals_ignoring_case("print"sv)) + return MediaQuery::MediaType::Print; + if (name.equals_ignoring_case("projection"sv)) + return MediaQuery::MediaType::Projection; + if (name.equals_ignoring_case("screen"sv)) + return MediaQuery::MediaType::Screen; + if (name.equals_ignoring_case("speech"sv)) + return MediaQuery::MediaType::Speech; + if (name.equals_ignoring_case("tty"sv)) + return MediaQuery::MediaType::TTY; + if (name.equals_ignoring_case("tv"sv)) + return MediaQuery::MediaType::TV; + return {}; +} + +StringView to_string(MediaQuery::MediaType media_type) +{ + switch (media_type) { + case MediaQuery::MediaType::All: + return "all"sv; + case MediaQuery::MediaType::Aural: + return "aural"sv; + case MediaQuery::MediaType::Braille: + return "braille"sv; + case MediaQuery::MediaType::Embossed: + return "embossed"sv; + case MediaQuery::MediaType::Handheld: + return "handheld"sv; + case MediaQuery::MediaType::Print: + return "print"sv; + case MediaQuery::MediaType::Projection: + return "projection"sv; + case MediaQuery::MediaType::Screen: + return "screen"sv; + case MediaQuery::MediaType::Speech: + return "speech"sv; + case MediaQuery::MediaType::TTY: + return "tty"sv; + case MediaQuery::MediaType::TV: + return "tv"sv; + } + VERIFY_NOT_REACHED(); +} + } diff --git a/Userland/Libraries/LibWeb/CSS/MediaQuery.h b/Userland/Libraries/LibWeb/CSS/MediaQuery.h index a50d30508a..c66a351dff 100644 --- a/Userland/Libraries/LibWeb/CSS/MediaQuery.h +++ b/Userland/Libraries/LibWeb/CSS/MediaQuery.h @@ -258,6 +258,9 @@ String serialize_a_media_query_list(NonnullRefPtrVector<MediaQuery> const&); bool is_media_feature_name(StringView name); +Optional<MediaQuery::MediaType> media_type_from_string(StringView); +StringView to_string(MediaQuery::MediaType); + } namespace AK { diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index f6c6a50bea..88c38d5fee 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -1202,28 +1202,8 @@ Optional<MediaQuery::MediaType> Parser::parse_media_type(TokenStream<ComponentVa } auto ident = token.token().ident(); - if (ident.equals_ignoring_case("all")) { - return MediaQuery::MediaType::All; - } else if (ident.equals_ignoring_case("aural")) { - return MediaQuery::MediaType::Aural; - } else if (ident.equals_ignoring_case("braille")) { - return MediaQuery::MediaType::Braille; - } else if (ident.equals_ignoring_case("embossed")) { - return MediaQuery::MediaType::Embossed; - } else if (ident.equals_ignoring_case("handheld")) { - return MediaQuery::MediaType::Handheld; - } else if (ident.equals_ignoring_case("print")) { - return MediaQuery::MediaType::Print; - } else if (ident.equals_ignoring_case("projection")) { - return MediaQuery::MediaType::Projection; - } else if (ident.equals_ignoring_case("screen")) { - return MediaQuery::MediaType::Screen; - } else if (ident.equals_ignoring_case("speech")) { - return MediaQuery::MediaType::Speech; - } else if (ident.equals_ignoring_case("tty")) { - return MediaQuery::MediaType::TTY; - } else if (ident.equals_ignoring_case("tv")) { - return MediaQuery::MediaType::TV; + if (auto media_type = media_type_from_string(ident); media_type.has_value()) { + return media_type.release_value(); } tokens.rewind_to_position(position); |