Skip to main content

script/css/
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::device::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(&mut self, sheet: S, guard: &SharedRwLockReadGuard) {
29        match self {
30            StylesheetSetRef::Author(set) => set.append_stylesheet(sheet, guard),
31            StylesheetSetRef::Document(set) => set.append_stylesheet(sheet, guard),
32        }
33    }
34
35    /// Insert a given stylesheet before another stylesheet in the document.
36    pub(crate) fn insert_stylesheet_before(
37        &mut self,
38        sheet: S,
39        before_sheet: S,
40        guard: &SharedRwLockReadGuard,
41    ) {
42        match self {
43            StylesheetSetRef::Author(set) => {
44                set.insert_stylesheet_before(sheet, before_sheet, guard)
45            },
46            StylesheetSetRef::Document(set) => {
47                set.insert_stylesheet_before(sheet, before_sheet, guard)
48            },
49        }
50    }
51
52    /// Remove a given stylesheet from the set.
53    pub(crate) fn remove_stylesheet(
54        &mut self,
55        device: Option<&Device>,
56        sheet: S,
57        guard: &SharedRwLockReadGuard,
58    ) {
59        let custom_media = &CustomMediaMap::default();
60        match self {
61            StylesheetSetRef::Author(set) => {
62                set.remove_stylesheet(device, custom_media, sheet, guard)
63            },
64            StylesheetSetRef::Document(set) => {
65                set.remove_stylesheet(device, custom_media, sheet, guard)
66            },
67        }
68    }
69}