From 1dcde5792265e6417bb2700a781dfc181bc7da0d Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Mon, 28 Mar 2022 16:37:54 +0100 Subject: [PATCH] LibWeb: Bring "parse a list of declarations" closer to spec The work to create a PropertyOwningCSSStyleDeclaration is now moved to convert_to_style_declaration() instead. --- .../Libraries/LibWeb/CSS/Parser/Parser.cpp | 80 +++++++++++-------- Userland/Libraries/LibWeb/CSS/Parser/Parser.h | 8 +- 2 files changed, 51 insertions(+), 37 deletions(-) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp index c45ab5b609d..4756203878c 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -1902,39 +1902,20 @@ Optional Parser::parse_a_declaration(TokenStream& tokens) return {}; } -RefPtr Parser::parse_as_list_of_declarations() +Vector Parser::parse_as_list_of_declarations() { return parse_a_list_of_declarations(m_token_stream); } +// 5.3.8. Parse a list of declarations +// https://www.w3.org/TR/css-syntax-3/#parse-list-of-declarations template -RefPtr Parser::parse_a_list_of_declarations(TokenStream& tokens) +Vector Parser::parse_a_list_of_declarations(TokenStream& tokens) { - auto declarations_and_at_rules = consume_a_list_of_declarations(tokens); - - Vector properties; - HashMap custom_properties; - - for (auto& declaration_or_at_rule : declarations_and_at_rules) { - if (declaration_or_at_rule.is_at_rule()) { - dbgln_if(CSS_PARSER_DEBUG, "!!! CSS at-rule is not allowed here!"); - continue; - } - - auto& declaration = declaration_or_at_rule.m_declaration; - - auto maybe_property = convert_to_style_property(declaration); - if (maybe_property.has_value()) { - auto property = maybe_property.value(); - if (property.property_id == PropertyID::Custom) { - custom_properties.set(property.custom_name, property); - } else { - properties.append(property); - } - } - } - - return PropertyOwningCSSStyleDeclaration::create(move(properties), move(custom_properties)); + // To parse a list of declarations from input: + // 1. Normalize input, and set input to the result. + // 2. Consume a list of declarations from input, and return the result. + return consume_a_list_of_declarations(tokens); } Optional Parser::parse_as_component_value() @@ -2015,6 +1996,12 @@ Vector> Parser::parse_a_comma_separated_list_of_ return lists; } +RefPtr Parser::parse_as_style_attribute() +{ + auto declarations_and_at_rules = parse_a_list_of_declarations(m_token_stream); + return convert_to_style_declaration(declarations_and_at_rules); +} + Optional Parser::parse_url_function(StyleComponentValueRule const& component_value, AllowedDataUrlType allowed_data_url_type) { // FIXME: Handle list of media queries. https://www.w3.org/TR/css-cascade-3/#conditional-import @@ -2156,7 +2143,13 @@ RefPtr Parser::convert_to_rule(NonnullRefPtr rule) return {}; } - auto declaration = convert_to_declaration(*rule->m_block); + if (!rule->block()->is_curly()) + return {}; + + auto stream = TokenStream(rule->block()->values()); + auto declarations_and_at_rules = parse_a_list_of_declarations(stream); + + auto declaration = convert_to_style_declaration(declarations_and_at_rules); if (!declaration) { dbgln_if(CSS_PARSER_DEBUG, "CSSParser: style rule declaration invalid; discarding."); return {}; @@ -2168,13 +2161,31 @@ RefPtr Parser::convert_to_rule(NonnullRefPtr rule) return {}; } -RefPtr Parser::convert_to_declaration(NonnullRefPtr block) +RefPtr Parser::convert_to_style_declaration(Vector declarations_and_at_rules) { - if (!block->is_curly()) - return {}; + Vector properties; + HashMap custom_properties; - auto stream = TokenStream(block->m_values); - return parse_a_list_of_declarations(stream); + for (auto& declaration_or_at_rule : declarations_and_at_rules) { + if (declaration_or_at_rule.is_at_rule()) { + dbgln_if(CSS_PARSER_DEBUG, "!!! CSS at-rule is not allowed here!"); + continue; + } + + auto& declaration = declaration_or_at_rule.m_declaration; + + auto maybe_property = convert_to_style_property(declaration); + if (maybe_property.has_value()) { + auto property = maybe_property.value(); + if (property.property_id == PropertyID::Custom) { + custom_properties.set(property.custom_name, property); + } else { + properties.append(property); + } + } + } + + return PropertyOwningCSSStyleDeclaration::create(move(properties), move(custom_properties)); } Optional Parser::convert_to_style_property(StyleDeclarationRule const& declaration) @@ -5086,10 +5097,11 @@ RefPtr parse_css(CSS::ParsingContext const& context, StringV RefPtr parse_css_style_attribute(CSS::ParsingContext const& context, StringView css) { + // FIXME: We could save some effort by creating an ElementInlineCSSStyleDeclaration right here. if (css.is_empty()) return CSS::PropertyOwningCSSStyleDeclaration::create({}, {}); CSS::Parser parser(context, css); - return parser.parse_as_list_of_declarations(); + return parser.parse_as_style_attribute(); } RefPtr parse_css_value(CSS::ParsingContext const& context, StringView string, CSS::PropertyID property_id) diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h index 0f2533117e6..c417bb6778d 100644 --- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h @@ -98,13 +98,15 @@ public: // Used in @supports conditions. [CSS3-CONDITIONAL] Optional parse_as_declaration(); // For the contents of a style attribute, which parses text into the contents of a single style rule. - RefPtr parse_as_list_of_declarations(); + Vector parse_as_list_of_declarations(); // For things that need to consume a single value, like the parsing rules for attr(). Optional parse_as_component_value(); // For the contents of presentational attributes, which parse text into a single declaration’s value, or for parsing a stand-alone selector [SELECT] or list of Media Queries [MEDIAQ], as in Selectors API or the media HTML attribute. Vector parse_as_list_of_component_values(); Vector> parse_as_comma_separated_list_of_component_values(); + RefPtr parse_as_style_attribute(); + enum class SelectorParsingMode { Standard, // `` and `` @@ -141,7 +143,7 @@ private: template Optional parse_a_declaration(TokenStream&); template - RefPtr parse_a_list_of_declarations(TokenStream&); + Vector parse_a_list_of_declarations(TokenStream&); template Optional parse_a_component_value(TokenStream&); template @@ -187,7 +189,7 @@ private: [[nodiscard]] Optional parse_general_enclosed(TokenStream&); [[nodiscard]] RefPtr convert_to_rule(NonnullRefPtr); - [[nodiscard]] RefPtr convert_to_declaration(NonnullRefPtr); + [[nodiscard]] RefPtr convert_to_style_declaration(Vector declarations); [[nodiscard]] Optional convert_to_style_property(StyleDeclarationRule const&); class Dimension {