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
parent 763b1b0ee2
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

@ -18,6 +18,7 @@ CSSGroupingRule::CSSGroupingRule(JS::Realm& realm, CSSRuleList& rules, Type type
: CSSRule(realm, type)
, m_rules(rules)
{
m_rules->set_owner_rule(*this);
for (auto& rule : *m_rules)
rule->set_parent_rule(this);
}

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;
}
}

View file

@ -11,6 +11,7 @@
#include <AK/Function.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/CSS/CSSRule.h>
#include <LibWeb/CSS/Parser/RuleContext.h>
#include <LibWeb/Forward.h>
#include <LibWeb/TraversalOrder.h>
#include <LibWeb/WebIDL/ExceptionOr.h>
@ -62,6 +63,7 @@ public:
// Returns whether the match state of any media queries changed after evaluation.
bool evaluate_media_queries(HTML::Window const&);
void set_owner_rule(GC::Ref<CSSRule> owner_rule) { m_owner_rule = owner_rule; }
void set_rules(Badge<CSSStyleSheet>, Vector<GC::Ref<CSSRule>> rules) { m_rules = move(rules); }
Function<void()> on_change;
@ -72,7 +74,10 @@ private:
virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
Vector<Parser::RuleContext> rule_context() const;
Vector<GC::Ref<CSSRule>> m_rules;
GC::Ptr<CSSRule> m_owner_rule;
};
}

View file

@ -1216,6 +1216,8 @@ void CSSStyleProperties::set_declarations_from_text(StringView css_text)
auto parsing_params = owner_node().has_value()
? Parser::ParsingParams(owner_node()->element().document())
: Parser::ParsingParams();
parsing_params.rule_context.append(Parser::RuleContext::Style);
auto style = parse_css_property_declaration_block(parsing_params, css_text);
set_the_declarations(style.properties, style.custom_properties);
}

View file

@ -1360,9 +1360,7 @@ Parser::PropertiesAndCustomProperties Parser::parse_as_property_declaration_bloc
};
// 1. Let declarations be the returned declarations from invoking parse a blocks contents with string.
m_rule_context.append(RuleContext::Style);
auto declarations_and_at_rules = parse_a_blocks_contents(m_token_stream);
m_rule_context.take_last();
// 2. Let parsed declarations be a new empty list.
PropertiesAndCustomProperties parsed_declarations;

View file

@ -2,14 +2,14 @@ Harness status: OK
Found 34 tests
26 Pass
8 Fail
30 Pass
4 Fail
Pass @media is CSSGroupingRule
Pass @media rule type
Pass Empty @media rule length
Fail insertRule of @import into @media
Fail insertRule into empty @media at bad index
Fail insertRule into @media updates length
Pass insertRule into empty @media at bad index
Pass insertRule into @media updates length
Pass insertRule of valid @media into @media
Pass insertRule of valid style rule into @media
Fail insertRule of invalid @media into @media
@ -25,8 +25,8 @@ Pass @supports is CSSGroupingRule
Pass @supports rule type
Pass Empty @supports rule length
Fail insertRule of @import into @supports
Fail insertRule into empty @supports at bad index
Fail insertRule into @supports updates length
Pass insertRule into empty @supports at bad index
Pass insertRule into @supports updates length
Pass insertRule of valid @media into @supports
Pass insertRule of valid style rule into @supports
Fail insertRule of invalid @media into @supports

View file

@ -2,8 +2,8 @@ Harness status: OK
Found 12 tests
7 Pass
5 Fail
8 Pass
4 Fail
Fail Trailing declarations
Fail Mixed declarations
Fail CSSNestedDeclarations.style
@ -12,7 +12,7 @@ Pass Nested @scope rule
Fail Inner rule starting with an ident
Pass Inserting a CSSNestedDeclaration rule into style rule
Pass Inserting a CSSNestedDeclaration rule into nested group rule
Fail Attempting to insert a CSSNestedDeclaration rule into top-level @media rule
Pass Attempting to insert a CSSNestedDeclaration rule into top-level @media rule
Pass Attempting to insert a CSSNestedDeclaration rule into a stylesheet
Pass Attempting to insert a CSSNestedDeclaration rule, empty block
Pass Attempting to insert a CSSNestedDeclaration rule, all invalid declarations