script/
stylesheet_set.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 style::media_queries::Device;
6use style::shared_lock::SharedRwLockReadGuard;
7use style::stylesheet_set::{AuthorStylesheetSet, DocumentStylesheetSet};
8use style::stylesheets::{CustomMediaMap, StylesheetInDocument};
9
10/// Functionality common to DocumentStylesheetSet and AuthorStylesheetSet.
11pub(crate) enum StylesheetSetRef<'a, S>
12where
13    S: StylesheetInDocument + PartialEq + 'static,
14{
15    /// Author stylesheet set.
16    Author(&'a mut AuthorStylesheetSet<S>),
17    /// Document stylesheet set.
18    Document(&'a mut DocumentStylesheetSet<S>),
19}
20
21impl<S> StylesheetSetRef<'_, S>
22where
23    S: StylesheetInDocument + PartialEq + 'static,
24{
25    /// Appends a new stylesheet to the current set.
26    ///
27    /// No device implies not computing invalidations.
28    pub(crate) fn append_stylesheet(
29        &mut self,
30        device: Option<&Device>,
31        sheet: S,
32        guard: &SharedRwLockReadGuard,
33    ) {
34        let custom_media = &CustomMediaMap::default();
35        match self {
36            StylesheetSetRef::Author(set) => {
37                set.append_stylesheet(device, custom_media, sheet, guard)
38            },
39            StylesheetSetRef::Document(set) => {
40                set.append_stylesheet(device, custom_media, sheet, guard)
41            },
42        }
43    }
44
45    /// Insert a given stylesheet before another stylesheet in the document.
46    pub(crate) fn insert_stylesheet_before(
47        &mut self,
48        device: Option<&Device>,
49        sheet: S,
50        before_sheet: S,
51        guard: &SharedRwLockReadGuard,
52    ) {
53        let custom_media = &CustomMediaMap::default();
54        match self {
55            StylesheetSetRef::Author(set) => {
56                set.insert_stylesheet_before(device, custom_media, sheet, before_sheet, guard)
57            },
58            StylesheetSetRef::Document(set) => {
59                set.insert_stylesheet_before(device, custom_media, sheet, before_sheet, guard)
60            },
61        }
62    }
63
64    /// Remove a given stylesheet from the set.
65    pub(crate) fn remove_stylesheet(
66        &mut self,
67        device: Option<&Device>,
68        sheet: S,
69        guard: &SharedRwLockReadGuard,
70    ) {
71        let custom_media = &CustomMediaMap::default();
72        match self {
73            StylesheetSetRef::Author(set) => {
74                set.remove_stylesheet(device, custom_media, sheet, guard)
75            },
76            StylesheetSetRef::Document(set) => {
77                set.remove_stylesheet(device, custom_media, sheet, guard)
78            },
79        }
80    }
81}