Skip to content

Commit a12d28f

Browse files
committed
LibWeb: Implement CSSStyleDeclaration.parentRule
This readonly attribute returns the containing CSS rule, or null (in the case of element inline style).
1 parent ec4d298 commit a12d28f

File tree

8 files changed

+55
-10
lines changed

8 files changed

+55
-10
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
spanRule: [object CSSStyleRule] ~ span { color: rgb(128, 0, 128); }
2+
spanRule.style: [object CSSStyleDeclaration] ~ span { color: rgb(128, 0, 128); }
3+
spanRule.style.parentRule: [object CSSStyleRule] ~ span { color: rgb(128, 0, 128); }
4+
spanRule.style.parentRule === spanRule: true
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<style>
2+
span {
3+
color: purple;
4+
}
5+
</style>
6+
<script src="../include.js"></script>
7+
<script>
8+
test(() => {
9+
const spanRule = document.styleSheets[0].cssRules[0];
10+
println("spanRule: " + spanRule + " ~ " + spanRule.cssText);
11+
println("spanRule.style: " + spanRule.style + " ~ " + spanRule.cssText);
12+
println("spanRule.style.parentRule: " + spanRule.style.parentRule + " ~ " + spanRule.cssText);
13+
println("spanRule.style.parentRule === spanRule: " + (spanRule.style.parentRule === spanRule));
14+
});
15+
</script>

