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