mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-16 07:20:46 +00:00
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:
parent
763b1b0ee2
commit
658569b533
Notes:
github-actions[bot]
2025-04-23 10:39:55 +00:00
Author: https://github.com/AtkinsSJ
Commit: 658569b533
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4354
7 changed files with 32 additions and 15 deletions
|
@ -18,6 +18,7 @@ CSSGroupingRule::CSSGroupingRule(JS::Realm& realm, CSSRuleList& rules, Type type
|
||||||
: CSSRule(realm, type)
|
: CSSRule(realm, type)
|
||||||
, m_rules(rules)
|
, m_rules(rules)
|
||||||
{
|
{
|
||||||
|
m_rules->set_owner_rule(*this);
|
||||||
for (auto& rule : *m_rules)
|
for (auto& rule : *m_rules)
|
||||||
rule->set_parent_rule(this);
|
rule->set_parent_rule(this);
|
||||||
}
|
}
|
||||||
|
|
|
@ -46,6 +46,7 @@ void CSSRuleList::visit_edges(Cell::Visitor& visitor)
|
||||||
{
|
{
|
||||||
Base::visit_edges(visitor);
|
Base::visit_edges(visitor);
|
||||||
visitor.visit(m_rules);
|
visitor.visit(m_rules);
|
||||||
|
visitor.visit(m_owner_rule);
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://drafts.csswg.org/cssom/#insert-a-css-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()
|
// 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
|
// 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.
|
// if that variant holds a CSSRule already.
|
||||||
|
Parser::ParsingParams parsing_params { realm() };
|
||||||
|
parsing_params.rule_context = rule_context();
|
||||||
|
|
||||||
CSSRule* new_rule = nullptr;
|
CSSRule* new_rule = nullptr;
|
||||||
if (rule.has<StringView>()) {
|
if (rule.has<StringView>()) {
|
||||||
new_rule = parse_css_rule(
|
new_rule = parse_css_rule(parsing_params, rule.get<StringView>());
|
||||||
Parser::ParsingParams { realm() },
|
|
||||||
rule.get<StringView>());
|
|
||||||
} else {
|
} else {
|
||||||
new_rule = rule.get<CSSRule*>();
|
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:
|
// 4. If new rule is a syntax error, and nested is set, perform the following substeps:
|
||||||
if (!new_rule && nested == Nested::Yes) {
|
if (!new_rule && nested == Nested::Yes) {
|
||||||
// - Set declarations to the results of performing parse a CSS declaration block, on argument rule.
|
// - 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 is empty, throw a SyntaxError exception.
|
||||||
if (declarations.custom_properties.is_empty() && declarations.properties.is_empty())
|
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 {};
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
#include <AK/Function.h>
|
#include <AK/Function.h>
|
||||||
#include <LibWeb/Bindings/PlatformObject.h>
|
#include <LibWeb/Bindings/PlatformObject.h>
|
||||||
#include <LibWeb/CSS/CSSRule.h>
|
#include <LibWeb/CSS/CSSRule.h>
|
||||||
|
#include <LibWeb/CSS/Parser/RuleContext.h>
|
||||||
#include <LibWeb/Forward.h>
|
#include <LibWeb/Forward.h>
|
||||||
#include <LibWeb/TraversalOrder.h>
|
#include <LibWeb/TraversalOrder.h>
|
||||||
#include <LibWeb/WebIDL/ExceptionOr.h>
|
#include <LibWeb/WebIDL/ExceptionOr.h>
|
||||||
|
@ -62,6 +63,7 @@ public:
|
||||||
// Returns whether the match state of any media queries changed after evaluation.
|
// Returns whether the match state of any media queries changed after evaluation.
|
||||||
bool evaluate_media_queries(HTML::Window const&);
|
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); }
|
void set_rules(Badge<CSSStyleSheet>, Vector<GC::Ref<CSSRule>> rules) { m_rules = move(rules); }
|
||||||
|
|
||||||
Function<void()> on_change;
|
Function<void()> on_change;
|
||||||
|
@ -72,7 +74,10 @@ private:
|
||||||
virtual void initialize(JS::Realm&) override;
|
virtual void initialize(JS::Realm&) override;
|
||||||
virtual void visit_edges(Cell::Visitor&) override;
|
virtual void visit_edges(Cell::Visitor&) override;
|
||||||
|
|
||||||
|
Vector<Parser::RuleContext> rule_context() const;
|
||||||
|
|
||||||
Vector<GC::Ref<CSSRule>> m_rules;
|
Vector<GC::Ref<CSSRule>> m_rules;
|
||||||
|
GC::Ptr<CSSRule> m_owner_rule;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1216,6 +1216,8 @@ void CSSStyleProperties::set_declarations_from_text(StringView css_text)
|
||||||
auto parsing_params = owner_node().has_value()
|
auto parsing_params = owner_node().has_value()
|
||||||
? Parser::ParsingParams(owner_node()->element().document())
|
? Parser::ParsingParams(owner_node()->element().document())
|
||||||
: Parser::ParsingParams();
|
: Parser::ParsingParams();
|
||||||
|
parsing_params.rule_context.append(Parser::RuleContext::Style);
|
||||||
|
|
||||||
auto style = parse_css_property_declaration_block(parsing_params, css_text);
|
auto style = parse_css_property_declaration_block(parsing_params, css_text);
|
||||||
set_the_declarations(style.properties, style.custom_properties);
|
set_the_declarations(style.properties, style.custom_properties);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1360,9 +1360,7 @@ Parser::PropertiesAndCustomProperties Parser::parse_as_property_declaration_bloc
|
||||||
};
|
};
|
||||||
|
|
||||||
// 1. Let declarations be the returned declarations from invoking parse a block’s contents with string.
|
// 1. Let declarations be the returned declarations from invoking parse a block’s contents with string.
|
||||||
m_rule_context.append(RuleContext::Style);
|
|
||||||
auto declarations_and_at_rules = parse_a_blocks_contents(m_token_stream);
|
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.
|
// 2. Let parsed declarations be a new empty list.
|
||||||
PropertiesAndCustomProperties parsed_declarations;
|
PropertiesAndCustomProperties parsed_declarations;
|
||||||
|
|
|
@ -2,14 +2,14 @@ Harness status: OK
|
||||||
|
|
||||||
Found 34 tests
|
Found 34 tests
|
||||||
|
|
||||||
26 Pass
|
30 Pass
|
||||||
8 Fail
|
4 Fail
|
||||||
Pass @media is CSSGroupingRule
|
Pass @media is CSSGroupingRule
|
||||||
Pass @media rule type
|
Pass @media rule type
|
||||||
Pass Empty @media rule length
|
Pass Empty @media rule length
|
||||||
Fail insertRule of @import into @media
|
Fail insertRule of @import into @media
|
||||||
Fail insertRule into empty @media at bad index
|
Pass insertRule into empty @media at bad index
|
||||||
Fail insertRule into @media updates length
|
Pass insertRule into @media updates length
|
||||||
Pass insertRule of valid @media into @media
|
Pass insertRule of valid @media into @media
|
||||||
Pass insertRule of valid style rule into @media
|
Pass insertRule of valid style rule into @media
|
||||||
Fail insertRule of invalid @media into @media
|
Fail insertRule of invalid @media into @media
|
||||||
|
@ -25,8 +25,8 @@ Pass @supports is CSSGroupingRule
|
||||||
Pass @supports rule type
|
Pass @supports rule type
|
||||||
Pass Empty @supports rule length
|
Pass Empty @supports rule length
|
||||||
Fail insertRule of @import into @supports
|
Fail insertRule of @import into @supports
|
||||||
Fail insertRule into empty @supports at bad index
|
Pass insertRule into empty @supports at bad index
|
||||||
Fail insertRule into @supports updates length
|
Pass insertRule into @supports updates length
|
||||||
Pass insertRule of valid @media into @supports
|
Pass insertRule of valid @media into @supports
|
||||||
Pass insertRule of valid style rule into @supports
|
Pass insertRule of valid style rule into @supports
|
||||||
Fail insertRule of invalid @media into @supports
|
Fail insertRule of invalid @media into @supports
|
||||||
|
|
|
@ -2,8 +2,8 @@ Harness status: OK
|
||||||
|
|
||||||
Found 12 tests
|
Found 12 tests
|
||||||
|
|
||||||
7 Pass
|
8 Pass
|
||||||
5 Fail
|
4 Fail
|
||||||
Fail Trailing declarations
|
Fail Trailing declarations
|
||||||
Fail Mixed declarations
|
Fail Mixed declarations
|
||||||
Fail CSSNestedDeclarations.style
|
Fail CSSNestedDeclarations.style
|
||||||
|
@ -12,7 +12,7 @@ Pass Nested @scope rule
|
||||||
Fail Inner rule starting with an ident
|
Fail Inner rule starting with an ident
|
||||||
Pass Inserting a CSSNestedDeclaration rule into style rule
|
Pass Inserting a CSSNestedDeclaration rule into style rule
|
||||||
Pass Inserting a CSSNestedDeclaration rule into nested group 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 into a stylesheet
|
||||||
Pass Attempting to insert a CSSNestedDeclaration rule, empty block
|
Pass Attempting to insert a CSSNestedDeclaration rule, empty block
|
||||||
Pass Attempting to insert a CSSNestedDeclaration rule, all invalid declarations
|
Pass Attempting to insert a CSSNestedDeclaration rule, all invalid declarations
|
Loading…
Add table
Add a link
Reference in a new issue