LibWeb/CSS: Combine the CSSRuleList constructors

This commit is contained in:
Sam Atkins 2025-04-14 16:02:29 +01:00
parent 3d1665cc80
commit 9465492edf
Notes: github-actions[bot] 2025-04-23 10:40:08 +00:00
7 changed files with 19 additions and 25 deletions

View file

@ -22,11 +22,11 @@ namespace Web::CSS {
GC_DEFINE_ALLOCATOR(CSSRuleList);
GC::Ref<CSSRuleList> CSSRuleList::create(JS::Realm& realm, GC::RootVector<CSSRule*> const& rules)
GC::Ref<CSSRuleList> CSSRuleList::create(JS::Realm& realm, ReadonlySpan<GC::Ref<CSSRule>> rules)
{
auto rule_list = realm.create<CSSRuleList>(realm);
for (auto* rule : rules)
rule_list->m_rules.append(*rule);
for (auto rule : rules)
rule_list->m_rules.append(rule);
return rule_list;
}
@ -36,11 +36,6 @@ CSSRuleList::CSSRuleList(JS::Realm& realm)
m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = 1 };
}
GC::Ref<CSSRuleList> CSSRuleList::create_empty(JS::Realm& realm)
{
return realm.create<CSSRuleList>(realm);
}
void CSSRuleList::initialize(JS::Realm& realm)
{
WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSRuleList);

View file

