script/dom/
csssupportsrule.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::{SharedRwLockReadGuard, ToCssWithGuard};
10use style::stylesheets::{CssRuleType, SupportsRule};
11use style_traits::ToCss;
12
13use crate::dom::bindings::reflector::reflect_dom_object;
14use crate::dom::bindings::root::DomRoot;
15use crate::dom::bindings::str::DOMString;
16use crate::dom::cssconditionrule::CSSConditionRule;
17use crate::dom::cssrule::SpecificCSSRule;
18use crate::dom::cssstylesheet::CSSStyleSheet;
19use crate::dom::window::Window;
20use crate::script_runtime::CanGc;
21
22#[dom_struct]
23pub(crate) struct CSSSupportsRule {
24    cssconditionrule: CSSConditionRule,
25    #[ignore_malloc_size_of = "Arc"]
26    #[no_trace]
27    supportsrule: RefCell<Arc<SupportsRule>>,
28}
29
30impl CSSSupportsRule {
31    fn new_inherited(
32        parent_stylesheet: &CSSStyleSheet,
33        supportsrule: Arc<SupportsRule>,
34    ) -> CSSSupportsRule {
35        let list = supportsrule.rules.clone();
36        CSSSupportsRule {
37            cssconditionrule: CSSConditionRule::new_inherited(parent_stylesheet, list),
38            supportsrule: RefCell::new(supportsrule),
39        }
40    }
41
42    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
43    pub(crate) fn new(
44        window: &Window,
45        parent_stylesheet: &CSSStyleSheet,
46        supportsrule: Arc<SupportsRule>,
47        can_gc: CanGc,
48    ) -> DomRoot<CSSSupportsRule> {
49        reflect_dom_object(
50            Box::new(CSSSupportsRule::new_inherited(
51                parent_stylesheet,
52                supportsrule,
53            )),
54            window,
55            can_gc,
56        )
57    }
58
59    /// <https://drafts.csswg.org/css-conditional-3/#the-csssupportsrule-interface>
60    pub(crate) fn get_condition_text(&self) -> DOMString {
61        self.supportsrule.borrow().condition.to_css_string().into()
62    }
63
64    pub(crate) fn update_rule(
65        &self,
66        supportsrule: Arc<SupportsRule>,
67        guard: &SharedRwLockReadGuard,
68    ) {
69        self.cssconditionrule
70            .update_rules(supportsrule.rules.clone(), guard);
71        *self.supportsrule.borrow_mut() = supportsrule;
72    }
73}
74
75impl SpecificCSSRule for CSSSupportsRule {
76    fn ty(&self) -> CssRuleType {
77        CssRuleType::Supports
78    }
79
80    fn get_css(&self) -> DOMString {
81        let guard = self.cssconditionrule.shared_lock().read();
82        self.supportsrule.borrow().to_css_string(&guard).into()
83    }
84}