Skip to main content

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