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