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