Skip to main content

script/dom/css/
cssmediarule.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::{SharedRwLockReadGuard, ToCssWithGuard};
12use style::stylesheets::{CssRuleType, MediaRule};
13use style_traits::ToCss;
14
15use super::cssconditionrule::CSSConditionRule;
16use super::cssrule::SpecificCSSRule;
17use super::cssstylesheet::CSSStyleSheet;
18use crate::dom::bindings::codegen::Bindings::CSSMediaRuleBinding::CSSMediaRuleMethods;
19use crate::dom::bindings::reflector::DomGlobal;
20use crate::dom::bindings::root::{DomRoot, MutNullableDom};
21use crate::dom::bindings::str::DOMString;
22use crate::dom::css::cssgroupingrule::CSSGroupingRule;
23use crate::dom::medialist::MediaList;
24use crate::dom::window::Window;
25
26#[dom_struct]
27pub(crate) struct CSSMediaRule {
28    css_condition_rule: CSSConditionRule,
29    #[ignore_malloc_size_of = "Stylo"]
30    #[no_trace]
31    media_rule: RefCell<Arc<MediaRule>>,
32    media_list: MutNullableDom<MediaList>,
33}
34
35impl CSSMediaRule {
36    fn new_inherited(
37        parent_rule: Option<&CSSGroupingRule>,
38        parent_stylesheet: &CSSStyleSheet,
39        mediarule: Arc<MediaRule>,
40    ) -> CSSMediaRule {
41        let list = mediarule.rules.clone();
42        CSSMediaRule {
43            css_condition_rule: CSSConditionRule::new_inherited(
44                parent_rule,
45                parent_stylesheet,
46                list,
47            ),
48            media_rule: RefCell::new(mediarule),
49            media_list: MutNullableDom::new(None),
50        }
51    }
52
53    pub(crate) fn new(
54        cx: &mut JSContext,
55        window: &Window,
56        parent_rule: Option<&CSSGroupingRule>,
57        parent_stylesheet: &CSSStyleSheet,
58        mediarule: Arc<MediaRule>,
59    ) -> DomRoot<CSSMediaRule> {
60        reflect_dom_object_with_cx(
61            Box::new(CSSMediaRule::new_inherited(
62                parent_rule,
63                parent_stylesheet,
64                mediarule,
65            )),
66            window,
67            cx,
68        )
69    }
70
71    fn medialist(&self, cx: &mut JSContext) -> DomRoot<MediaList> {
72        self.media_list.or_init(|| {
73            MediaList::new(
74                cx,
75                self.global().as_window(),
76                self.css_condition_rule.parent_stylesheet(),
77                self.media_rule.borrow().media_queries.clone(),
78            )
79        })
80    }
81
82    /// <https://drafts.csswg.org/css-conditional-3/#the-cssmediarule-interface>
83    pub(crate) fn get_condition_text(&self) -> DOMString {
84        let guard = self.css_condition_rule.shared_lock().read();
85        self.media_rule
86            .borrow()
87            .media_queries
88            .read_with(&guard)
89            .to_css_string()
90            .into()
91    }
92
93    pub(crate) fn update_rule(&self, mediarule: Arc<MediaRule>, guard: &SharedRwLockReadGuard) {
94        self.css_condition_rule
95            .update_rules(mediarule.rules.clone(), guard);
96        if let Some(medialist) = self.media_list.get() {
97            medialist.update_media_list(mediarule.media_queries.clone());
98        }
99        *self.media_rule.borrow_mut() = mediarule;
100    }
101}
102
103impl SpecificCSSRule for CSSMediaRule {
104    fn ty(&self) -> CssRuleType {
105        CssRuleType::Media
106    }
107
108    fn get_css(&self) -> DOMString {
109        let guard = self.css_condition_rule.shared_lock().read();
110        self.media_rule.borrow().to_css_string(&guard).into()
111    }
112}
113
114impl CSSMediaRuleMethods<crate::DomTypeHolder> for CSSMediaRule {
115    /// <https://drafts.csswg.org/cssom/#dom-cssgroupingrule-media>
116    fn Media(&self, cx: &mut JSContext) -> DomRoot<MediaList> {
117        self.medialist(cx)
118    }
119}