Skip to main content

script/dom/css/
cssconditionrule.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::RefCell;
6
7use dom_struct::dom_struct;
8use servo_arc::Arc;
9use style::shared_lock::{Locked, SharedRwLock, SharedRwLockReadGuard};
10use style::stylesheets::CssRules as StyleCssRules;
11
12use super::cssgroupingrule::CSSGroupingRule;
13use super::cssmediarule::CSSMediaRule;
14use super::cssstylesheet::CSSStyleSheet;
15use super::csssupportsrule::CSSSupportsRule;
16use crate::dom::bindings::codegen::Bindings::CSSConditionRuleBinding::CSSConditionRuleMethods;
17use crate::dom::bindings::inheritance::Castable;
18use crate::dom::bindings::str::DOMString;
19
20#[dom_struct]
21pub(crate) struct CSSConditionRule {
22    css_grouping_rule: CSSGroupingRule,
23    #[ignore_malloc_size_of = "Stylo"]
24    #[no_trace]
25    rules: RefCell<Arc<Locked<StyleCssRules>>>,
26}
27
28impl CSSConditionRule {
29    pub(crate) fn new_inherited(
30        parent_rule: Option<&CSSGroupingRule>,
31        parent_stylesheet: &CSSStyleSheet,
32        rules: Arc<Locked<StyleCssRules>>,
33    ) -> CSSConditionRule {
34        CSSConditionRule {
35            css_grouping_rule: CSSGroupingRule::new_inherited(parent_rule, parent_stylesheet),
36            rules: RefCell::new(rules),
37        }
38    }
39
40    pub(crate) fn parent_stylesheet(&self) -> &CSSStyleSheet {
41        self.css_grouping_rule.parent_stylesheet()
42    }
43
44    pub(crate) fn shared_lock(&self) -> &SharedRwLock {
45        self.css_grouping_rule.shared_lock()
46    }
47
48    pub(crate) fn clone_rules(&self) -> Arc<Locked<StyleCssRules>> {
49        self.rules.borrow().clone()
50    }
51
52    pub(crate) fn update_rules(
53        &self,
54        rules: Arc<Locked<StyleCssRules>>,
55        guard: &SharedRwLockReadGuard,
56    ) {
57        self.css_grouping_rule.update_rules(&rules, guard);
58        *self.rules.borrow_mut() = rules;
59    }
60}
61
62impl CSSConditionRuleMethods<crate::DomTypeHolder> for CSSConditionRule {
63    /// <https://drafts.csswg.org/css-conditional-3/#dom-cssconditionrule-conditiontext>
64    fn ConditionText(&self) -> DOMString {
65        if let Some(rule) = self.downcast::<CSSMediaRule>() {
66            rule.get_condition_text()
67        } else if let Some(rule) = self.downcast::<CSSSupportsRule>() {
68            rule.get_condition_text()
69        } else {
70            unreachable!()
71        }
72    }
73}