script/dom/
csslayerblockrule.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, CssRules, LayerBlockRule};
11use style_traits::ToCss;
12
13use crate::dom::bindings::codegen::Bindings::CSSLayerBlockRuleBinding::CSSLayerBlockRuleMethods;
14use crate::dom::bindings::reflector::reflect_dom_object;
15use crate::dom::bindings::root::DomRoot;
16use crate::dom::bindings::str::DOMString;
17use crate::dom::cssgroupingrule::CSSGroupingRule;
18use crate::dom::cssrule::SpecificCSSRule;
19use crate::dom::cssstylesheet::CSSStyleSheet;
20use crate::dom::window::Window;
21use crate::script_runtime::CanGc;
22
23#[dom_struct]
24pub(crate) struct CSSLayerBlockRule {
25    cssgroupingrule: CSSGroupingRule,
26    #[ignore_malloc_size_of = "Arc"]
27    #[no_trace]
28    layerblockrule: RefCell<Arc<LayerBlockRule>>,
29}
30
31impl CSSLayerBlockRule {
32    pub(crate) fn new_inherited(
33        parent_stylesheet: &CSSStyleSheet,
34        layerblockrule: Arc<LayerBlockRule>,
35    ) -> CSSLayerBlockRule {
36        CSSLayerBlockRule {
37            cssgroupingrule: CSSGroupingRule::new_inherited(parent_stylesheet),
38            layerblockrule: RefCell::new(layerblockrule),
39        }
40    }
41
42    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
43    pub(crate) fn new(
44        window: &Window,
45        parent_stylesheet: &CSSStyleSheet,
46        layerblockrule: Arc<LayerBlockRule>,
47        can_gc: CanGc,
48    ) -> DomRoot<CSSLayerBlockRule> {
49        reflect_dom_object(
50            Box::new(CSSLayerBlockRule::new_inherited(
51                parent_stylesheet,
52                layerblockrule,
53            )),
54            window,
55            can_gc,
56        )
57    }
58
59    pub(crate) fn clone_rules(&self) -> Arc<Locked<CssRules>> {
60        self.layerblockrule.borrow().rules.clone()
61    }
62
63    pub(crate) fn update_rule(
64        &self,
65        layerblockrule: Arc<LayerBlockRule>,
66        guard: &SharedRwLockReadGuard,
67    ) {
68        self.cssgroupingrule
69            .update_rules(&layerblockrule.rules, guard);
70        *self.layerblockrule.borrow_mut() = layerblockrule;
71    }
72}
73
74impl SpecificCSSRule for CSSLayerBlockRule {
75    fn ty(&self) -> CssRuleType {
76        CssRuleType::LayerBlock
77    }
78
79    fn get_css(&self) -> DOMString {
80        let guard = self.cssgroupingrule.shared_lock().read();
81        self.layerblockrule.borrow().to_css_string(&guard).into()
82    }
83}
84
85impl CSSLayerBlockRuleMethods<crate::DomTypeHolder> for CSSLayerBlockRule {
86    /// <https://drafts.csswg.org/css-cascade-5/#dom-csslayerblockrule-name>
87    fn Name(&self) -> DOMString {
88        if let Some(name) = &self.layerblockrule.borrow().name {
89            DOMString::from_string(name.to_css_string())
90        } else {
91            DOMString::new()
92        }
93    }
94}