1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 10:28:10 +00:00

LibWeb: Implement and use "parse a CSS stylesheet" algorithm

`parse_a_stylesheet()` should not do any conversion on its rules. This
change corrects that. There are other places where we get this wrong,
but one thing at a time. :^)
This commit is contained in:
Sam Atkins 2022-03-29 14:13:39 +01:00 committed by Andreas Kling
parent fc3d51c59e
commit 85d8c652e9
2 changed files with 27 additions and 15 deletions

View file

@ -160,15 +160,10 @@ Parser::Parser(ParsingContext const& context, StringView input, String const& en
{
}
NonnullRefPtr<CSSStyleSheet> Parser::parse_as_stylesheet(Optional<AK::URL> location)
{
return parse_a_stylesheet(m_token_stream, location);
}
// 5.3.3. Parse a stylesheet
// https://www.w3.org/TR/css-syntax-3/#parse-stylesheet
template<typename T>
NonnullRefPtr<CSSStyleSheet> Parser::parse_a_stylesheet(TokenStream<T>& tokens, Optional<AK::URL> location)
Parser::ParsedStyleSheet Parser::parse_a_stylesheet(TokenStream<T>& tokens, Optional<AK::URL> location)
{
// To parse a stylesheet from an input given an optional url location:
@ -177,19 +172,31 @@ NonnullRefPtr<CSSStyleSheet> Parser::parse_a_stylesheet(TokenStream<T>& tokens,
// NOTE: These are done automatically when creating the Parser.
// 3. Create a new stylesheet, with its location set to location (or null, if location was not passed).
// NOTE: We create the stylesheet at the end.
ParsedStyleSheet style_sheet;
style_sheet.location = location;
// 4. Consume a list of rules from input, with the top-level flag set, and set the stylesheets value to the result.
auto parser_rules = consume_a_list_of_rules(tokens, TopLevel::Yes);
NonnullRefPtrVector<CSSRule> rules;
style_sheet.rules = consume_a_list_of_rules(tokens, TopLevel::Yes);
for (auto& raw_rule : parser_rules) {
// 5. Return the stylesheet.
return style_sheet;
}
// https://www.w3.org/TR/css-syntax-3/#parse-a-css-stylesheet
NonnullRefPtr<CSSStyleSheet> Parser::parse_as_css_stylesheet(Optional<AK::URL> location)
{
// To parse a CSS stylesheet, first parse a stylesheet.
auto style_sheet = parse_a_stylesheet(m_token_stream, {});
// Interpret all of the resulting top-level qualified rules as style rules, defined below.
NonnullRefPtrVector<CSSRule> rules;
for (auto& raw_rule : style_sheet.rules) {
auto rule = convert_to_rule(raw_rule);
// If any style rule is invalid, or any at-rule is not recognized or is invalid according to its grammar or context, its a parse error. Discard that rule.
if (rule)
rules.append(*rule);
}
// 5. Return the stylesheet.
return CSSStyleSheet::create(move(rules), move(location));
}
@ -5242,7 +5249,7 @@ RefPtr<CSS::CSSStyleSheet> parse_css_stylesheet(CSS::ParsingContext const& conte
if (css.is_empty())
return CSS::CSSStyleSheet::create({}, location);
CSS::Parser parser(context, css);
return parser.parse_as_stylesheet(location);
return parser.parse_as_css_stylesheet(location);
}
RefPtr<CSS::ElementInlineCSSStyleDeclaration> parse_css_style_attribute(CSS::ParsingContext const& context, StringView css, DOM::Element& element)