Skip to main content

script/dom/css/
cssnesteddeclarations.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::{Locked, SharedRwLockReadGuard, ToCssWithGuard};
12use style::stylesheets::{CssRuleType, NestedDeclarationsRule};
13
14use super::cssrule::{CSSRule, SpecificCSSRule};
15use super::cssstyledeclaration::{CSSModificationAccess, CSSStyleDeclaration, CSSStyleOwner};
16use super::cssstylesheet::CSSStyleSheet;
17use crate::dom::bindings::codegen::Bindings::CSSNestedDeclarationsBinding::CSSNestedDeclarationsMethods;
18use crate::dom::bindings::inheritance::Castable;
19use crate::dom::bindings::reflector::DomGlobal;
20use crate::dom::bindings::root::{Dom, DomRoot, MutNullableDom};
21use crate::dom::bindings::str::DOMString;
22use crate::dom::window::Window;
23use crate::script_runtime::CanGc;
24
25#[dom_struct]
26pub(crate) struct CSSNestedDeclarations {
27    css_rule: CSSRule,
28    #[ignore_malloc_size_of = "Stylo"]
29    #[no_trace]
30    nesteddeclarationsrule: RefCell<Arc<Locked<NestedDeclarationsRule>>>,
31    style_declaration: MutNullableDom<CSSStyleDeclaration>,
32}
33
34impl CSSNestedDeclarations {
35    pub(crate) fn new_inherited(
36        parent_stylesheet: &CSSStyleSheet,
37        nesteddeclarationsrule: Arc<Locked<NestedDeclarationsRule>>,
38    ) -> Self {
39        Self {
40            css_rule: CSSRule::new_inherited(parent_stylesheet),
41            nesteddeclarationsrule: RefCell::new(nesteddeclarationsrule),
42            style_declaration: Default::default(),
43        }
44    }
45
46    pub(crate) fn new(
47        cx: &mut JSContext,
48        window: &Window,
49        parent_stylesheet: &CSSStyleSheet,
50        nesteddeclarationsrule: Arc<Locked<NestedDeclarationsRule>>,
51    ) -> DomRoot<Self> {
52        reflect_dom_object_with_cx(
53            Box::new(Self::new_inherited(
54                parent_stylesheet,
55                nesteddeclarationsrule,
56            )),
57            window,
58            cx,
59        )
60    }
61
62    pub(crate) fn update_rule(
63        &self,
64        nesteddeclarationsrule: Arc<Locked<NestedDeclarationsRule>>,
65        guard: &SharedRwLockReadGuard,
66    ) {
67        if let Some(ref style_decl) = self.style_declaration.get() {
68            style_decl
69                .update_property_declaration_block(&nesteddeclarationsrule.read_with(guard).block);
70        }
71        *self.nesteddeclarationsrule.borrow_mut() = nesteddeclarationsrule;
72    }
73}
74
75impl SpecificCSSRule for CSSNestedDeclarations {
76    fn ty(&self) -> CssRuleType {
77        CssRuleType::NestedDeclarations
78    }
79
80    fn get_css(&self) -> DOMString {
81        let guard = self.css_rule.shared_lock().read();
82        self.nesteddeclarationsrule
83            .borrow()
84            .read_with(&guard)
85            .to_css_string(&guard)
86            .into()
87    }
88}
89
90impl CSSNestedDeclarationsMethods<crate::DomTypeHolder> for CSSNestedDeclarations {
91    /// <https://drafts.csswg.org/css-nesting/#dom-cssnesteddeclarations-style>
92    fn Style(&self, cx: &mut JSContext) -> DomRoot<CSSStyleDeclaration> {
93        self.style_declaration.or_init(|| {
94            let guard = self.css_rule.shared_lock().read();
95            CSSStyleDeclaration::new(
96                self.global().as_window(),
97                CSSStyleOwner::CSSRule(
98                    Dom::from_ref(self.upcast()),
99                    RefCell::new(
100                        self.nesteddeclarationsrule
101                            .borrow()
102                            .read_with(&guard)
103                            .block
104                            .clone(),
105                    ),
106                ),
107                None,
108                CSSModificationAccess::ReadWrite,
109                CanGc::from_cx(cx),
110            )
111        })
112    }
113}