Skip to content

Commit

Permalink
LibWeb/CSS: Stop assuming CSSNestedDeclarations's parent is CSSStyleRule
Browse files Browse the repository at this point in the history
This will no longer be true once we implement at-rules nested inside
style rules, for example:

```css
.foo {
  color: yellow;

  @media (min-width: 800px) {
    color: orange;
  }
}
```
  • Loading branch information
AtkinsSJ authored and awesomekling committed Nov 7, 2024
1 parent f3139c0 commit e138310
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
24 changes: 24 additions & 0 deletions Userland/Libraries/LibWeb/CSS/CSSNestedDeclarations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "CSSNestedDeclarations.h"
#include <LibWeb/Bindings/CSSNestedDeclarationsPrototype.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/CSS/CSSStyleRule.h>

namespace Web::CSS {

Expand Down Expand Up @@ -34,13 +35,30 @@ void CSSNestedDeclarations::visit_edges(Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_declaration);
visitor.visit(m_parent_style_rule);
}

CSSStyleDeclaration* CSSNestedDeclarations::style()
{
return m_declaration;
}

CSSStyleRule const& CSSNestedDeclarations::parent_style_rule() const
{
if (m_parent_style_rule)
return *m_parent_style_rule;

for (auto* parent = parent_rule(); parent; parent = parent->parent_rule()) {
if (is<CSSStyleRule>(parent)) {
m_parent_style_rule = static_cast<CSSStyleRule const*>(parent);
return *m_parent_style_rule;
}
}

dbgln("CSSNestedDeclarations has no parent style rule!");
VERIFY_NOT_REACHED();
}

String CSSNestedDeclarations::serialized() const
{
// NOTE: There's no proper spec for this yet, only this note:
Expand All @@ -50,4 +68,10 @@ String CSSNestedDeclarations::serialized() const
return m_declaration->serialized();
}

void CSSNestedDeclarations::clear_caches()
{
Base::clear_caches();
m_parent_style_rule = nullptr;
}

}
4 changes: 4 additions & 0 deletions Userland/Libraries/LibWeb/CSS/CSSNestedDeclarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ class CSSNestedDeclarations final : public CSSRule {

CSSStyleDeclaration* style();

CSSStyleRule const& parent_style_rule() const;

private:
CSSNestedDeclarations(JS::Realm&, PropertyOwningCSSStyleDeclaration&);

virtual void initialize(JS::Realm&) override;
virtual void visit_edges(Cell::Visitor&) override;
virtual String serialized() const override;
virtual void clear_caches() override;

JS::NonnullGCPtr<PropertyOwningCSSStyleDeclaration> m_declaration;
JS::GCPtr<CSSStyleRule const> mutable m_parent_style_rule;
};

template<>
Expand Down
6 changes: 3 additions & 3 deletions Userland/Libraries/LibWeb/CSS/StyleComputer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ SelectorList const& MatchingRule::absolutized_selectors() const
if (rule->type() == CSSRule::Type::Style)
return static_cast<CSSStyleRule const&>(*rule).absolutized_selectors();
if (rule->type() == CSSRule::Type::NestedDeclarations)
return static_cast<CSSStyleRule const&>(*rule->parent_rule()).absolutized_selectors();
return static_cast<CSSNestedDeclarations const&>(*rule).parent_style_rule().absolutized_selectors();
VERIFY_NOT_REACHED();
}

Expand All @@ -120,7 +120,7 @@ FlyString const& MatchingRule::qualified_layer_name() const
if (rule->type() == CSSRule::Type::Style)
return static_cast<CSSStyleRule const&>(*rule).qualified_layer_name();
if (rule->type() == CSSRule::Type::NestedDeclarations)
return static_cast<CSSStyleRule const&>(*rule->parent_rule()).qualified_layer_name();
return static_cast<CSSNestedDeclarations const&>(*rule).parent_style_rule().qualified_layer_name();
VERIFY_NOT_REACHED();
}

Expand Down Expand Up @@ -2462,7 +2462,7 @@ NonnullOwnPtr<StyleComputer::RuleCache> StyleComputer::make_rule_cache_for_casca
if (rule.type() == CSSRule::Type::Style)
return static_cast<CSSStyleRule const&>(rule).absolutized_selectors();
if (rule.type() == CSSRule::Type::NestedDeclarations)
return static_cast<CSSStyleRule const&>(*rule.parent_rule()).absolutized_selectors();
return static_cast<CSSNestedDeclarations const&>(rule).parent_style_rule().absolutized_selectors();
VERIFY_NOT_REACHED();
}();
for (CSS::Selector const& selector : absolutized_selectors) {
Expand Down

0 comments on commit e138310

Please sign in to comment.