script/dom/css/
cssrule.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::cell::Cell;
6
7use dom_struct::dom_struct;
8use js::context::JSContext;
9use style::shared_lock::{SharedRwLock, SharedRwLockReadGuard};
10use style::stylesheets::{CssRule as StyleCssRule, CssRuleType};
11
12use super::cssfontfacerule::CSSFontFaceRule;
13use super::cssimportrule::CSSImportRule;
14use super::csskeyframerule::CSSKeyframeRule;
15use super::csskeyframesrule::CSSKeyframesRule;
16use super::csslayerblockrule::CSSLayerBlockRule;
17use super::csslayerstatementrule::CSSLayerStatementRule;
18use super::cssmediarule::CSSMediaRule;
19use super::cssnamespacerule::CSSNamespaceRule;
20use super::cssnesteddeclarations::CSSNestedDeclarations;
21use super::csspropertyrule::CSSPropertyRule;
22use super::cssstylerule::CSSStyleRule;
23use super::cssstylesheet::CSSStyleSheet;
24use super::csssupportsrule::CSSSupportsRule;
25use crate::dom::bindings::codegen::Bindings::CSSRuleBinding::CSSRuleMethods;
26use crate::dom::bindings::inheritance::Castable;
27use crate::dom::bindings::reflector::Reflector;
28use crate::dom::bindings::root::{Dom, DomRoot};
29use crate::dom::bindings::str::DOMString;
30use crate::dom::window::Window;
31
32#[dom_struct]
33pub(crate) struct CSSRule {
34    reflector_: Reflector,
35    parent_stylesheet: Dom<CSSStyleSheet>,
36
37    /// Whether the parentStyleSheet attribute should return null.
38    /// We keep parent_stylesheet in that case because insertRule needs it
39    /// for the stylesheet’s base URL and namespace prefixes.
40    parent_stylesheet_removed: Cell<bool>,
41}
42
43impl CSSRule {
44    pub(crate) fn new_inherited(parent_stylesheet: &CSSStyleSheet) -> CSSRule {
45        CSSRule {
46            reflector_: Reflector::new(),
47            parent_stylesheet: Dom::from_ref(parent_stylesheet),
48            parent_stylesheet_removed: Cell::new(false),
49        }
50    }
51
52    pub(crate) fn as_specific(&self) -> &dyn SpecificCSSRule {
53        if let Some(rule) = self.downcast::<CSSStyleRule>() {
54            rule as &dyn SpecificCSSRule
55        } else if let Some(rule) = self.downcast::<CSSFontFaceRule>() {
56            rule as &dyn SpecificCSSRule
57        } else if let Some(rule) = self.downcast::<CSSKeyframesRule>() {
58            rule as &dyn SpecificCSSRule
59        } else if let Some(rule) = self.downcast::<CSSMediaRule>() {
60            rule as &dyn SpecificCSSRule
61        } else if let Some(rule) = self.downcast::<CSSNamespaceRule>() {
62            rule as &dyn SpecificCSSRule
63        } else if let Some(rule) = self.downcast::<CSSKeyframeRule>() {
64            rule as &dyn SpecificCSSRule
65        } else if let Some(rule) = self.downcast::<CSSImportRule>() {
66            rule as &dyn SpecificCSSRule
67        } else if let Some(rule) = self.downcast::<CSSSupportsRule>() {
68            rule as &dyn SpecificCSSRule
69        } else if let Some(rule) = self.downcast::<CSSLayerBlockRule>() {
70            rule as &dyn SpecificCSSRule
71        } else if let Some(rule) = self.downcast::<CSSLayerStatementRule>() {
72            rule as &dyn SpecificCSSRule
73        } else if let Some(rule) = self.downcast::<CSSNestedDeclarations>() {
74            rule as &dyn SpecificCSSRule
75        } else if let Some(rule) = self.downcast::<CSSPropertyRule>() {
76            rule as &dyn SpecificCSSRule
77        } else {
78            unreachable!()
79        }
80    }
81
82    // Given a StyleCssRule, create a new instance of a derived class of
83    // CSSRule based on which rule it is
84    pub(crate) fn new_specific(
85        cx: &mut JSContext,
86        window: &Window,
87        parent_stylesheet: &CSSStyleSheet,
88        rule: StyleCssRule,
89    ) -> DomRoot<CSSRule> {
90        // be sure to update the match in as_specific when this is updated
91        match rule {
92            StyleCssRule::Import(s) => {
93                DomRoot::upcast(CSSImportRule::new(cx, window, parent_stylesheet, s))
94            },
95            StyleCssRule::Style(s) => {
96                DomRoot::upcast(CSSStyleRule::new(cx, window, parent_stylesheet, s))
97            },
98            StyleCssRule::FontFace(s) => {
99                DomRoot::upcast(CSSFontFaceRule::new(cx, window, parent_stylesheet, s))
100            },
101            StyleCssRule::FontFeatureValues(_) => unimplemented!(),
102            StyleCssRule::CounterStyle(_) => unimplemented!(),
103            StyleCssRule::Keyframes(s) => {
104                DomRoot::upcast(CSSKeyframesRule::new(cx, window, parent_stylesheet, s))
105            },
106            StyleCssRule::Media(s) => {
107                DomRoot::upcast(CSSMediaRule::new(cx, window, parent_stylesheet, s))
108            },
109            StyleCssRule::Namespace(s) => {
110                DomRoot::upcast(CSSNamespaceRule::new(cx, window, parent_stylesheet, s))
111            },
112            StyleCssRule::Supports(s) => {
113                DomRoot::upcast(CSSSupportsRule::new(cx, window, parent_stylesheet, s))
114            },
115            StyleCssRule::Page(_) => unreachable!(),
116            StyleCssRule::Container(_) => unimplemented!(), // TODO
117            StyleCssRule::Document(_) => unimplemented!(),  // TODO
118            StyleCssRule::LayerBlock(s) => {
119                DomRoot::upcast(CSSLayerBlockRule::new(cx, window, parent_stylesheet, s))
120            },
121            StyleCssRule::LayerStatement(s) => {
122                DomRoot::upcast(CSSLayerStatementRule::new(cx, window, parent_stylesheet, s))
123            },
124            StyleCssRule::FontPaletteValues(_) => unimplemented!(), // TODO
125            StyleCssRule::Property(s) => {
126                DomRoot::upcast(CSSPropertyRule::new(cx, window, parent_stylesheet, s))
127            },
128            StyleCssRule::Margin(_) => unimplemented!(), // TODO
129            StyleCssRule::Scope(_) => unimplemented!(),  // TODO
130            StyleCssRule::StartingStyle(_) => unimplemented!(), // TODO
131            StyleCssRule::PositionTry(_) => unimplemented!(), // TODO
132            StyleCssRule::CustomMedia(_) => unimplemented!(), // TODO
133            StyleCssRule::NestedDeclarations(s) => {
134                DomRoot::upcast(CSSNestedDeclarations::new(cx, window, parent_stylesheet, s))
135            },
136            StyleCssRule::AppearanceBase(_) => unimplemented!(), // TODO
137            StyleCssRule::ViewTransition(_) => unimplemented!(), // TODO
138        }
139    }
140
141    /// Sets owner sheet/rule to null
142    pub(crate) fn detach(&self) {
143        self.deparent();
144        // should set parent rule to None when we add parent rule support
145    }
146
147    /// Sets owner sheet to null (and does the same for all children)
148    pub(crate) fn deparent(&self) {
149        self.parent_stylesheet_removed.set(true);
150        // https://github.com/w3c/csswg-drafts/issues/722
151        // Spec doesn't ask us to do this, but it makes sense
152        // and browsers implement this behavior
153        self.as_specific().deparent_children();
154    }
155
156    pub(crate) fn parent_stylesheet(&self) -> &CSSStyleSheet {
157        &self.parent_stylesheet
158    }
159
160    pub(crate) fn shared_lock(&self) -> &SharedRwLock {
161        self.parent_stylesheet.shared_lock()
162    }
163
164    pub(crate) fn update_rule(&self, style_rule: &StyleCssRule, guard: &SharedRwLockReadGuard) {
165        match style_rule {
166            StyleCssRule::Import(s) => {
167                if let Some(rule) = self.downcast::<CSSImportRule>() {
168                    rule.update_rule(s.clone());
169                }
170            },
171            StyleCssRule::Style(s) => {
172                if let Some(rule) = self.downcast::<CSSStyleRule>() {
173                    rule.update_rule(s.clone(), guard);
174                }
175            },
176            StyleCssRule::FontFace(s) => {
177                if let Some(rule) = self.downcast::<CSSFontFaceRule>() {
178                    rule.update_rule(s.clone());
179                }
180            },
181            StyleCssRule::FontFeatureValues(_) => unimplemented!(),
182            StyleCssRule::CounterStyle(_) => unimplemented!(),
183            StyleCssRule::Keyframes(s) => {
184                if let Some(rule) = self.downcast::<CSSKeyframesRule>() {
185                    rule.update_rule(s.clone(), guard);
186                }
187            },
188            StyleCssRule::Media(s) => {
189                if let Some(rule) = self.downcast::<CSSMediaRule>() {
190                    rule.update_rule(s.clone(), guard);
191                }
192            },
193            StyleCssRule::Namespace(s) => {
194                if let Some(rule) = self.downcast::<CSSNamespaceRule>() {
195                    rule.update_rule(s.clone());
196                }
197            },
198            StyleCssRule::Supports(s) => {
199                if let Some(rule) = self.downcast::<CSSSupportsRule>() {
200                    rule.update_rule(s.clone(), guard);
201                }
202            },
203            StyleCssRule::Page(_) => unreachable!(),
204            StyleCssRule::Container(_) => unimplemented!(), // TODO
205            StyleCssRule::Document(_) => unimplemented!(),  // TODO
206            StyleCssRule::LayerBlock(s) => {
207                if let Some(rule) = self.downcast::<CSSLayerBlockRule>() {
208                    rule.update_rule(s.clone(), guard);
209                }
210            },
211            StyleCssRule::LayerStatement(s) => {
212                if let Some(rule) = self.downcast::<CSSLayerStatementRule>() {
213                    rule.update_rule(s.clone());
214                }
215            },
216            StyleCssRule::FontPaletteValues(_) => unimplemented!(), // TODO
217            StyleCssRule::Property(s) => {
218                if let Some(rule) = self.downcast::<CSSPropertyRule>() {
219                    rule.update_rule(s.clone());
220                }
221            },
222            StyleCssRule::Margin(_) => unimplemented!(), // TODO
223            StyleCssRule::Scope(_) => unimplemented!(),  // TODO
224            StyleCssRule::StartingStyle(_) => unimplemented!(), // TODO
225            StyleCssRule::PositionTry(_) => unimplemented!(), // TODO
226            StyleCssRule::CustomMedia(_) => unimplemented!(), // TODO
227            StyleCssRule::NestedDeclarations(s) => {
228                if let Some(rule) = self.downcast::<CSSNestedDeclarations>() {
229                    rule.update_rule(s.clone(), guard);
230                }
231            },
232            StyleCssRule::AppearanceBase(_) => unimplemented!(), // TODO
233            StyleCssRule::ViewTransition(_) => unimplemented!(), // TODO
234        }
235    }
236}
237
238impl CSSRuleMethods<crate::DomTypeHolder> for CSSRule {
239    /// <https://drafts.csswg.org/cssom/#dom-cssrule-type>
240    fn Type(&self) -> u16 {
241        let rule_type = self.as_specific().ty() as u16;
242        // Per https://drafts.csswg.org/cssom/#dom-cssrule-type for constants > 15
243        // we return 0.
244        if rule_type > 15 { 0 } else { rule_type }
245    }
246
247    /// <https://drafts.csswg.org/cssom/#dom-cssrule-parentstylesheet>
248    fn GetParentStyleSheet(&self) -> Option<DomRoot<CSSStyleSheet>> {
249        if self.parent_stylesheet_removed.get() {
250            None
251        } else {
252            Some(DomRoot::from_ref(&*self.parent_stylesheet))
253        }
254    }
255
256    /// <https://drafts.csswg.org/cssom/#dom-cssrule-csstext>
257    fn CssText(&self) -> DOMString {
258        self.as_specific().get_css()
259    }
260
261    /// <https://drafts.csswg.org/cssom/#dom-cssrule-csstext>
262    fn SetCssText(&self, _: DOMString) {
263        // do nothing
264    }
265}
266
267pub(crate) trait SpecificCSSRule {
268    fn ty(&self) -> CssRuleType;
269    fn get_css(&self) -> DOMString;
270    /// Remove parentStylesheet from all transitive children
271    fn deparent_children(&self) {
272        // most CSSRules do nothing here
273    }
274}