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