script/dom/
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 crate::dom::bindings::codegen::Bindings::StyleSheetListBinding::StyleSheetListMethods;
10use crate::dom::bindings::reflector::{Reflector, reflect_dom_object};
11use crate::dom::bindings::root::{Dom, DomRoot};
12use crate::dom::cssstylesheet::CSSStyleSheet;
13use crate::dom::document::Document;
14use crate::dom::documentorshadowroot::StylesheetSource;
15use crate::dom::element::Element;
16use crate::dom::shadowroot::ShadowRoot;
17use crate::dom::stylesheet::StyleSheet;
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    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
53    pub(crate) fn append_constructed_stylesheet(&self, cssom_stylesheet: &CSSStyleSheet) {
54        match *self {
55            StyleSheetListOwner::Document(ref doc) => {
56                doc.append_constructed_stylesheet(cssom_stylesheet)
57            },
58            StyleSheetListOwner::ShadowRoot(ref shadow_root) => {
59                shadow_root.append_constructed_stylesheet(cssom_stylesheet)
60            },
61        }
62    }
63
64    #[cfg_attr(crown, allow(crown::unrooted_must_root))] // Owner needs to be rooted already necessarily.
65    pub(crate) fn remove_stylesheet(&self, owner: StylesheetSource, s: &Arc<Stylesheet>) {
66        match *self {
67            StyleSheetListOwner::Document(ref doc) => doc.remove_stylesheet(owner, s),
68            StyleSheetListOwner::ShadowRoot(ref shadow_root) => {
69                shadow_root.remove_stylesheet(owner, s)
70            },
71        }
72    }
73
74    pub(crate) fn invalidate_stylesheets(&self) {
75        match *self {
76            StyleSheetListOwner::Document(ref doc) => doc.invalidate_stylesheets(),
77            StyleSheetListOwner::ShadowRoot(ref shadow_root) => {
78                shadow_root.invalidate_stylesheets()
79            },
80        }
81    }
82}
83
84#[dom_struct]
85pub(crate) struct StyleSheetList {
86    reflector_: Reflector,
87    document_or_shadow_root: StyleSheetListOwner,
88}
89
90impl StyleSheetList {
91    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
92    fn new_inherited(doc_or_sr: StyleSheetListOwner) -> StyleSheetList {
93        StyleSheetList {
94            reflector_: Reflector::new(),
95            document_or_shadow_root: doc_or_sr,
96        }
97    }
98
99    #[cfg_attr(crown, allow(crown::unrooted_must_root))]
100    pub(crate) fn new(
101        window: &Window,
102        doc_or_sr: StyleSheetListOwner,
103        can_gc: CanGc,
104    ) -> DomRoot<StyleSheetList> {
105        reflect_dom_object(
106            Box::new(StyleSheetList::new_inherited(doc_or_sr)),
107            window,
108            can_gc,
109        )
110    }
111}
112
113impl StyleSheetListMethods<crate::DomTypeHolder> for StyleSheetList {
114    // https://drafts.csswg.org/cssom/#dom-stylesheetlist-length
115    fn Length(&self) -> u32 {
116        self.document_or_shadow_root.stylesheet_count() as u32
117    }
118
119    // https://drafts.csswg.org/cssom/#dom-stylesheetlist-item
120    fn Item(&self, index: u32) -> Option<DomRoot<StyleSheet>> {
121        // XXXManishearth this  doesn't handle the origin clean flag and is a
122        // cors vulnerability
123        self.document_or_shadow_root
124            .stylesheet_at(index as usize)
125            .map(DomRoot::upcast)
126    }
127
128    // check-tidy: no specs after this line
129    fn IndexedGetter(&self, index: u32) -> Option<DomRoot<StyleSheet>> {
130        self.Item(index)
131    }
132}