From a1516d0ca0e2fcb36d474b41ef26bd5e1737a1bd Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Thu, 16 Jan 2025 21:45:37 +0000 Subject: [PATCH] LibWeb: Treat import rules as invalid if preceded by non-import rules If an import statement is preceded by a valid rule that isn't an import rule or a @layer statement rule, it is now treated as invalid. --- Libraries/LibWeb/CSS/Parser/Parser.cpp | 15 ++++++++++ .../css/CSS2/css1/c11-import-000-ref.xht | 27 +++++++++++++++++ .../css/CSS2/css1/c11-import-000.xht | 29 +++++++++++++++++++ .../css/CSS2/css1/support/a-green.css | 1 + .../css/CSS2/css1/support/b-green.css | 1 + .../css/CSS2/css1/support/c-red.css | 1 + 6 files changed, 74 insertions(+) create mode 100644 Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/css1/c11-import-000-ref.xht create mode 100644 Tests/LibWeb/Ref/input/wpt-import/css/CSS2/css1/c11-import-000.xht create mode 100644 Tests/LibWeb/Ref/input/wpt-import/css/CSS2/css1/support/a-green.css create mode 100644 Tests/LibWeb/Ref/input/wpt-import/css/CSS2/css1/support/b-green.css create mode 100644 Tests/LibWeb/Ref/input/wpt-import/css/CSS2/css1/support/c-red.css diff --git a/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Libraries/LibWeb/CSS/Parser/Parser.cpp index 356a3e1445f10..e8aa386dee264 100644 --- a/Libraries/LibWeb/CSS/Parser/Parser.cpp +++ b/Libraries/LibWeb/CSS/Parser/Parser.cpp @@ -17,6 +17,8 @@ #include #include #include +#include +#include #include #include #include @@ -161,6 +163,7 @@ CSSStyleSheet* Parser::parse_as_css_stylesheet(Optional location) // Interpret all of the resulting top-level qualified rules as style rules, defined below. GC::RootVector rules(m_context.realm().heap()); + auto more_import_rules_allowed = true; for (auto const& raw_rule : style_sheet.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. @@ -169,6 +172,18 @@ CSSStyleSheet* Parser::parse_as_css_stylesheet(Optional location) log_parse_error(); continue; } + // https://drafts.csswg.org/css-cascade-5/#at-import + // Any @import rules must precede all other valid at-rules and style rules in a style sheet (ignoring @charset and @layer statement rules) + // and must not have any other valid at-rules or style rules between it and previous @import rules, or else the @import rule is invalid. + auto is_layer_statement_rule = is(*rule); + auto is_import_rule = is(*rule); + + // FIXME: Include @charset rules in this check when they are added. + if (!(is_layer_statement_rule || is_import_rule)) { + more_import_rules_allowed = false; + } else if (is_import_rule && !more_import_rules_allowed) { + continue; + } rules.append(rule); } diff --git a/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/css1/c11-import-000-ref.xht b/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/css1/c11-import-000-ref.xht new file mode 100644 index 0000000000000..1f2fd4bf3bcc2 --- /dev/null +++ b/Tests/LibWeb/Ref/expected/wpt-import/css/CSS2/css1/c11-import-000-ref.xht @@ -0,0 +1,27 @@ + + + + + + + CSS Reftest Reference + + + + + + + + +

This text should be green.

+ +

This text should be green.

+ +

This text should be green.

+ +

This text should be green.

+ + + diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/css1/c11-import-000.xht b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/css1/c11-import-000.xht new file mode 100644 index 0000000000000..bcad7beaa492b --- /dev/null +++ b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/css1/c11-import-000.xht @@ -0,0 +1,29 @@ + + + + CSS Test: Basic Containment + + + + + + + + + + + + + +

This text should be green.

+

This text should be green.

+

This text should be green.

+

This text should be green.

+ + diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/css1/support/a-green.css b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/css1/support/a-green.css new file mode 100644 index 0000000000000..b0dbb071d5b05 --- /dev/null +++ b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/css1/support/a-green.css @@ -0,0 +1 @@ +.a { color: green; } diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/css1/support/b-green.css b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/css1/support/b-green.css new file mode 100644 index 0000000000000..84ed4618b194e --- /dev/null +++ b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/css1/support/b-green.css @@ -0,0 +1 @@ +.b { color: green; } diff --git a/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/css1/support/c-red.css b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/css1/support/c-red.css new file mode 100644 index 0000000000000..e48671667a3bc --- /dev/null +++ b/Tests/LibWeb/Ref/input/wpt-import/css/CSS2/css1/support/c-red.css @@ -0,0 +1 @@ +.c { color: red; }