style/stylesheets/
style_rule.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 style rule.
6
7use crate::properties::PropertyDeclarationBlock;
8use crate::selector_parser::SelectorImpl;
9use crate::shared_lock::{
10    DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard,
11};
12use crate::str::CssStringWriter;
13use crate::stylesheets::{style_or_page_rule_to_css, CssRules};
14use cssparser::SourceLocation;
15#[cfg(feature = "gecko")]
16use malloc_size_of::{
17    MallocSizeOf, MallocSizeOfOps, MallocUnconditionalShallowSizeOf, MallocUnconditionalSizeOf,
18};
19use selectors::SelectorList;
20use servo_arc::Arc;
21use std::fmt::{self, Write};
22
23/// A style rule, with selectors and declarations.
24#[derive(Debug, ToShmem)]
25pub struct StyleRule {
26    /// The list of selectors in this rule.
27    pub selectors: SelectorList<SelectorImpl>,
28    /// The declaration block with the properties it contains.
29    pub block: Arc<Locked<PropertyDeclarationBlock>>,
30    /// The nested rules to this style rule. Only non-`None` when nesting is enabled.
31    pub rules: Option<Arc<Locked<CssRules>>>,
32    /// The location in the sheet where it was found.
33    pub source_location: SourceLocation,
34}
35
36impl DeepCloneWithLock for StyleRule {
37    /// Deep clones this StyleRule.
38    fn deep_clone_with_lock(
39        &self,
40        lock: &SharedRwLock,
41        guard: &SharedRwLockReadGuard,
42    ) -> StyleRule {
43        StyleRule {
44            selectors: self.selectors.clone(),
45            block: Arc::new(lock.wrap(self.block.read_with(guard).clone())),
46            rules: self.rules.as_ref().map(|rules| {
47                let rules = rules.read_with(guard);
48                Arc::new(lock.wrap(rules.deep_clone_with_lock(lock, guard)))
49            }),
50            source_location: self.source_location.clone(),
51        }
52    }
53}
54
55impl StyleRule {
56    /// Measure heap usage.
57    #[cfg(feature = "gecko")]
58    pub fn size_of(&self, guard: &SharedRwLockReadGuard, ops: &mut MallocSizeOfOps) -> usize {
59        let mut n = 0;
60        n += self.selectors.unconditional_size_of(ops);
61        n += self.block.unconditional_shallow_size_of(ops)
62            + self.block.read_with(guard).size_of(ops);
63        if let Some(ref rules) = self.rules {
64            n += rules.unconditional_shallow_size_of(ops)
65                + rules.read_with(guard).size_of(guard, ops)
66        }
67        n
68    }
69}
70
71impl ToCssWithGuard for StyleRule {
72    /// https://drafts.csswg.org/cssom/#serialize-a-css-rule CSSStyleRule
73    fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut CssStringWriter) -> fmt::Result {
74        use cssparser::ToCss;
75        self.selectors.to_css(dest)?;
76        dest.write_char(' ')?;
77        style_or_page_rule_to_css(self.rules.as_ref(), &self.block, guard, dest)
78    }
79}