Skip to main content

script/dom/css/
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;
6use js::context::{JSContext, NoGC};
7use script_bindings::domstring::TracedDOMString;
8use script_bindings::reflector::Reflector;
9use xml5ever::local_name;
10
11use super::cssstylesheet::CSSStyleSheet;
12use crate::dom::Node;
13use crate::dom::bindings::codegen::Bindings::StyleSheetBinding::StyleSheetMethods;
14use crate::dom::bindings::inheritance::Castable;
15use crate::dom::bindings::root::DomRoot;
16use crate::dom::bindings::str::DOMString;
17use crate::dom::element::Element;
18use crate::dom::medialist::MediaList;
19
20#[dom_struct]
21pub(crate) struct StyleSheet {
22    reflector_: Reflector,
23    type_: TracedDOMString,
24    href: Option<TracedDOMString>,
25}
26
27impl StyleSheet {
28    pub(crate) fn new_inherited(type_: DOMString, href: Option<DOMString>) -> StyleSheet {
29        StyleSheet {
30            reflector_: Reflector::new(),
31            type_: type_.traced(),
32            href: href.map(|domstring| domstring.traced()),
33        }
34    }
35}
36
37impl StyleSheetMethods<crate::DomTypeHolder> for StyleSheet {
38    /// <https://drafts.csswg.org/cssom/#dom-stylesheet-type>
39    fn Type(&self) -> DOMString {
40        self.type_.clone().root()
41    }
42
43    /// <https://drafts.csswg.org/cssom/#dom-stylesheet-href>
44    fn GetHref(&self) -> Option<DOMString> {
45        self.href.clone().map(|domstring| domstring.root())
46    }
47
48    /// <https://drafts.csswg.org/cssom/#dom-stylesheet-ownernode>
49    fn GetOwnerNode(&self) -> Option<DomRoot<Element>> {
50        self.downcast::<CSSStyleSheet>()
51            .and_then(|s| s.owner_node())
52    }
53
54    /// <https://drafts.csswg.org/cssom/#dom-stylesheet-media>
55    fn Media(&self, cx: &mut JSContext) -> DomRoot<MediaList> {
56        self.downcast::<CSSStyleSheet>().unwrap().medialist(cx)
57    }
58
59    /// <https://drafts.csswg.org/cssom/#dom-stylesheet-title>
60    fn GetTitle(&self) -> Option<DOMString> {
61        let css_style_sheet = self.downcast::<CSSStyleSheet>()?;
62        let owner_element = css_style_sheet.owner_node()?;
63        if !owner_element.upcast::<Node>().is_in_a_document_tree() {
64            return None;
65        }
66
67        let title_string = owner_element.get_attribute_string_value(&local_name!("title"))?;
68        if title_string.is_empty() {
69            return None;
70        }
71        Some(title_string.into())
72    }
73
74    /// <https://drafts.csswg.org/cssom/#dom-stylesheet-disabled>
75    fn Disabled(&self) -> bool {
76        self.downcast::<CSSStyleSheet>().unwrap().disabled()
77    }
78
79    /// <https://drafts.csswg.org/cssom/#dom-stylesheet-disabled>
80    fn SetDisabled(&self, no_gc: &NoGC, disabled: bool) {
81        self.downcast::<CSSStyleSheet>()
82            .unwrap()
83            .set_disabled(no_gc, disabled)
84    }
85}