script/dom/css/
stylesheetlist.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 servo_arc::Arc;
7use style::stylesheets::Stylesheet;
8
9use super::cssstylesheet::CSSStyleSheet;
10use super::stylesheet::StyleSheet;
11use crate::dom::bindings::codegen::Bindings::StyleSheetListBinding::StyleSheetListMethods;
12use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
13use crate::dom::bindings::root::{Dom, DomRoot};
14use crate::dom::document::Document;
15use crate::dom::documentorshadowroot::StylesheetSource;
16use crate::dom::element::Element;
17use crate::dom::shadowroot::ShadowRoot;
18use crate::dom::window::Window;
19use crate::script_runtime::CanGc;
20
21#[cfg_attr(crown, crown::unrooted_must_root_lint::must_root)]
22#[derive(Clone, JSTraceable, MallocSizeOf, PartialEq)]
23pub(crate) enum StyleSheetListOwner {
24    Document(Dom<Document>),
25    ShadowRoot(Dom<ShadowRoot>),
26}
27
28impl StyleSheetListOwner {
29    pub(crate) fn stylesheet_count(&self) -> usize {
30        match *self {
31            StyleSheetListOwner::Document(ref doc) => doc.stylesheet_count(),
32            StyleSheetListOwner::ShadowRoot(ref shadow_root) => shadow_root.stylesheet_count(),
33        }
34    }
35
36    pub(crate) fn stylesheet_at(&self, index: usize) -> Option<DomRoot<CSSStyleSheet>> {
37        match *self {
38            StyleSheetListOwner::Document(ref doc) => doc.stylesheet_at(index),
39            StyleSheetListOwner::ShadowRoot(ref shadow_root) => shadow_root.stylesheet_at(index),
40        }
41    }
42
43    pub(crate) fn add_owned_stylesheet(&self, owner_node: &Element, sheet: Arc<Stylesheet>) {
44        match *self {
45            StyleSheetListOwner::Document(ref doc) => doc.add_owned_stylesheet(owner_node, sheet),
46            StyleSheetListOwner::ShadowRoot(ref shadow_root) => {
47                shadow_root.add_owned_stylesheet(owner_node, sheet)
48            },
49        }
50    }
51
52    pub(crate) fn append_constructed_stylesheet(&self, cssom_stylesheet: &CSSStyleSheet) {
53        match *self {
54            StyleSheetListOwner::Document(ref doc) => {
55                doc.append_constructed_stylesheet(cssom_stylesheet)
56            },
57            StyleSheetListOwner::ShadowRoot(ref shadow_root) => {
58                shadow_root.append_constructed_stylesheet(cssom_stylesheet)
59            },
60        }
61    }
62
63    #[cfg_attr(crown, expect(crown::unrooted_must_root))] // Owner needs to be rooted already necessarily.
64    pub(crate) fn remove_stylesheet(&self, owner: StylesheetSource, s: &Arc<Stylesheet>) {
65        match *self {
66            StyleSheetListOwner::Document(ref doc) => doc.remove_stylesheet(owner, s),
67            StyleSheetListOwner::ShadowRoot(ref shadow_root) => {
68                shadow_root.remove_stylesheet(owner, s)
69            },
70        }
71    }
72
73    pub(crate) fn invalidate_stylesheets(&self) {
74        match *self {
75            StyleSheetListOwner::Document(ref doc) => doc.invalidate_stylesheets(),
76            StyleSheetListOwner::ShadowRoot(ref shadow_root) => {
77                shadow_root.invalidate_stylesheets()
78            },
79        }
80    }
81}
82
83#[dom_struct]
84pub(crate) struct StyleSheetList {
85    reflector_: Reflector,
86    document_or_shadow_root: StyleSheetListOwner,
87}
88
89impl StyleSheetList {
90    #[cfg_attr(crown, expect(crown::unrooted_must_root))]
91    fn new_inherited(doc_or_sr: StyleSheetListOwner) -> StyleSheetList {
92        StyleSheetList {
93            reflector_: Reflector::new(),
94            document_or_shadow_root: doc_or_sr,
95        }
96    }
97
98    #[cfg_attr(crown, expect(crown::unrooted_must_root))]
99    pub(crate) fn new(
100        window: &Window,
101        doc_or_sr: StyleSheetListOwner,
102        can_gc: CanGc,
103    ) -> DomRoot<StyleSheetList> {
104        reflect_dom_object(
105            Box::new(StyleSheetList::new_inherited(doc_or_sr)),
106            window,
107            can_gc,
108        )
109    }
110}
111
112impl StyleSheetListMethods<crate::DomTypeHolder> for StyleSheetList {
113    /// <https://drafts.csswg.org/cssom/#dom-stylesheetlist-length>
114    fn Length(&self) -> u32 {
115        self.document_or_shadow_root.stylesheet_count() as u32
116    }
117
118    /// <https://drafts.csswg.org/cssom/#dom-stylesheetlist-item>
119    fn Item(&self, index: u32) -> Option<DomRoot<StyleSheet>> {
120        // XXXManishearth this  doesn't handle the origin clean flag and is a
121        // cors vulnerability
122        self.document_or_shadow_root
123            .stylesheet_at(index as usize)
124            .map(DomRoot::upcast)
125    }
126
127    // check-tidy: no specs after this line
128    fn IndexedGetter(&self, index: u32) -> Option<DomRoot<StyleSheet>> {
129        self.Item(index)
130    }
131}