diff --git a/Meta/gn/secondary/Userland/Libraries/LibWeb/CSS/BUILD.gn b/Meta/gn/secondary/Userland/Libraries/LibWeb/CSS/BUILD.gn index 12a80600d8..076a33f87f 100644 --- a/Meta/gn/secondary/Userland/Libraries/LibWeb/CSS/BUILD.gn +++ b/Meta/gn/secondary/Userland/Libraries/LibWeb/CSS/BUILD.gn @@ -40,7 +40,6 @@ source_set("CSS") { "MediaQueryList.cpp", "MediaQueryListEvent.cpp", "PercentageOr.cpp", - "Position.cpp", "PreferredColorScheme.cpp", "Ratio.cpp", "Resolution.cpp", diff --git a/Userland/Libraries/LibWeb/CMakeLists.txt b/Userland/Libraries/LibWeb/CMakeLists.txt index 303d14ef60..8638d42ab9 100644 --- a/Userland/Libraries/LibWeb/CMakeLists.txt +++ b/Userland/Libraries/LibWeb/CMakeLists.txt @@ -67,7 +67,6 @@ set(SOURCES CSS/Parser/Token.cpp CSS/Parser/Tokenizer.cpp CSS/PercentageOr.cpp - CSS/Position.cpp CSS/PreferredColorScheme.cpp CSS/Ratio.cpp CSS/Resolution.cpp diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index 8dea7cbb1a..339ace8189 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -1189,220 +1189,6 @@ RefPtr Parser::parse_url_value(ComponentValue const& component_value return URLStyleValue::create(*url); } -Optional Parser::parse_position(TokenStream& tokens, PositionValue initial_value) -{ - auto transaction = tokens.begin_transaction(); - tokens.skip_whitespace(); - if (!tokens.has_next_token()) - return {}; - - auto parse_horizontal_preset = [&](auto ident) -> Optional { - if (ident.equals_ignoring_ascii_case("left"sv)) - return PositionValue::HorizontalPreset::Left; - if (ident.equals_ignoring_ascii_case("center"sv)) - return PositionValue::HorizontalPreset::Center; - if (ident.equals_ignoring_ascii_case("right"sv)) - return PositionValue::HorizontalPreset::Right; - return {}; - }; - - auto parse_vertical_preset = [&](auto ident) -> Optional { - if (ident.equals_ignoring_ascii_case("top"sv)) - return PositionValue::VerticalPreset::Top; - if (ident.equals_ignoring_ascii_case("center"sv)) - return PositionValue::VerticalPreset::Center; - if (ident.equals_ignoring_ascii_case("bottom"sv)) - return PositionValue::VerticalPreset::Bottom; - return {}; - }; - - auto parse_horizontal_edge = [&](auto ident) -> Optional { - if (ident.equals_ignoring_ascii_case("left"sv)) - return PositionValue::HorizontalEdge::Left; - if (ident.equals_ignoring_ascii_case("right"sv)) - return PositionValue::HorizontalEdge::Right; - return {}; - }; - - auto parse_vertical_edge = [&](auto ident) -> Optional { - if (ident.equals_ignoring_ascii_case("top"sv)) - return PositionValue::VerticalEdge::Top; - if (ident.equals_ignoring_ascii_case("bottom"sv)) - return PositionValue::VerticalEdge::Bottom; - return {}; - }; - - // = [ - // [ left | center | right ] || [ top | center | bottom ] - // | - // [ left | center | right | ] - // [ top | center | bottom | ]? - // | - // [ [ left | right ] ] && - // [ [ top | bottom ] ] - // ] - - // [ left | center | right ] || [ top | center | bottom ] - auto alternation_1 = [&]() -> Optional { - auto transaction = tokens.begin_transaction(); - PositionValue position = initial_value; - auto& first_token = tokens.next_token(); - if (!first_token.is(Token::Type::Ident)) - return {}; - auto ident = first_token.token().ident(); - // ? - auto horizontal_position = parse_horizontal_preset(ident); - if (horizontal_position.has_value()) { - position.horizontal_position = *horizontal_position; - auto transaction_optional_parse = tokens.begin_transaction(); - tokens.skip_whitespace(); - if (tokens.has_next_token()) { - auto& second_token = tokens.next_token(); - if (second_token.is(Token::Type::Ident)) { - auto vertical_position = parse_vertical_preset(second_token.token().ident()); - if (vertical_position.has_value()) { - transaction_optional_parse.commit(); - position.vertical_position = *vertical_position; - } - } - } - } else { - // ? - auto vertical_position = parse_vertical_preset(ident); - if (!vertical_position.has_value()) - return {}; - position.vertical_position = *vertical_position; - auto transaction_optional_parse = tokens.begin_transaction(); - tokens.skip_whitespace(); - if (tokens.has_next_token()) { - auto& second_token = tokens.next_token(); - if (second_token.is(Token::Type::Ident)) { - auto horizontal_position = parse_horizontal_preset(second_token.token().ident()); - if (horizontal_position.has_value()) { - transaction_optional_parse.commit(); - position.horizontal_position = *horizontal_position; - } - } - } - } - transaction.commit(); - return position; - }; - - // [ left | center | right | ] - // [ top | center | bottom | ]? - auto alternation_2 = [&]() -> Optional { - auto transaction = tokens.begin_transaction(); - PositionValue position = initial_value; - auto& first_token = tokens.next_token(); - if (first_token.is(Token::Type::Ident)) { - auto horizontal_position = parse_horizontal_preset(first_token.token().ident()); - if (!horizontal_position.has_value()) - return {}; - position.horizontal_position = *horizontal_position; - } else { - auto dimension = parse_dimension(first_token); - if (!dimension.has_value() || !dimension->is_length_percentage()) - return {}; - position.horizontal_position = dimension->length_percentage(); - } - auto transaction_optional_parse = tokens.begin_transaction(); - tokens.skip_whitespace(); - if (tokens.has_next_token()) { - auto& second_token = tokens.next_token(); - if (second_token.is(Token::Type::Ident)) { - auto vertical_position = parse_vertical_preset(second_token.token().ident()); - if (vertical_position.has_value()) { - transaction_optional_parse.commit(); - position.vertical_position = *vertical_position; - } - } else { - auto dimension = parse_dimension(second_token); - if (dimension.has_value() && dimension->is_length_percentage()) { - transaction_optional_parse.commit(); - position.vertical_position = dimension->length_percentage(); - } - } - } - transaction.commit(); - return position; - }; - - // [ [ left | right ] ] && - // [ [ top | bottom ] ] - auto alternation_3 = [&]() -> Optional { - auto transaction = tokens.begin_transaction(); - PositionValue position {}; - - auto parse_horizontal = [&] { - // [ left | right ] ] - auto transaction = tokens.begin_transaction(); - tokens.skip_whitespace(); - if (!tokens.has_next_token()) - return false; - auto& first_token = tokens.next_token(); - if (!first_token.is(Token::Type::Ident)) - return false; - auto horizontal_egde = parse_horizontal_edge(first_token.token().ident()); - if (!horizontal_egde.has_value()) - return false; - position.x_relative_to = *horizontal_egde; - tokens.skip_whitespace(); - if (!tokens.has_next_token()) - return false; - auto& second_token = tokens.next_token(); - auto dimension = parse_dimension(second_token); - if (!dimension.has_value() || !dimension->is_length_percentage()) - return false; - position.horizontal_position = dimension->length_percentage(); - transaction.commit(); - return true; - }; - - auto parse_vertical = [&] { - // [ top | bottom ] ] - auto transaction = tokens.begin_transaction(); - tokens.skip_whitespace(); - if (!tokens.has_next_token()) - return false; - auto& first_token = tokens.next_token(); - if (!first_token.is(Token::Type::Ident)) - return false; - auto vertical_edge = parse_vertical_edge(first_token.token().ident()); - if (!vertical_edge.has_value()) - return false; - position.y_relative_to = *vertical_edge; - tokens.skip_whitespace(); - if (!tokens.has_next_token()) - return false; - auto& second_token = tokens.next_token(); - auto dimension = parse_dimension(second_token); - if (!dimension.has_value() || !dimension->is_length_percentage()) - return false; - position.vertical_position = dimension->length_percentage(); - transaction.commit(); - return true; - }; - - if ((parse_horizontal() && parse_vertical()) || (parse_vertical() && parse_horizontal())) { - transaction.commit(); - return position; - } - - return {}; - }; - - // Note: The alternatives must be attempted in this order since `alternation_2' can match a prefix of `alternation_3' - auto position = alternation_3(); - if (!position.has_value()) - position = alternation_2(); - if (!position.has_value()) - position = alternation_1(); - if (position.has_value()) - transaction.commit(); - return position; -} - CSSRule* Parser::convert_to_rule(NonnullRefPtr rule) { if (rule->is_at_rule()) { diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index 49f9f4ae97..f81fc619b1 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -24,7 +24,6 @@ #include #include #include -#include #include #include #include @@ -183,7 +182,6 @@ private: Optional parse_min_max(Vector const&); Optional parse_repeat(Vector const&); Optional parse_track_sizing_function(ComponentValue const&); - Optional parse_position(TokenStream&, PositionValue initial_value = PositionValue::center()); Optional parse_url_function(ComponentValue const&); RefPtr parse_url_value(ComponentValue const&); diff --git a/Userland/Libraries/LibWeb/CSS/Position.cpp b/Userland/Libraries/LibWeb/CSS/Position.cpp deleted file mode 100644 index 8dab7861c0..0000000000 --- a/Userland/Libraries/LibWeb/CSS/Position.cpp +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright (c) 2022-2023, MacDue - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#include "Position.h" -#include -#include - -namespace Web::CSS { - -CSSPixelPoint PositionValue::resolved(Layout::Node const& node, CSSPixelRect const& rect) const -{ - // Note: A preset + a none default x/y_relative_to is impossible in the syntax (and makes little sense) - CSSPixels x = horizontal_position.visit( - [&](HorizontalPreset preset) -> CSSPixels { - switch (preset) { - case HorizontalPreset::Left: - return 0; - case HorizontalPreset::Center: - return rect.width() / 2; - case HorizontalPreset::Right: - return rect.width(); - default: - VERIFY_NOT_REACHED(); - } - }, - [&](LengthPercentage length_percentage) -> CSSPixels { - return length_percentage.to_px(node, rect.width()); - }); - CSSPixels y = vertical_position.visit( - [&](VerticalPreset preset) -> CSSPixels { - switch (preset) { - case VerticalPreset::Top: - return 0; - case VerticalPreset::Center: - return rect.height() / 2; - case VerticalPreset::Bottom: - return rect.height(); - default: - VERIFY_NOT_REACHED(); - } - }, - [&](LengthPercentage length_percentage) -> CSSPixels { - return length_percentage.to_px(node, rect.height()); - }); - if (x_relative_to == HorizontalEdge::Right) - x = rect.width() - x; - if (y_relative_to == VerticalEdge::Bottom) - y = rect.height() - y; - return CSSPixelPoint { rect.x() + x, rect.y() + y }; -} - -void PositionValue::serialize(StringBuilder& builder) const -{ - // Note: This means our serialization with simplify any with explicit edges that are just `top left`. - bool has_relative_edges = x_relative_to == HorizontalEdge::Right || y_relative_to == VerticalEdge::Bottom; - if (has_relative_edges) - builder.append(x_relative_to == HorizontalEdge::Left ? "left "sv : "right "sv); - horizontal_position.visit( - [&](HorizontalPreset preset) { - builder.append([&] { - switch (preset) { - case HorizontalPreset::Left: - return "left"sv; - case HorizontalPreset::Center: - return "center"sv; - case HorizontalPreset::Right: - return "right"sv; - default: - VERIFY_NOT_REACHED(); - } - }()); - }, - [&](LengthPercentage length_percentage) { - builder.append(length_percentage.to_string()); - }); - builder.append(' '); - if (has_relative_edges) - builder.append(y_relative_to == VerticalEdge::Top ? "top "sv : "bottom "sv); - vertical_position.visit( - [&](VerticalPreset preset) { - builder.append([&] { - switch (preset) { - case VerticalPreset::Top: - return "top"sv; - case VerticalPreset::Center: - return "center"sv; - case VerticalPreset::Bottom: - return "bottom"sv; - default: - VERIFY_NOT_REACHED(); - } - }()); - }, - [&](LengthPercentage length_percentage) { - builder.append(length_percentage.to_string()); - }); -} - -} diff --git a/Userland/Libraries/LibWeb/CSS/Position.h b/Userland/Libraries/LibWeb/CSS/Position.h deleted file mode 100644 index a494a71d08..0000000000 --- a/Userland/Libraries/LibWeb/CSS/Position.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright (c) 2022-2023, MacDue - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include -#include - -namespace Web::CSS { - -// FIXME: Named PositionValue to avoid conflicts with enums, but this represents a -struct PositionValue { - enum class HorizontalPreset { - Left, - Center, - Right - }; - - enum class VerticalPreset { - Top, - Center, - Bottom - }; - - enum class HorizontalEdge { - Left, - Right - }; - - enum class VerticalEdge { - Top, - Bottom - }; - - static PositionValue center() - { - return PositionValue { HorizontalPreset::Center, VerticalPreset::Center }; - } - - Variant horizontal_position { HorizontalPreset::Left }; - Variant vertical_position { VerticalPreset::Top }; - HorizontalEdge x_relative_to { HorizontalEdge::Left }; - VerticalEdge y_relative_to { VerticalEdge::Top }; - - CSSPixelPoint resolved(Layout::Node const& node, CSSPixelRect const& rect) const; - void serialize(StringBuilder&) const; - bool operator==(PositionValue const&) const = default; -}; - -}