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