diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index ea184fd9d9..5ee33377fe 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -2297,6 +2297,97 @@ Optional Parser::parse_url_function(ComponentValue const& component_val return {}; } +template +static Optional> parse_color_stop_list(auto& tokens, auto is_position, auto get_position, auto parse_color, auto parse_dimension) +{ + enum class ElementType { + Garbage, + ColorStop, + ColorHint + }; + + auto parse_color_stop_list_element = [&](TElement& element) -> ElementType { + tokens.skip_whitespace(); + if (!tokens.has_next_token()) + return ElementType::Garbage; + auto const& token = tokens.next_token(); + + Gfx::Color color; + Optional position; + Optional second_position; + auto dimension = parse_dimension(token); + if (dimension.has_value() && is_position(*dimension)) { + // [ ] or [] + position = get_position(*dimension); + tokens.skip_whitespace(); + // + if (!tokens.has_next_token() || tokens.peek_token().is(Token::Type::Comma)) { + element.transition_hint = typename TElement::ColorHint { *position }; + return ElementType::ColorHint; + } + // + auto maybe_color = parse_color(tokens.next_token()); + if (!maybe_color.has_value()) + return ElementType::Garbage; + color = *maybe_color; + } else { + // [ ?] + auto maybe_color = parse_color(token); + if (!maybe_color.has_value()) + return ElementType::Garbage; + color = *maybe_color; + tokens.skip_whitespace(); + // Allow up to [ ] (double-position color stops) + // Note: Double-position color stops only appear to be valid in this order. + for (auto stop_position : Array { &position, &second_position }) { + if (tokens.has_next_token() && !tokens.peek_token().is(Token::Type::Comma)) { + auto token = tokens.next_token(); + auto dimension = parse_dimension(token); + if (!dimension.has_value() || !is_position(*dimension)) + return ElementType::Garbage; + *stop_position = get_position(*dimension); + tokens.skip_whitespace(); + } + } + } + + element.color_stop = typename TElement::ColorStop { color, position, second_position }; + return ElementType::ColorStop; + }; + + TElement first_element {}; + if (parse_color_stop_list_element(first_element) != ElementType::ColorStop) + return {}; + + if (!tokens.has_next_token()) + return {}; + + Vector color_stops { first_element }; + while (tokens.has_next_token()) { + TElement list_element {}; + tokens.skip_whitespace(); + if (!tokens.next_token().is(Token::Type::Comma)) + return {}; + auto element_type = parse_color_stop_list_element(list_element); + if (element_type == ElementType::ColorHint) { + // , + tokens.skip_whitespace(); + if (!tokens.next_token().is(Token::Type::Comma)) + return {}; + // Note: This fills in the color stop on the same list_element as the color hint (it does not overwrite it). + if (parse_color_stop_list_element(list_element) != ElementType::ColorStop) + return {}; + } else if (element_type == ElementType::ColorStop) { + // + } else { + return {}; + } + color_stops.append(list_element); + } + + return color_stops; +} + RefPtr Parser::parse_linear_gradient_function(ComponentValue const& component_value) { using GradientType = LinearGradientStyleValue::GradientType; @@ -2429,93 +2520,123 @@ RefPtr Parser::parse_linear_gradient_function(ComponentValue const& // = // , [ ? , ]# - - enum class ElementType { - Garbage, - ColorStop, - ColorHint + auto is_length_percentage = [](Dimension& dimension) { + return dimension.is_length_percentage(); }; - - auto parse_color_stop_list_element = [&](ColorStopListElement& element) -> ElementType { - tokens.skip_whitespace(); - if (!tokens.has_next_token()) - return ElementType::Garbage; - auto const& token = tokens.next_token(); - - Gfx::Color color; - Optional position; - Optional second_position; - auto dimension = parse_dimension(token); - if (dimension.has_value() && dimension->is_length_percentage()) { - // [ ] or [] - position = dimension->length_percentage(); - tokens.skip_whitespace(); - // - if (!tokens.has_next_token() || tokens.peek_token().is(Token::Type::Comma)) { - element.transition_hint = GradientColorHint { *position }; - return ElementType::ColorHint; - } - // - auto maybe_color = parse_color(tokens.next_token()); - if (!maybe_color.has_value()) - return ElementType::Garbage; - color = *maybe_color; - } else { - // [ ?] - auto maybe_color = parse_color(token); - if (!maybe_color.has_value()) - return ElementType::Garbage; - color = *maybe_color; - tokens.skip_whitespace(); - // Allow up to [ ] (double-position color stops) - // Note: Double-position color stops only appear to be valid in this order. - for (auto stop_position : Array { &position, &second_position }) { - if (tokens.has_next_token() && !tokens.peek_token().is(Token::Type::Comma)) { - auto token = tokens.next_token(); - auto dimension = parse_dimension(token); - if (!dimension.has_value() || !dimension->is_length_percentage()) - return ElementType::Garbage; - *stop_position = dimension->length_percentage(); - tokens.skip_whitespace(); - } - } - } - - element.color_stop = GradientColorStop { color, position, second_position }; - return ElementType::ColorStop; + auto get_length_percentage = [](Dimension& dimension) { + return dimension.length_percentage(); }; + auto color_stops = parse_color_stop_list( + tokens, is_length_percentage, get_length_percentage, + [&](auto& token) { return parse_color(token); }, + [&](auto& token) { return parse_dimension(token); }); - ColorStopListElement first_element {}; - if (parse_color_stop_list_element(first_element) != ElementType::ColorStop) + if (!color_stops.has_value()) return {}; + return LinearGradientStyleValue::create(gradient_direction, move(*color_stops), gradient_type, repeating_gradient); +} + +RefPtr Parser::parse_conic_gradient_function(ComponentValue const& component_value) +{ + if (!component_value.is_function()) + return {}; + auto function_name = component_value.function().name(); + + if (!function_name.equals_ignoring_case("conic-gradient"sv)) + return {}; + + TokenStream tokens { component_value.function().values() }; + tokens.skip_whitespace(); + if (!tokens.has_next_token()) return {}; - Vector color_stops { first_element }; - while (tokens.has_next_token()) { - ColorStopListElement list_element {}; - tokens.skip_whitespace(); - if (!tokens.next_token().is(Token::Type::Comma)) - return {}; - auto element_type = parse_color_stop_list_element(list_element); - if (element_type == ElementType::ColorHint) { - // , - tokens.skip_whitespace(); - if (!tokens.next_token().is(Token::Type::Comma)) + Angle from_angle(0, Angle::Type::Deg); + PositionValue at_position = PositionValue::center(); + + // conic-gradient( [ [ from ]? [ at ]? ] || + // , ) + auto token = tokens.peek_token(); + bool got_from_angle = false; + bool got_color_interpolation_method = false; + bool got_at_position = false; + while (token.is(Token::Type::Ident)) { + auto token_string = token.token().ident(); + auto consume_identifier = [&](auto identifier) { + if (token_string.equals_ignoring_case(identifier)) { + (void)tokens.next_token(); + tokens.skip_whitespace(); + return true; + } + return false; + }; + + if (consume_identifier("from"sv)) { + // from + if (got_from_angle || got_at_position) return {}; - // Note: This fills in the color stop on the same list_element as the color hint (it does not overwrite it). - if (parse_color_stop_list_element(list_element) != ElementType::ColorStop) + if (!tokens.has_next_token()) return {}; - } else if (element_type == ElementType::ColorStop) { - // + + auto angle_token = tokens.next_token(); + if (!angle_token.is(Token::Type::Dimension)) + return {}; + float angle = angle_token.token().dimension_value(); + auto angle_unit = angle_token.token().dimension_unit(); + auto angle_type = Angle::unit_from_name(angle_unit); + if (!angle_type.has_value()) + return {}; + + from_angle = Angle(angle, *angle_type); + got_from_angle = true; + } else if (consume_identifier("at"sv)) { + // at + if (got_at_position) + return {}; + auto position = parse_position(tokens); + if (!position.has_value()) + return {}; + at_position = *position; + got_at_position = true; + } else if (consume_identifier("in"sv)) { + // + if (got_color_interpolation_method) + return {}; + dbgln("FIXME: Parse color interpolation method for conic-gradient()"); + got_color_interpolation_method = true; } else { - return {}; + break; } - color_stops.append(list_element); + tokens.skip_whitespace(); + if (!tokens.has_next_token()) + return {}; + token = tokens.peek_token(); } - return LinearGradientStyleValue::create(gradient_direction, move(color_stops), gradient_type, repeating_gradient); + tokens.skip_whitespace(); + if (!tokens.has_next_token()) + return {}; + if ((got_from_angle || got_color_interpolation_method) && !tokens.next_token().is(Token::Type::Comma)) + return {}; + + // = + // , [ ? , ]# + auto is_angle_percentage = [](Dimension& dimension) { + return dimension.is_angle_percentage(); + }; + auto get_angle_percentage = [](Dimension& dimension) { + return dimension.angle_percentage(); + }; + auto color_stops = parse_color_stop_list( + tokens, is_angle_percentage, get_angle_percentage, + [&](auto& token) { return parse_color(token); }, + [&](auto& token) { return parse_dimension(token); }); + + if (!color_stops.has_value()) + return {}; + + return ConicGradientStyleValue::create(from_angle, at_position, move(*color_stops)); } Optional Parser::parse_position(TokenStream& tokens) @@ -3692,7 +3813,10 @@ RefPtr Parser::parse_image_value(ComponentValue const& component_val if (url.has_value()) return ImageStyleValue::create(url.value()); // FIXME: Implement other kinds of gradient - return parse_linear_gradient_function(component_value); + auto linear_gradient = parse_linear_gradient_function(component_value); + if (linear_gradient) + return linear_gradient; + return parse_conic_gradient_function(component_value); } template diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index 7ba16298f0..28f0e6e2ea 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -264,6 +264,7 @@ private: Optional parse_url_function(ComponentValue const&, AllowedDataUrlType = AllowedDataUrlType::None); RefPtr parse_linear_gradient_function(ComponentValue const&); + RefPtr parse_conic_gradient_function(ComponentValue const&); ParseErrorOr> parse_css_value(PropertyID, TokenStream&); RefPtr parse_css_value(ComponentValue const&);