Skip to main content

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