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