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.
This commit is contained in:
Callum Law 2025-06-21 21:57:57 +12:00 committed by Sam Atkins
commit 80ea865b19
Notes: github-actions[bot] 2025-06-23 11:53:52 +00:00
4 changed files with 139 additions and 9 deletions

View file

@ -63,18 +63,17 @@ WebIDL::ExceptionOr<unsigned> CSSRuleList::insert_a_css_rule(Variant<StringView,
// NOTE: The insert-a-css-rule spec expects `rule` to be a string, but the CSSStyleSheet.insertRule()
// spec calls this algorithm with an already-parsed CSSRule. So, we use a Variant and skip step 3
// if that variant holds a CSSRule already.
Parser::ParsingParams parsing_params { realm() };
parsing_params.rule_context = rule_context();
CSSRule* new_rule = nullptr;
if (rule.has<StringView>()) {
new_rule = parse_css_rule(parsing_params, rule.get<StringView>());
new_rule = parse_css_rule(Parser::ParsingParams { realm() }, rule.get<StringView>());
} else {
new_rule = rule.get<CSSRule*>();
}
// 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<StringView>());
@ -130,7 +129,8 @@ WebIDL::ExceptionOr<unsigned> CSSRuleList::insert_a_css_rule(Variant<StringView,
break;
}
if (rule_is_disallowed)
// FIXME: There are more constraints that we should check here - Parser::is_valid_in_the_current_context is probably a good reference for that.
if (rule_is_disallowed || (nested == Nested::Yes && first_is_one_of(new_rule->type(), 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.