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(
44        &self,
45        lock: &SharedRwLock,
46        guard: &SharedRwLockReadGuard,
47    ) -> Self {
48        PositionTryRule {
49            name: self.name.clone(),
50            block: Arc::new(lock.wrap(self.block.read_with(&guard).clone())),
51            source_location: self.source_location.clone(),
52        }
53    }
54}
55
56impl ToCssWithGuard for PositionTryRule {
57    fn to_css(
58        &self,
59        guard: &SharedRwLockReadGuard,
60        dest: &mut CssStringWriter,
61    ) -> std::fmt::Result {
62        dest.write_str("@position-try ")?;
63        self.name.to_css(&mut CssWriter::new(dest))?;
64        dest.write_str(" { ")?;
65        let declarations = self.block.read_with(guard);
66        declarations.to_css(dest)?;
67        if !declarations.is_empty() {
68            dest.write_char(' ')?;
69        }
70        dest.write_char('}')
71    }
72}