LibWeb/CSS: Use CSSRule's context to parse new child rules

Keep track of which CSSRule owns a CSSRuleList, and then use that to
produce a stack of RuleContexts for the CSS Parser to use.

There are certainly other places we should do this!
This commit is contained in:
Sam Atkins 2025-04-14 17:27:00 +01:00
commit 658569b533
Notes: github-actions[bot] 2025-04-23 10:39:55 +00:00
7 changed files with 32 additions and 15 deletions

View file

@ -46,6 +46,7 @@ void CSSRuleList::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_rules);
visitor.visit(m_owner_rule);
}
// https://drafts.csswg.org/cssom/#insert-a-css-rule
@ -62,11 +63,12 @@ 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(
Parser::ParsingParams { realm() },
rule.get<StringView>());
new_rule = parse_css_rule(parsing_params, rule.get<StringView>());
} else {
new_rule = rule.get<CSSRule*>();
}
@ -74,7 +76,7 @@ WebIDL::ExceptionOr<unsigned> CSSRuleList::insert_a_css_rule(Variant<StringView,
// 4. If new rule is a syntax error, and nested is set, perform the following substeps:
if (!new_rule && nested == Nested::Yes) {
// - Set declarations to the results of performing parse a CSS declaration block, on argument rule.
auto declarations = parse_css_property_declaration_block(Parser::ParsingParams { realm() }, rule.get<StringView>());
auto declarations = parse_css_property_declaration_block(parsing_params, rule.get<StringView>());
// - If declarations is empty, throw a SyntaxError exception.
if (declarations.custom_properties.is_empty() && declarations.properties.is_empty())
@ -225,4 +227,13 @@ Optional<JS::Value> CSSRuleList::item_value(size_t index) const
return {};
}
Vector<Parser::RuleContext> CSSRuleList::rule_context() const
{
Vector<Parser::RuleContext> context;
for (auto* rule = m_owner_rule.ptr(); rule; rule = rule->parent_rule())
context.append(Parser::rule_context_type_for_rule(rule->type()));
context.reverse();
return context;
}
}