diff --git a/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Libraries/LibWeb/CSS/Parser/Parser.cpp index ce5938d69d4..c8d3a91f0a3 100644 --- a/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -103,34 +103,14 @@ Vector Parser::parse_a_stylesheets_contents(TokenStream& input) return consume_a_stylesheets_contents(input); } -GC::RootVector> Parser::parse_as_stylesheet_contents() +GC::RootVector> Parser::convert_rules(Vector const& raw_rules) { - auto raw_rules = parse_a_stylesheets_contents(m_token_stream); - GC::RootVector> rules(realm().heap()); - for (auto const& raw_rule : raw_rules) { - auto rule = convert_to_rule(raw_rule, Nested::No); - if (!rule) { - log_parse_error(); - continue; - } - rules.append(*rule); - } - - return rules; -} - -// https://drafts.csswg.org/css-syntax/#parse-a-css-stylesheet -GC::Ref Parser::parse_as_css_stylesheet(Optional<::URL::URL> location, Vector> media_query_list) -{ - // To parse a CSS stylesheet, first parse a stylesheet. - auto const& style_sheet = parse_a_stylesheet(m_token_stream, location); - bool import_rules_valid = true; bool namespace_rules_valid = true; // Interpret all of the resulting top-level qualified rules as style rules, defined below. GC::RootVector> rules(realm().heap()); - for (auto const& raw_rule : style_sheet.rules) { + for (auto const& raw_rule : raw_rules) { auto rule = convert_to_rule(raw_rule, Nested::No); // If any style rule is invalid, or any at-rule is not recognized or is invalid according to its grammar or context, it’s a parse error. // Discard that rule. @@ -171,7 +151,21 @@ GC::Ref Parser::parse_as_css_stylesheet(Optional<::URL::URL> rules.append(*rule); } - auto rule_list = CSSRuleList::create(realm(), rules); + return rules; +} + +GC::RootVector> Parser::parse_as_stylesheet_contents() +{ + return convert_rules(parse_a_stylesheets_contents(m_token_stream)); +} + +// https://drafts.csswg.org/css-syntax/#parse-a-css-stylesheet +GC::Ref Parser::parse_as_css_stylesheet(Optional<::URL::URL> location, Vector> media_query_list) +{ + // To parse a CSS stylesheet, first parse a stylesheet. + auto const& style_sheet = parse_a_stylesheet(m_token_stream, location); + + auto rule_list = CSSRuleList::create(realm(), convert_rules(style_sheet.rules)); auto media_list = MediaList::create(realm(), move(media_query_list)); return CSSStyleSheet::create(realm(), rule_list, media_list, move(location)); } diff --git a/Libraries/LibWeb/CSS/Parser/Parser.h b/Libraries/LibWeb/CSS/Parser/Parser.h index 3e5ebaa6fa5..47ec72752d9 100644 --- a/Libraries/LibWeb/CSS/Parser/Parser.h +++ b/Libraries/LibWeb/CSS/Parser/Parser.h @@ -93,6 +93,7 @@ class Parser { public: static Parser create(ParsingParams const&, StringView input, StringView encoding = "utf-8"sv); + GC::RootVector> convert_rules(Vector const& raw_rules); GC::Ref parse_as_css_stylesheet(Optional<::URL::URL> location, Vector> media_query_list = {}); struct PropertiesAndCustomProperties {