From 80ea865b1977e137e4c5c80b56afacef7bc25a72 Mon Sep 17 00:00:00 2001 From: Callum Law Date: Sat, 21 Jun 2025 21:57:57 +1200 Subject: [PATCH] LibWeb: Use correct error for invalid rule in nested insertRule When we try to insert a disallowed (non-nested) statement into a CSSGroupingRule we should throw a `HierarchyRequestError` as it being disallowed is a "constraint specified by CSS". Previously we would rely on `Parser::is_valid_in_the_current_context` and throw a Syntax error. There are more constraints to be implemented. --- Libraries/LibWeb/CSS/CSSRuleList.cpp | 10 +- .../js/conditional-CSSGroupingRule.txt | 8 +- .../css/cssom/CSSGroupingRule-insertRule.txt | 12 ++ .../css/cssom/CSSGroupingRule-insertRule.html | 118 ++++++++++++++++++ 4 files changed, 139 insertions(+), 9 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/wpt-import/css/cssom/CSSGroupingRule-insertRule.txt create mode 100644 Tests/LibWeb/Text/input/wpt-import/css/cssom/CSSGroupingRule-insertRule.html diff --git a/Libraries/LibWeb/CSS/CSSRuleList.cpp b/Libraries/LibWeb/CSS/CSSRuleList.cpp index 76b9971f45e..025032f7aa3 100644 --- a/Libraries/LibWeb/CSS/CSSRuleList.cpp +++ b/Libraries/LibWeb/CSS/CSSRuleList.cpp @@ -63,18 +63,17 @@ WebIDL::ExceptionOr CSSRuleList::insert_a_css_rule(Variant()) { - new_rule = parse_css_rule(parsing_params, rule.get()); + new_rule = parse_css_rule(Parser::ParsingParams { realm() }, rule.get()); } else { new_rule = rule.get(); } // 4. If new rule is a syntax error, and nested is set, perform the following substeps: if (!new_rule && nested == Nested::Yes) { + Parser::ParsingParams parsing_params { realm() }; + parsing_params.rule_context = rule_context(); // - Set declarations to the results of performing parse a CSS declaration block, on argument rule. auto declarations = parse_css_property_declaration_block(parsing_params, rule.get()); @@ -130,7 +129,8 @@ WebIDL::ExceptionOr CSSRuleList::insert_a_css_rule(Varianttype(), CSSRule::Type::Import, CSSRule::Type::Namespace))) return WebIDL::HierarchyRequestError::create(realm(), "Cannot insert rule at specified index."_string); // 7. If new rule is an @namespace at-rule, and list contains anything other than @import at-rules, and @namespace at-rules, throw an InvalidStateError exception. diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/css-conditional/js/conditional-CSSGroupingRule.txt b/Tests/LibWeb/Text/expected/wpt-import/css/css-conditional/js/conditional-CSSGroupingRule.txt index e45e3813268..482273bec5c 100644 --- a/Tests/LibWeb/Text/expected/wpt-import/css/css-conditional/js/conditional-CSSGroupingRule.txt +++ b/Tests/LibWeb/Text/expected/wpt-import/css/css-conditional/js/conditional-CSSGroupingRule.txt @@ -2,12 +2,12 @@ Harness status: OK Found 34 tests -30 Pass -4 Fail +32 Pass +2 Fail Pass @media is CSSGroupingRule Pass @media rule type Pass Empty @media rule length -Fail insertRule of @import into @media +Pass insertRule of @import into @media Pass insertRule into empty @media at bad index Pass insertRule into @media updates length Pass insertRule of valid @media into @media @@ -24,7 +24,7 @@ Pass Return value of insertRule into @media Pass @supports is CSSGroupingRule Pass @supports rule type Pass Empty @supports rule length -Fail insertRule of @import into @supports +Pass insertRule of @import into @supports Pass insertRule into empty @supports at bad index Pass insertRule into @supports updates length Pass insertRule of valid @media into @supports diff --git a/Tests/LibWeb/Text/expected/wpt-import/css/cssom/CSSGroupingRule-insertRule.txt b/Tests/LibWeb/Text/expected/wpt-import/css/cssom/CSSGroupingRule-insertRule.txt new file mode 100644 index 00000000000..22851e7102b --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/css/cssom/CSSGroupingRule-insertRule.txt @@ -0,0 +1,12 @@ +Harness status: OK + +Found 7 tests + +7 Pass +Pass index before first +Pass index after final +Pass index not specified +Pass index exceeds length +Pass CSS parsing error +Pass constraint violation +Pass disallowed namespace rule \ No newline at end of file diff --git a/Tests/LibWeb/Text/input/wpt-import/css/cssom/CSSGroupingRule-insertRule.html b/Tests/LibWeb/Text/input/wpt-import/css/cssom/CSSGroupingRule-insertRule.html new file mode 100644 index 00000000000..f592e14e978 --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/css/cssom/CSSGroupingRule-insertRule.html @@ -0,0 +1,118 @@ + + + + + CSSOM - CSSGroupingRule - insertRule + + + + + +