style/stylesheets/
position_try_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 [`@position-try`][position-try] rule for Anchor Positioning.
6//!
7//! [position-try]: https://drafts.csswg.org/css-anchor-position-1/#fallback-rule
8
9use std::fmt::Write;
10
11use crate::properties::PropertyDeclarationBlock;
12use crate::shared_lock::{
13    DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard,
14};
15use crate::values::DashedIdent;
16use cssparser::SourceLocation;
17#[cfg(feature = "gecko")]
18use malloc_size_of::{MallocSizeOf, MallocSizeOfOps, MallocUnconditionalShallowSizeOf};
19use servo_arc::Arc;
20use style_traits::{CssStringWriter, CssWriter, ToCss};
21
22/// A position-try rule.
23#[derive(Clone, Debug, ToShmem)]
24pub struct PositionTryRule {
25    /// Name of this position-try rule.
26    pub name: DashedIdent,
27    /// The declaration block this position-try rule contains.
28    pub block: Arc<Locked<PropertyDeclarationBlock>>,
29    /// The source position this rule was found at.
30    pub source_location: SourceLocation,
31}
32
33impl PositionTryRule {
34    /// Measure heap usage.
35    #[cfg(feature = "gecko")]
36    pub fn size_of(&self, guard: &SharedRwLockReadGuard, ops: &mut MallocSizeOfOps) -> usize {
37        self.block.unconditional_shallow_size_of(ops) + self.block.read_with(guard).size_of(ops)
38    }
39}
40
41impl DeepCloneWithLock for PositionTryRule {
42    fn deep_clone_with_lock(&self, lock: &SharedRwLock, guard: &SharedRwLockReadGuard) -> Self {
43        PositionTryRule {
44            name: self.name.clone(),
45            block: Arc::new(lock.wrap(self.block.read_with(&guard).clone())),
46            source_location: self.source_location.clone(),
47        }
48    }
49}
50
51impl ToCssWithGuard for PositionTryRule {
52    fn to_css(
53        &self,
54        guard: &SharedRwLockReadGuard,
55        dest: &mut CssStringWriter,
56    ) -> std::fmt::Result {
57        dest.write_str("@position-try ")?;
58        self.name.to_css(&mut CssWriter::new(dest))?;
59        dest.write_str(" { ")?;
60        let declarations = self.block.read_with(guard);
61        declarations.to_css(dest)?;
62        if !declarations.is_empty() {
63            dest.write_char(' ')?;
64        }
65        dest.write_char('}')
66    }
67}