script/dom/css/
cssimportrule.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, ToCssWithGuard};
10use style::stylesheets::import_rule::ImportLayer;
11use style::stylesheets::{CssRuleType, ImportRule};
12use style_traits::ToCss;
13
14use super::cssrule::{CSSRule, SpecificCSSRule};
15use super::cssstylesheet::CSSStyleSheet;
16use crate::dom::bindings::codegen::Bindings::CSSImportRuleBinding::CSSImportRuleMethods;
17use crate::dom::bindings::reflector::reflect_dom_object;
18use crate::dom::bindings::root::DomRoot;
19use crate::dom::bindings::str::DOMString;
20use crate::dom::window::Window;
21use crate::script_runtime::CanGc;
22
23#[dom_struct]
24pub(crate) struct CSSImportRule {
25    cssrule: CSSRule,
26    #[ignore_malloc_size_of = "Stylo"]
27    #[no_trace]
28    import_rule: RefCell<Arc<Locked<ImportRule>>>,
29}
30
31impl CSSImportRule {
32    fn new_inherited(
33        parent_stylesheet: &CSSStyleSheet,
34        import_rule: Arc<Locked<ImportRule>>,
35    ) -> Self {
36        CSSImportRule {
37            cssrule: CSSRule::new_inherited(parent_stylesheet),
38            import_rule: RefCell::new(import_rule),
39        }
40    }
41
42    pub(crate) fn new(
43        window: &Window,
44        parent_stylesheet: &CSSStyleSheet,
45        import_rule: Arc<Locked<ImportRule>>,
46        can_gc: CanGc,
47    ) -> DomRoot<Self> {
48        reflect_dom_object(
49            Box::new(Self::new_inherited(parent_stylesheet, import_rule)),
50            window,
51            can_gc,
52        )
53    }
54
55    pub(crate) fn update_rule(&self, import_rule: Arc<Locked<ImportRule>>) {
56        *self.import_rule.borrow_mut() = import_rule;
57    }
58}
59
60impl SpecificCSSRule for CSSImportRule {
61    fn ty(&self) -> CssRuleType {
62        CssRuleType::Import
63    }
64
65    fn get_css(&self) -> DOMString {
66        let guard = self.cssrule.shared_lock().read();
67        self.import_rule
68            .borrow()
69            .read_with(&guard)
70            .to_css_string(&guard)
71            .into()
72    }
73}
74
75impl CSSImportRuleMethods<crate::DomTypeHolder> for CSSImportRule {
76    /// <https://drafts.csswg.org/cssom-1/#dom-cssimportrule-layername>
77    fn GetLayerName(&self) -> Option<DOMString> {
78        let guard = self.cssrule.shared_lock().read();
79        match &self.import_rule.borrow().read_with(&guard).layer {
80            ImportLayer::None => None,
81            ImportLayer::Anonymous => Some(DOMString::new()),
82            ImportLayer::Named(name) => Some(DOMString::from_string(name.to_css_string())),
83        }
84    }
85}