Skip to main content

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::derives::*;
9use crate::invalidation::stylesheets::StylesheetInvalidationSet;
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;
16use std::sync::LazyLock;
17
18/// A set of author stylesheets and their computed representation, such as the
19/// ones used for ShadowRoot.
20#[derive(MallocSizeOf)]
21pub struct GenericAuthorStyles<S>
22where
23    S: StylesheetInDocument + PartialEq + 'static,
24{
25    /// The sheet collection, which holds the sheet pointers, the invalidations,
26    /// and all that stuff.
27    pub stylesheets: AuthorStylesheetSet<S>,
28    /// The actual cascade data computed from the stylesheets.
29    #[ignore_malloc_size_of = "Measured as part of the stylist"]
30    pub data: Arc<CascadeData>,
31}
32
33pub use self::GenericAuthorStyles as AuthorStyles;
34
35static EMPTY_CASCADE_DATA: LazyLock<Arc<CascadeData>> =
36    LazyLock::new(|| Arc::new_leaked(CascadeData::new()));
37
38impl<S> Default for GenericAuthorStyles<S>
39where
40    S: StylesheetInDocument + PartialEq + 'static,
41{
42    fn default() -> Self {
43        Self::new()
44    }
45}
46
47impl<S> GenericAuthorStyles<S>
48where
49    S: StylesheetInDocument + PartialEq + 'static,
50{
51    /// Create an empty AuthorStyles.
52    #[inline]
53    pub fn new() -> Self {
54        Self {
55            stylesheets: AuthorStylesheetSet::new(),
56            data: EMPTY_CASCADE_DATA.clone(),
57        }
58    }
59
60    /// Flush the pending sheet changes, updating `data` as appropriate.
61    #[inline]
62    pub fn flush(
63        &mut self,
64        stylist: &mut Stylist,
65        guard: &SharedRwLockReadGuard,
66    ) -> StylesheetInvalidationSet {
67        let (flusher, mut invalidations) = self.stylesheets.flush();
68        let result = stylist.rebuild_author_data(
69            &self.data,
70            flusher.sheets,
71            guard,
72            &mut invalidations.cascade_data_difference,
73        );
74        if let Ok(Some(new_data)) = result {
75            self.data = new_data;
76        }
77        invalidations
78    }
79}