script/dom/
cssimportrule.rs1use 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 crate::dom::bindings::codegen::Bindings::CSSImportRuleBinding::CSSImportRuleMethods;
15use crate::dom::bindings::reflector::reflect_dom_object;
16use crate::dom::bindings::root::DomRoot;
17use crate::dom::bindings::str::DOMString;
18use crate::dom::cssrule::{CSSRule, SpecificCSSRule};
19use crate::dom::cssstylesheet::CSSStyleSheet;
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 = "Arc"]
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 #[cfg_attr(crown, allow(crown::unrooted_must_root))]
43 pub(crate) fn new(
44 window: &Window,
45 parent_stylesheet: &CSSStyleSheet,
46 import_rule: Arc<Locked<ImportRule>>,
47 can_gc: CanGc,
48 ) -> DomRoot<Self> {
49 reflect_dom_object(
50 Box::new(Self::new_inherited(parent_stylesheet, import_rule)),
51 window,
52 can_gc,
53 )
54 }
55
56 pub(crate) fn update_rule(&self, import_rule: Arc<Locked<ImportRule>>) {
57 *self.import_rule.borrow_mut() = import_rule;
58 }
59}
60
61impl SpecificCSSRule for CSSImportRule {
62 fn ty(&self) -> CssRuleType {
63 CssRuleType::Import
64 }
65
66 fn get_css(&self) -> DOMString {
67 let guard = self.cssrule.shared_lock().read();
68 self.import_rule
69 .borrow()
70 .read_with(&guard)
71 .to_css_string(&guard)
72 .into()
73 }
74}
75
76impl CSSImportRuleMethods<crate::DomTypeHolder> for CSSImportRule {
77 fn GetLayerName(&self) -> Option<DOMString> {
79 let guard = self.cssrule.shared_lock().read();
80 match &self.import_rule.borrow().read_with(&guard).layer {
81 ImportLayer::None => None,
82 ImportLayer::Anonymous => Some(DOMString::new()),
83 ImportLayer::Named(name) => Some(DOMString::from_string(name.to_css_string())),
84 }
85 }
86}