script/dom/
stylesheet.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 dom_struct::dom_struct;
6
7use crate::dom::bindings::codegen::Bindings::StyleSheetBinding::StyleSheetMethods;
8use crate::dom::bindings::inheritance::Castable;
9use crate::dom::bindings::reflector::Reflector;
10use crate::dom::bindings::root::DomRoot;
11use crate::dom::bindings::str::DOMString;
12use crate::dom::cssstylesheet::CSSStyleSheet;
13use crate::dom::element::Element;
14use crate::dom::medialist::MediaList;
15use crate::script_runtime::CanGc;
16
17#[dom_struct]
18pub(crate) struct StyleSheet {
19    reflector_: Reflector,
20    type_: DOMString,
21    href: Option<DOMString>,
22    title: Option<DOMString>,
23}
24
25impl StyleSheet {
26    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
27    pub(crate) fn new_inherited(
28        type_: DOMString,
29        href: Option<DOMString>,
30        title: Option<DOMString>,
31    ) -> StyleSheet {
32        StyleSheet {
33            reflector_: Reflector::new(),
34            type_,
35            href,
36            title,
37        }
38    }
39}
40
41impl StyleSheetMethods<crate::DomTypeHolder> for StyleSheet {
42    /// <https://drafts.csswg.org/cssom/#dom-stylesheet-type>
43    fn Type(&self) -> DOMString {
44        self.type_.clone()
45    }
46
47    // https://drafts.csswg.org/cssom/#dom-stylesheet-href
48    fn GetHref(&self) -> Option<DOMString> {
49        self.href.clone()
50    }
51
52    // https://drafts.csswg.org/cssom/#dom-stylesheet-ownernode
53    fn GetOwnerNode(&self) -> Option<DomRoot<Element>> {
54        self.downcast::<CSSStyleSheet>()
55            .and_then(|s| s.owner_node())
56    }
57
58    // https://drafts.csswg.org/cssom/#dom-stylesheet-media
59    fn Media(&self) -> DomRoot<MediaList> {
60        self.downcast::<CSSStyleSheet>()
61            .unwrap()
62            .medialist(CanGc::note())
63    }
64
65    // https://drafts.csswg.org/cssom/#dom-stylesheet-title
66    fn GetTitle(&self) -> Option<DOMString> {
67        self.title.clone()
68    }
69
70    // https://drafts.csswg.org/cssom/#dom-stylesheet-disabled
71    fn Disabled(&self) -> bool {
72        self.downcast::<CSSStyleSheet>().unwrap().disabled()
73    }
74
75    // https://drafts.csswg.org/cssom/#dom-stylesheet-disabled
76    fn SetDisabled(&self, disabled: bool) {
77        self.downcast::<CSSStyleSheet>()
78            .unwrap()
79            .set_disabled(disabled)
80    }
81}