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