style/stylesheets/
nested_declarations_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 nested declarations rule.
6//! https://drafts.csswg.org/css-nesting-1/#nested-declarations-rule
7
8use crate::properties::PropertyDeclarationBlock;
9use crate::shared_lock::{
10    DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard,
11};
12use crate::str::CssStringWriter;
13use cssparser::SourceLocation;
14use malloc_size_of::{MallocSizeOf, MallocSizeOfOps, MallocUnconditionalShallowSizeOf};
15use servo_arc::Arc;
16
17/// A nested declarations rule.
18#[derive(Clone, Debug, ToShmem)]
19pub struct NestedDeclarationsRule {
20    /// The declarations.
21    pub block: Arc<Locked<PropertyDeclarationBlock>>,
22    /// The source position this rule was found at.
23    pub source_location: SourceLocation,
24}
25
26impl NestedDeclarationsRule {
27    /// Measure heap usage.
28    pub fn size_of(&self, guard: &SharedRwLockReadGuard, ops: &mut MallocSizeOfOps) -> usize {
29        self.block.unconditional_shallow_size_of(ops) + self.block.read_with(guard).size_of(ops)
30    }
31}
32
33impl DeepCloneWithLock for NestedDeclarationsRule {
34    fn deep_clone_with_lock(&self, lock: &SharedRwLock, guard: &SharedRwLockReadGuard) -> Self {
35        Self {
36            block: Arc::new(lock.wrap(self.block.read_with(&guard).clone())),
37            source_location: self.source_location.clone(),
38        }
39    }
40}
41
42impl ToCssWithGuard for NestedDeclarationsRule {
43    fn to_css(
44        &self,
45        guard: &SharedRwLockReadGuard,
46        dest: &mut CssStringWriter,
47    ) -> std::fmt::Result {
48        self.block.read_with(guard).to_css(dest)
49    }
50}