style/
author_styles.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
5//! A set of author stylesheets and their computed representation, such as the
6//! ones used for ShadowRoot.
7
8use crate::dom::TElement;
9use crate::invalidation::media_queries::ToMediaListKey;
10use crate::shared_lock::SharedRwLockReadGuard;
11use crate::stylesheet_set::AuthorStylesheetSet;
12use crate::stylesheets::StylesheetInDocument;
13use crate::stylist::CascadeData;
14use crate::stylist::Stylist;
15use servo_arc::Arc;
16
17/// A set of author stylesheets and their computed representation, such as the
18/// ones used for ShadowRoot.
19#[derive(MallocSizeOf)]
20pub struct GenericAuthorStyles<S>
21where
22    S: StylesheetInDocument + PartialEq + 'static,
23{
24    /// The sheet collection, which holds the sheet pointers, the invalidations,
25    /// and all that stuff.
26    pub stylesheets: AuthorStylesheetSet<S>,
27    /// The actual cascade data computed from the stylesheets.
28    #[ignore_malloc_size_of = "Measured as part of the stylist"]
29    pub data: Arc<CascadeData>,
30}
31
32pub use self::GenericAuthorStyles as AuthorStyles;
33
34lazy_static! {
35    static ref EMPTY_CASCADE_DATA: Arc<CascadeData> = Arc::new_leaked(CascadeData::new());
36}
37
38impl<S> GenericAuthorStyles<S>
39where
40    S: StylesheetInDocument + PartialEq + 'static,
41{
42    /// Create an empty AuthorStyles.
43    #[inline]
44    pub fn new() -> Self {
45        Self {
46            stylesheets: AuthorStylesheetSet::new(),
47            data: EMPTY_CASCADE_DATA.clone(),
48        }
49    }
50
51    /// Flush the pending sheet changes, updating `data` as appropriate.
52    ///
53    /// TODO(emilio): Need a host element and a snapshot map to do invalidation
54    /// properly.
55    #[inline]
56    pub fn flush<E>(&mut self, stylist: &mut Stylist, guard: &SharedRwLockReadGuard)
57    where
58        E: TElement,
59        S: ToMediaListKey,
60    {
61        let flusher = self
62            .stylesheets
63            .flush::<E>(/* host = */ None, /* snapshot_map = */ None);
64
65        let result = stylist.rebuild_author_data(&self.data, flusher.sheets, guard);
66        if let Ok(Some(new_data)) = result {
67            self.data = new_data;
68        }
69    }
70}