Skip to content

Commit

Permalink
WIP: Allow nested things in at-rules
Browse files Browse the repository at this point in the history
This is a bit repetitive, and we're doing something odd with calling
evaluate_media_queries way too often (at least once per frame) so more
needs doing!
  • Loading branch information
AtkinsSJ committed Nov 1, 2024
1 parent 8428f8a commit 036a39c
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 14 deletions.
8 changes: 7 additions & 1 deletion Userland/Libraries/LibWeb/CSS/CSSRuleList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,19 @@ bool CSSRuleList::evaluate_media_queries(HTML::Window const& window)
any_media_queries_changed_match_state = true;
break;
}
case CSSRule::Type::Style: {
dbgln("Evaluating media queries in style rule");
auto& style_rule = verify_cast<CSSStyleRule>(*rule);
if (style_rule.css_rules().evaluate_media_queries(window))
any_media_queries_changed_match_state = true;
break;
}
case CSSRule::Type::FontFace:
case CSSRule::Type::Keyframe:
case CSSRule::Type::Keyframes:
case CSSRule::Type::LayerStatement:
case CSSRule::Type::Namespace:
case CSSRule::Type::NestedDeclarations:
case CSSRule::Type::Style:
case CSSRule::Type::Property:
break;
}
Expand Down
22 changes: 17 additions & 5 deletions Userland/Libraries/LibWeb/CSS/Parser/MediaParsing.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Copyright (c) 2018-2022, Andreas Kling <andreas@ladybird.org>
* Copyright (c) 2020-2021, the SerenityOS developers.
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021-2024, Sam Atkins <sam@ladybird.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2022, MacDue <macdue@dueutil.tech>
*
Expand All @@ -10,6 +10,7 @@

#include <AK/Debug.h>
#include <LibWeb/CSS/CSSMediaRule.h>
#include <LibWeb/CSS/CSSNestedDeclarations.h>
#include <LibWeb/CSS/CalculatedOr.h>
#include <LibWeb/CSS/MediaList.h>
#include <LibWeb/CSS/MediaQuery.h>
Expand Down Expand Up @@ -627,10 +628,21 @@ JS::GCPtr<CSSMediaRule> Parser::convert_to_media_rule(AtRule const& rule, Nested
auto media_list = MediaList::create(m_context.realm(), move(media_query_list));

JS::MarkedVector<CSSRule*> child_rules { m_context.realm().heap() };
rule.for_each_as_rule_list([&](auto& rule) {
if (auto child_rule = convert_to_rule(rule, nested))
child_rules.append(child_rule);
});
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);
},
[&](Vector<Declaration> const& declarations) {
auto* declaration = convert_to_style_declaration(declarations);
if (!declaration) {
dbgln_if(CSS_PARSER_DEBUG, "CSSParser: nested declarations invalid; discarding.");
return;
}
child_rules.append(CSSNestedDeclarations::create(m_context.realm(), *declaration));
});
}
auto rule_list = CSSRuleList::create(m_context.realm(), child_rules);
return CSSMediaRule::create(m_context.realm(), media_list, rule_list);
}
Expand Down
38 changes: 30 additions & 8 deletions Userland/Libraries/LibWeb/CSS/Parser/RuleParsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,10 +278,21 @@ JS::GCPtr<CSSRule> Parser::convert_to_layer_rule(AtRule const& rule, Nested nest

// Then the rules
JS::MarkedVector<CSSRule*> child_rules { m_context.realm().heap() };
rule.for_each_as_rule_list([&](auto& rule) {
if (auto child_rule = convert_to_rule(rule, nested))
child_rules.append(child_rule);
});
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);
},
[&](Vector<Declaration> const& declarations) {
auto* declaration = convert_to_style_declaration(declarations);
if (!declaration) {
dbgln_if(CSS_PARSER_DEBUG, "CSSParser: nested declarations invalid; discarding.");
return;
}
child_rules.append(CSSNestedDeclarations::create(m_context.realm(), *declaration));
});
}
auto rule_list = CSSRuleList::create(m_context.realm(), child_rules);
return CSSLayerBlockRule::create(m_context.realm(), layer_name, rule_list);
}
Expand Down Expand Up @@ -489,10 +500,21 @@ JS::GCPtr<CSSSupportsRule> Parser::convert_to_supports_rule(AtRule const& rule,
}

JS::MarkedVector<CSSRule*> child_rules { m_context.realm().heap() };
rule.for_each_as_rule_list([&](auto& rule) {
if (auto child_rule = convert_to_rule(rule, nested))
child_rules.append(child_rule);
});
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);
},
[&](Vector<Declaration> const& declarations) {
auto* declaration = convert_to_style_declaration(declarations);
if (!declaration) {
dbgln_if(CSS_PARSER_DEBUG, "CSSParser: nested declarations invalid; discarding.");
return;
}
child_rules.append(CSSNestedDeclarations::create(m_context.realm(), *declaration));
});
}

auto rule_list = CSSRuleList::create(m_context.realm(), child_rules);
return CSSSupportsRule::create(m_context.realm(), supports.release_nonnull(), rule_list);
Expand Down

0 comments on commit 036a39c

Please sign in to comment.