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