script/dom/css/
csspropertyrule.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::ToCssWithGuard;
10use style::stylesheets::{CssRuleType, PropertyRule};
11use style_traits::ToCss;
12
13use super::cssrule::{CSSRule, SpecificCSSRule};
14use super::cssstylesheet::CSSStyleSheet;
15use crate::dom::bindings::codegen::Bindings::CSSPropertyRuleBinding::CSSPropertyRuleMethods;
16use crate::dom::bindings::reflector::reflect_dom_object;
17use crate::dom::bindings::root::DomRoot;
18use crate::dom::bindings::str::DOMString;
19use crate::dom::window::Window;
20use crate::script_runtime::CanGc;
21
22#[dom_struct]
23pub(crate) struct CSSPropertyRule {
24    cssrule: CSSRule,
25    #[ignore_malloc_size_of = "Stylo"]
26    #[no_trace]
27    property_rule: RefCell<Arc<PropertyRule>>,
28}
29
30impl CSSPropertyRule {
31    fn new_inherited(parent_stylesheet: &CSSStyleSheet, property_rule: Arc<PropertyRule>) -> Self {
32        CSSPropertyRule {
33            cssrule: CSSRule::new_inherited(parent_stylesheet),
34            property_rule: RefCell::new(property_rule),
35        }
36    }
37
38    pub(crate) fn new(
39        window: &Window,
40        parent_stylesheet: &CSSStyleSheet,
41        property_rule: Arc<PropertyRule>,
42        can_gc: CanGc,
43    ) -> DomRoot<Self> {
44        reflect_dom_object(
45            Box::new(Self::new_inherited(parent_stylesheet, property_rule)),
46            window,
47            can_gc,
48        )
49    }
50
51    pub(crate) fn update_rule(&self, property_rule: Arc<PropertyRule>) {
52        *self.property_rule.borrow_mut() = property_rule;
53    }
54}
55
56impl SpecificCSSRule for CSSPropertyRule {
57    fn ty(&self) -> CssRuleType {
58        CssRuleType::Property
59    }
60
61    fn get_css(&self) -> DOMString {
62        let guard = self.cssrule.shared_lock().read();
63        self.property_rule.borrow().to_css_string(&guard).into()
64    }
65}
66
67impl CSSPropertyRuleMethods<crate::DomTypeHolder> for CSSPropertyRule {
68    /// <https://drafts.css-houdini.org/css-properties-values-api/#dom-csspropertyrule-name>
69    fn Name(&self) -> DOMString {
70        format!("--{}", self.property_rule.borrow().name.0).into()
71    }
72
73    /// <https://drafts.css-houdini.org/css-properties-values-api/#dom-csspropertyrule-syntax>
74    fn Syntax(&self) -> DOMString {
75        self.property_rule
76            .borrow()
77            .data
78            .syntax
79            .specified_string()
80            .unwrap_or_else(|| {
81                debug_assert!(false, "PropertyRule exists but missing a syntax string?");
82                "*"
83            })
84            .into()
85    }
86
87    /// <https://drafts.css-houdini.org/css-properties-values-api/#dom-csspropertyrule-initialvalue>
88    fn GetInitialValue(&self) -> Option<DOMString> {
89        self.property_rule
90            .borrow()
91            .data
92            .initial_value
93            .as_ref()
94            .map(|value| value.to_css_string().into())
95    }
96
97    /// <https://drafts.css-houdini.org/css-properties-values-api/#dom-csspropertyrule-inherits>
98    fn Inherits(&self) -> bool {
99        self.property_rule.borrow().inherits()
100    }
101}