Pārlūkot izejas kodu

LibWeb: Move color stop list parsing to standalone functions

This makes these slightly less clunky to use for other gradient types.
MacDue 2 gadi atpakaļ
vecāks
revīzija
f1f1977e2d

+ 26 - 26
Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp

@@ -2386,6 +2386,30 @@ static Optional<Vector<TElement>> parse_color_stop_list(auto& tokens, auto is_po
     return color_stops;
     return color_stops;
 }
 }
 
 
+Optional<Vector<LinearColorStopListElement>> Parser::parse_linear_color_stop_list(TokenStream<ComponentValue>& tokens)
+{
+    // <color-stop-list> =
+    //   <linear-color-stop> , [ <linear-color-hint>? , <linear-color-stop> ]#
+    return parse_color_stop_list<LinearColorStopListElement>(
+        tokens,
+        [](Dimension& dimension) { return dimension.is_length_percentage(); },
+        [](Dimension& dimension) { return dimension.length_percentage(); },
+        [&](auto& token) { return parse_color(token); },
+        [&](auto& token) { return parse_dimension(token); });
+}
+
+Optional<Vector<AngularColorStopListElement>> Parser::parse_angular_color_stop_list(TokenStream<ComponentValue>& tokens)
+{
+    // <angular-color-stop-list> =
+    //   <angular-color-stop> , [ <angular-color-hint>? , <angular-color-stop> ]#
+    return parse_color_stop_list<AngularColorStopListElement>(
+        tokens,
+        [](Dimension& dimension) { return dimension.is_angle_percentage(); },
+        [](Dimension& dimension) { return dimension.angle_percentage(); },
+        [&](auto& token) { return parse_color(token); },
+        [&](auto& token) { return parse_dimension(token); });
+}
+
 static StringView consume_if_starts_with(StringView str, StringView start, auto found_callback)
 static StringView consume_if_starts_with(StringView str, StringView start, auto found_callback)
 {
 {
     if (str.starts_with(start, CaseSensitivity::CaseInsensitive)) {
     if (str.starts_with(start, CaseSensitivity::CaseInsensitive)) {
@@ -2516,19 +2540,7 @@ RefPtr<StyleValue> Parser::parse_linear_gradient_function(ComponentValue const&
     if (has_direction_param && !tokens.next_token().is(Token::Type::Comma))
     if (has_direction_param && !tokens.next_token().is(Token::Type::Comma))
         return {};
         return {};
 
 
-    // <color-stop-list> =
-    //      <linear-color-stop> , [ <linear-color-hint>? , <linear-color-stop> ]#
-    auto is_length_percentage = [](Dimension& dimension) {
-        return dimension.is_length_percentage();
-    };
-    auto get_length_percentage = [](Dimension& dimension) {
-        return dimension.length_percentage();
-    };
-    auto color_stops = parse_color_stop_list<LinearColorStopListElement>(
-        tokens, is_length_percentage, get_length_percentage,
-        [&](auto& token) { return parse_color(token); },
-        [&](auto& token) { return parse_dimension(token); });
-
+    auto color_stops = parse_linear_color_stop_list(tokens);
     if (!color_stops.has_value())
     if (!color_stops.has_value())
         return {};
         return {};
 
 
@@ -2625,19 +2637,7 @@ RefPtr<StyleValue> Parser::parse_conic_gradient_function(ComponentValue const& c
     if ((got_from_angle || got_at_position || got_color_interpolation_method) && !tokens.next_token().is(Token::Type::Comma))
     if ((got_from_angle || got_at_position || got_color_interpolation_method) && !tokens.next_token().is(Token::Type::Comma))
         return {};
         return {};
 
 
-    // <angular-color-stop-list> =
-    //   <angular-color-stop> , [ <angular-color-hint>? , <angular-color-stop> ]#
-    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<AngularColorStopListElement>(
-        tokens, is_angle_percentage, get_angle_percentage,
-        [&](auto& token) { return parse_color(token); },
-        [&](auto& token) { return parse_dimension(token); });
-
+    auto color_stops = parse_angular_color_stop_list(tokens);
     if (!color_stops.has_value())
     if (!color_stops.has_value())
         return {};
         return {};
 
 

+ 3 - 0
Userland/Libraries/LibWeb/CSS/Parser/Parser.h

@@ -264,6 +264,9 @@ private:
     };
     };
     Optional<AK::URL> parse_url_function(ComponentValue const&, AllowedDataUrlType = AllowedDataUrlType::None);
     Optional<AK::URL> parse_url_function(ComponentValue const&, AllowedDataUrlType = AllowedDataUrlType::None);
 
 
+    Optional<Vector<LinearColorStopListElement>> parse_linear_color_stop_list(TokenStream<ComponentValue>&);
+    Optional<Vector<AngularColorStopListElement>> parse_angular_color_stop_list(TokenStream<ComponentValue>&);
+
     RefPtr<StyleValue> parse_linear_gradient_function(ComponentValue const&);
     RefPtr<StyleValue> parse_linear_gradient_function(ComponentValue const&);
     RefPtr<StyleValue> parse_conic_gradient_function(ComponentValue const&);
     RefPtr<StyleValue> parse_conic_gradient_function(ComponentValue const&);