Userland/Libraries/LibWeb/CSS/CSSKeyframeRule.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,19 @@ namespace Web::CSS {
1313

1414
JS_DEFINE_ALLOCATOR(CSSKeyframeRule);
1515

16-
JS::NonnullGCPtr<CSSKeyframeRule> CSSKeyframeRule::create(JS::Realm& realm, CSS::Percentage key, Web::CSS::CSSStyleDeclaration& declarations)
16+
JS::NonnullGCPtr<CSSKeyframeRule> CSSKeyframeRule::create(JS::Realm& realm, CSS::Percentage key, Web::CSS::PropertyOwningCSSStyleDeclaration& declarations)
1717
{
1818
return realm.heap().allocate<CSSKeyframeRule>(realm, realm, key, declarations);
1919
}
2020

21+
CSSKeyframeRule::CSSKeyframeRule(JS::Realm& realm, CSS::Percentage key, PropertyOwningCSSStyleDeclaration& declarations)
22+
: CSSRule(realm)
23+
, m_key(key)
24+
, m_declarations(declarations)
25+
{
26+
m_declarations->set_parent_rule(*this);
27+
}
28+
2129
void CSSKeyframeRule::visit_edges(Visitor& visitor)
2230
{
2331
Base::visit_edges(visitor);

Userland/Libraries/LibWeb/CSS/CSSKeyframeRule.h

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class CSSKeyframeRule final : public CSSRule {
2121
JS_DECLARE_ALLOCATOR(CSSKeyframeRule);
2222

2323
public:
24-
static JS::NonnullGCPtr<CSSKeyframeRule> create(JS::Realm&, CSS::Percentage key, CSSStyleDeclaration&);
24+
static JS::NonnullGCPtr<CSSKeyframeRule> create(JS::Realm&, CSS::Percentage key, PropertyOwningCSSStyleDeclaration&);
2525

2626
virtual ~CSSKeyframeRule() = default;
2727

@@ -41,19 +41,14 @@ class CSSKeyframeRule final : public CSSRule {
4141
}
4242

4343
private:
44-
CSSKeyframeRule(JS::Realm& realm, CSS::Percentage key, CSSStyleDeclaration& declarations)
45-
: CSSRule(realm)
46-
, m_key(key)
47-
, m_declarations(declarations)
48-
{
49-
}
44+
CSSKeyframeRule(JS::Realm&, CSS::Percentage, PropertyOwningCSSStyleDeclaration&);
5045

5146
virtual void visit_edges(Visitor&) override;
5247
virtual void initialize(JS::Realm&) override;
5348
virtual String serialized() const override;
5449

5550
CSS::Percentage m_key;
56-
JS::NonnullGCPtr<CSSStyleDeclaration> m_declarations;
51+
JS::NonnullGCPtr<PropertyOwningCSSStyleDeclaration> m_declarations;
5752
};
5853

5954
template<>

Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ void CSSStyleDeclaration::initialize(JS::Realm& realm)
3131
WEB_SET_PROTOTYPE_FOR_INTERFACE(CSSStyleDeclaration);
3232
}
3333

34+
JS::GCPtr<CSSRule> CSSStyleDeclaration::parent_rule() const
35+
{
36+
return nullptr;
37+
}
38+
3439
JS::NonnullGCPtr<PropertyOwningCSSStyleDeclaration> PropertyOwningCSSStyleDeclaration::create(JS::Realm& realm, Vector<StyleProperty> properties, HashMap<FlyString, StyleProperty> custom_properties)
3540
{
3641
return realm.heap().allocate<PropertyOwningCSSStyleDeclaration>(realm, realm, move(properties), move(custom_properties));
@@ -46,6 +51,7 @@ PropertyOwningCSSStyleDeclaration::PropertyOwningCSSStyleDeclaration(JS::Realm&
4651
void PropertyOwningCSSStyleDeclaration::visit_edges(Cell::Visitor& visitor)
4752
{
4853
Base::visit_edges(visitor);
54+
visitor.visit(m_parent_rule);
4955
for (auto& property : m_properties) {
5056
if (property.value->is_image())
5157
property.value->as_image().visit_edges(visitor);
@@ -482,4 +488,14 @@ WebIDL::ExceptionOr<void> ElementInlineCSSStyleDeclaration::set_css_text(StringV
482488
return {};
483489
}
484490

491+
JS::GCPtr<CSSRule> PropertyOwningCSSStyleDeclaration::parent_rule() const
492+
{
493+
return m_parent_rule;
494+
}
495+
496+
void PropertyOwningCSSStyleDeclaration::set_parent_rule(JS::NonnullGCPtr<CSSRule> rule)
497+
{
498+
m_parent_rule = rule;
499+
}
500+
485501
}

Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class CSSStyleDeclaration : public Bindings::PlatformObject {
4545
virtual JS::ThrowCompletionOr<JS::Value> internal_get(JS::PropertyKey const&, JS::Value receiver, JS::CacheablePropertyMetadata*, PropertyLookupPhase) const override;
4646
virtual JS::ThrowCompletionOr<bool> internal_set(JS::PropertyKey const&, JS::Value value, JS::Value receiver, JS::CacheablePropertyMetadata*) override;
4747

48+
virtual JS::GCPtr<CSSRule> parent_rule() const;
49+
4850
protected:
4951
explicit CSSStyleDeclaration(JS::Realm&);
5052
};
@@ -77,6 +79,9 @@ class PropertyOwningCSSStyleDeclaration : public CSSStyleDeclaration {
7779
virtual String serialized() const final override;
7880
virtual WebIDL::ExceptionOr<void> set_css_text(StringView) override;
7981

82+
virtual JS::GCPtr<CSSRule> parent_rule() const override;
83+
void set_parent_rule(JS::NonnullGCPtr<CSSRule>);
84+
8085
protected:
8186
PropertyOwningCSSStyleDeclaration(JS::Realm&, Vector<StyleProperty>, HashMap<FlyString, StyleProperty>);
8287

@@ -90,6 +95,7 @@ class PropertyOwningCSSStyleDeclaration : public CSSStyleDeclaration {
9095

9196
virtual void visit_edges(Cell::Visitor&) override;
9297

98+
JS::GCPtr<CSSRule> m_parent_rule;
9399
Vector<StyleProperty> m_properties;
94100
HashMap<FlyString, StyleProperty> m_custom_properties;
95101
};

Userland/Libraries/LibWeb/CSS/CSSStyleDeclaration.idl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ interface CSSStyleDeclaration {
1313
[CEReactions] undefined setProperty(CSSOMString property, [LegacyNullToEmptyString] CSSOMString value, optional [LegacyNullToEmptyString] CSSOMString priority = "");
1414
[CEReactions] CSSOMString removeProperty(CSSOMString property);
1515

16-
[FIXME] readonly attribute CSSRule? parentRule;
16+
readonly attribute CSSRule? parentRule;
1717
[FIXME, CEReactions, LegacyNullToEmptyString] attribute CSSOMString cssFloat;
1818

1919
};

Userland/Libraries/LibWeb/CSS/CSSStyleRule.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ CSSStyleRule::CSSStyleRule(JS::Realm& realm, Vector<NonnullRefPtr<Selector>>&& s
2525
, m_selectors(move(selectors))
2626
, m_declaration(declaration)
2727
{
28+
m_declaration->set_parent_rule(*this);
2829
}
2930

3031
void CSSStyleRule::initialize(JS::Realm& realm)

0 commit comments

Comments
 (0)