@ -23,8 +23,7 @@ class CSSRuleList : public Bindings::PlatformObject {
GC_DECLARE_ALLOCATOR(CSSRuleList);
public:
[[nodiscard]] static GC::Ref<CSSRuleList> create(JS::Realm&, GC::RootVector<CSSRule*> const&);
[[nodiscard]] static GC::Ref<CSSRuleList> create_empty(JS::Realm&);
[[nodiscard]] static GC::Ref<CSSRuleList> create(JS::Realm&, ReadonlySpan<GC::Ref<CSSRule>> = {});
~CSSRuleList() = default;

View file

@ -33,7 +33,7 @@ GC::Ref<CSSStyleSheet> CSSStyleSheet::create(JS::Realm& realm, CSSRuleList& rule
WebIDL::ExceptionOr<GC::Ref<CSSStyleSheet>> CSSStyleSheet::construct_impl(JS::Realm& realm, Optional<CSSStyleSheetInit> const& options)
{
// 1. Construct a new CSSStyleSheet object sheet.
auto sheet = create(realm, CSSRuleList::create_empty(realm), CSS::MediaList::create(realm, {}), {});
auto sheet = create(realm, CSSRuleList::create(realm), CSS::MediaList::create(realm, {}), {});
// 2. Set sheets location to the base URL of the associated Document for the current principal global object.
auto associated_document = as<HTML::Window>(HTML::current_principal_global_object()).document();

View file

@ -45,7 +45,7 @@ GC::Ref<JS::Realm> internal_css_realm()
GC::Ref<CSS::CSSStyleSheet> parse_css_stylesheet(CSS::Parser::ParsingParams const& context, StringView css, Optional<::URL::URL> location, Vector<NonnullRefPtr<CSS::MediaQuery>> media_query_list)
{
if (css.is_empty()) {
auto rule_list = CSS::CSSRuleList::create_empty(*context.realm);
auto rule_list = CSS::CSSRuleList::create(*context.realm);
auto media_list = CSS::MediaList::create(*context.realm, {});
auto style_sheet = CSS::CSSStyleSheet::create(*context.realm, rule_list, media_list, location);
style_sheet->set_source_text({});

View file

@ -497,12 +497,12 @@ GC::Ptr<CSSMediaRule> Parser::convert_to_media_rule(AtRule const& rule, Nested n
auto media_query_list = parse_a_media_query_list(media_query_tokens);
auto media_list = MediaList::create(realm(), move(media_query_list));
GC::RootVector<CSSRule*> child_rules { realm().heap() };
GC::RootVector<GC::Ref<CSSRule>> child_rules { realm().heap() };
for (auto const& child : rule.child_rules_and_lists_of_declarations) {
child.visit(
[&](Rule const& rule) {
if (auto child_rule = convert_to_rule(rule, nested))
child_rules.append(child_rule);
child_rules.append(*child_rule);
},
[&](Vector<Declaration> const& declarations) {
child_rules.append(CSSNestedDeclarations::create(realm(), *convert_to_style_declaration(declarations)));

View file

@ -141,7 +141,7 @@ GC::Ref<CSS::CSSStyleSheet> Parser::parse_as_css_stylesheet(Optional<::URL::URL>
auto const& style_sheet = parse_a_stylesheet(m_token_stream, location);
// Interpret all of the resulting top-level qualified rules as style rules, defined below.
GC::RootVector<CSSRule*> rules(realm().heap());
GC::RootVector<GC::Ref<CSSRule>> rules(realm().heap());
for (auto const& raw_rule : style_sheet.rules) {
auto rule = convert_to_rule(raw_rule, Nested::No);
// If any style rule is invalid, or any at-rule is not recognized or is invalid according to its grammar or context, its a parse error.
@ -150,7 +150,7 @@ GC::Ref<CSS::CSSStyleSheet> Parser::parse_as_css_stylesheet(Optional<::URL::URL>
log_parse_error();
continue;
}
rules.append(rule);
rules.append(*rule);
}
auto rule_list = CSSRuleList::create(realm(), rules);

View file

@ -107,7 +107,7 @@ GC::Ptr<CSSStyleRule> Parser::convert_to_style_rule(QualifiedRule const& qualifi
auto declaration = convert_to_style_declaration(qualified_rule.declarations);
GC::RootVector<CSSRule*> child_rules { realm().heap() };
GC::RootVector<GC::Ref<CSSRule>> child_rules { realm().heap() };
for (auto& child : qualified_rule.child_rules) {
child.visit(
[&](Rule const& rule) {
@ -116,7 +116,7 @@ GC::Ptr<CSSStyleRule> Parser::convert_to_style_rule(QualifiedRule const& qualifi
// https://drafts.csswg.org/css-nesting-1/#nested-group-rules
if (auto converted_rule = convert_to_rule(rule, Nested::Yes)) {
if (is<CSSGroupingRule>(*converted_rule)) {
child_rules.append(converted_rule);
child_rules.append(*converted_rule);
} else {
dbgln_if(CSS_PARSER_DEBUG, "CSSParser: nested {} is not allowed inside style rule; discarding.", converted_rule->class_name());
}
@ -126,7 +126,7 @@ GC::Ptr<CSSStyleRule> Parser::convert_to_style_rule(QualifiedRule const& qualifi
child_rules.append(CSSNestedDeclarations::create(realm(), *convert_to_style_declaration(declarations)));
});
}
auto nested_rules = CSSRuleList::create(realm(), move(child_rules));
auto nested_rules = CSSRuleList::create(realm(), child_rules);
return CSSStyleRule::create(realm(), move(selectors), *declaration, *nested_rules);
}
@ -260,12 +260,12 @@ GC::Ptr<CSSRule> Parser::convert_to_layer_rule(AtRule const& rule, Nested nested
}
// Then the rules
GC::RootVector<CSSRule*> child_rules { realm().heap() };
GC::RootVector<GC::Ref<CSSRule>> child_rules { realm().heap() };
for (auto const& child : rule.child_rules_and_lists_of_declarations) {
child.visit(
[&](Rule const& rule) {
if (auto child_rule = convert_to_rule(rule, nested))
child_rules.append(child_rule);
child_rules.append(*child_rule);
},
[&](Vector<Declaration> const& declarations) {
child_rules.append(CSSNestedDeclarations::create(realm(), *convert_to_style_declaration(declarations)));
@ -350,7 +350,7 @@ GC::Ptr<CSSKeyframesRule> Parser::convert_to_keyframes_rule(AtRule const& rule)
auto name = name_token.to_string();
GC::RootVector<CSSRule*> keyframes(realm().heap());
GC::RootVector<GC::Ref<CSSRule>> keyframes(realm().heap());
rule.for_each_as_qualified_rule_list([&](auto& qualified_rule) {
if (!qualified_rule.child_rules.is_empty()) {
dbgln_if(CSS_PARSER_DEBUG, "CSSParser: @keyframes keyframe rule contains at-rules; discarding them.");
@ -405,7 +405,7 @@ GC::Ptr<CSSKeyframesRule> Parser::convert_to_keyframes_rule(AtRule const& rule)
}
});
return CSSKeyframesRule::create(realm(), name, CSSRuleList::create(realm(), move(keyframes)));
return CSSKeyframesRule::create(realm(), name, CSSRuleList::create(realm(), keyframes));
}
GC::Ptr<CSSNamespaceRule> Parser::convert_to_namespace_rule(AtRule const& rule)
@ -480,12 +480,12 @@ GC::Ptr<CSSSupportsRule> Parser::convert_to_supports_rule(AtRule const& rule, Ne
return {};
}
GC::RootVector<CSSRule*> child_rules { realm().heap() };
GC::RootVector<GC::Ref<CSSRule>> child_rules { realm().heap() };
for (auto const& child : rule.child_rules_and_lists_of_declarations) {
child.visit(
[&](Rule const& rule) {
if (auto child_rule = convert_to_rule(rule, nested))
child_rules.append(child_rule);
child_rules.append(*child_rule);
},
[&](Vector<Declaration> const& declarations) {
child_rules.append(CSSNestedDeclarations::create(realm(), *convert_to_style_declaration(declarations)));