Просмотр исходного кода

LibWeb: Parse aspect-ratio property using TokenStream

Sam Atkins 1 год назад
Родитель
Сommit
2efaadd63c

+ 10 - 5
Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp

@@ -2682,13 +2682,13 @@ static void remove_property(Vector<PropertyID>& properties, PropertyID property_
 }
 }
 
 
 // https://www.w3.org/TR/css-sizing-4/#aspect-ratio
 // https://www.w3.org/TR/css-sizing-4/#aspect-ratio
-RefPtr<StyleValue> Parser::parse_aspect_ratio_value(Vector<ComponentValue> const& component_values)
+RefPtr<StyleValue> Parser::parse_aspect_ratio_value(TokenStream<ComponentValue>& tokens)
 {
 {
     // `auto || <ratio>`
     // `auto || <ratio>`
     RefPtr<StyleValue> auto_value;
     RefPtr<StyleValue> auto_value;
     RefPtr<StyleValue> ratio_value;
     RefPtr<StyleValue> ratio_value;
 
 
-    auto tokens = TokenStream { component_values };
+    auto transaction = tokens.begin_transaction();
     while (tokens.has_next_token()) {
     while (tokens.has_next_token()) {
         auto maybe_value = parse_css_value_for_property(PropertyID::AspectRatio, tokens);
         auto maybe_value = parse_css_value_for_property(PropertyID::AspectRatio, tokens);
         if (!maybe_value)
         if (!maybe_value)
@@ -2712,16 +2712,21 @@ RefPtr<StyleValue> Parser::parse_aspect_ratio_value(Vector<ComponentValue> const
     }
     }
 
 
     if (auto_value && ratio_value) {
     if (auto_value && ratio_value) {
+        transaction.commit();
         return StyleValueList::create(
         return StyleValueList::create(
             StyleValueVector { auto_value.release_nonnull(), ratio_value.release_nonnull() },
             StyleValueVector { auto_value.release_nonnull(), ratio_value.release_nonnull() },
             StyleValueList::Separator::Space);
             StyleValueList::Separator::Space);
     }
     }
 
 
-    if (ratio_value)
+    if (ratio_value) {
+        transaction.commit();
         return ratio_value.release_nonnull();
         return ratio_value.release_nonnull();
+    }
 
 
-    if (auto_value)
+    if (auto_value) {
+        transaction.commit();
         return auto_value.release_nonnull();
         return auto_value.release_nonnull();
+    }
 
 
     return nullptr;
     return nullptr;
 }
 }
@@ -5728,7 +5733,7 @@ Parser::ParseErrorOr<NonnullRefPtr<StyleValue>> Parser::parse_css_value(Property
     auto tokens = TokenStream { component_values };
     auto tokens = TokenStream { component_values };
     switch (property_id) {
     switch (property_id) {
     case PropertyID::AspectRatio:
     case PropertyID::AspectRatio:
-        if (auto parsed_value = parse_aspect_ratio_value(component_values))
+        if (auto parsed_value = parse_aspect_ratio_value(tokens); parsed_value && !tokens.has_next_token())
             return parsed_value.release_nonnull();
             return parsed_value.release_nonnull();
         return ParseError::SyntaxError;
         return ParseError::SyntaxError;
     case PropertyID::BackdropFilter:
     case PropertyID::BackdropFilter:

+ 1 - 1
Userland/Libraries/LibWeb/CSS/Parser/Parser.h

@@ -225,7 +225,7 @@ private:
     RefPtr<StyleValue> parse_simple_comma_separated_value_list(PropertyID, TokenStream<ComponentValue>&);
     RefPtr<StyleValue> parse_simple_comma_separated_value_list(PropertyID, TokenStream<ComponentValue>&);
 
 
     RefPtr<StyleValue> parse_filter_value_list_value(Vector<ComponentValue> const&);
     RefPtr<StyleValue> parse_filter_value_list_value(Vector<ComponentValue> const&);
-    RefPtr<StyleValue> parse_aspect_ratio_value(Vector<ComponentValue> const&);
+    RefPtr<StyleValue> parse_aspect_ratio_value(TokenStream<ComponentValue>&);
     RefPtr<StyleValue> parse_background_value(Vector<ComponentValue> const&);
     RefPtr<StyleValue> parse_background_value(Vector<ComponentValue> const&);
     RefPtr<StyleValue> parse_single_background_position_x_or_y_value(TokenStream<ComponentValue>&, PropertyID);
     RefPtr<StyleValue> parse_single_background_position_x_or_y_value(TokenStream<ComponentValue>&, PropertyID);
     RefPtr<StyleValue> parse_single_background_repeat_value(TokenStream<ComponentValue>&);
     RefPtr<StyleValue> parse_single_background_repeat_value(TokenStream<ComponentValue>&);