script/dom/css/
cssnamespacerule.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, NamespaceRule};
11
12use super::cssrule::{CSSRule, SpecificCSSRule};
13use super::cssstylesheet::CSSStyleSheet;
14use crate::dom::bindings::codegen::Bindings::CSSNamespaceRuleBinding::CSSNamespaceRuleMethods;
15use crate::dom::bindings::reflector::reflect_dom_object;
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::bindings::str::DOMString;
18use crate::dom::window::Window;
19use crate::script_runtime::CanGc;
20
21#[dom_struct]
22pub(crate) struct CSSNamespaceRule {
23    cssrule: CSSRule,
24    #[ignore_malloc_size_of = "Stylo"]
25    #[no_trace]
26    namespacerule: RefCell<Arc<NamespaceRule>>,
27}
28
29impl CSSNamespaceRule {
30    fn new_inherited(
31        parent_stylesheet: &CSSStyleSheet,
32        namespacerule: Arc<NamespaceRule>,
33    ) -> CSSNamespaceRule {
34        CSSNamespaceRule {
35            cssrule: CSSRule::new_inherited(parent_stylesheet),
36            namespacerule: RefCell::new(namespacerule),
37        }
38    }
39
40    pub(crate) fn new(
41        window: &Window,
42        parent_stylesheet: &CSSStyleSheet,
43        namespacerule: Arc<NamespaceRule>,
44        can_gc: CanGc,
45    ) -> DomRoot<CSSNamespaceRule> {
46        reflect_dom_object(
47            Box::new(CSSNamespaceRule::new_inherited(
48                parent_stylesheet,
49                namespacerule,
50            )),
51            window,
52            can_gc,
53        )
54    }
55
56    pub(crate) fn update_rule(&self, namespacerule: Arc<NamespaceRule>) {
57        *self.namespacerule.borrow_mut() = namespacerule;
58    }
59}
60
61impl CSSNamespaceRuleMethods<crate::DomTypeHolder> for CSSNamespaceRule {
62    /// <https://drafts.csswg.org/cssom/#dom-cssnamespacerule-prefix>
63    fn Prefix(&self) -> DOMString {
64        self.namespacerule
65            .borrow()
66            .prefix
67            .as_ref()
68            .map(|s| s.to_string().into())
69            .unwrap_or_default()
70    }
71
72    /// <https://drafts.csswg.org/cssom/#dom-cssnamespacerule-namespaceuri>
73    fn NamespaceURI(&self) -> DOMString {
74        (**self.namespacerule.borrow().url).into()
75    }
76}
77
78impl SpecificCSSRule for CSSNamespaceRule {
79    fn ty(&self) -> CssRuleType {
80        CssRuleType::Namespace
81    }
82
83    fn get_css(&self) -> DOMString {
84        let guard = self.cssrule.shared_lock().read();
85        self.namespacerule.borrow().to_css_string(&guard).into()
86    }
87}