tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

position_try_rule.rs (2346B)


      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 
      9 use std::fmt::Write;
     10 
     11 use crate::derives::*;
     12 use crate::properties::PropertyDeclarationBlock;
     13 use crate::shared_lock::{
     14    DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard,
     15 };
     16 use crate::values::DashedIdent;
     17 use cssparser::SourceLocation;
     18 #[cfg(feature = "gecko")]
     19 use malloc_size_of::{MallocSizeOf, MallocSizeOfOps, MallocUnconditionalShallowSizeOf};
     20 use servo_arc::Arc;
     21 use style_traits::{CssStringWriter, CssWriter, ToCss};
     22 
     23 /// A position-try rule.
     24 #[derive(Clone, Debug, ToShmem)]
     25 pub 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 
     34 impl 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 
     42 impl 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 
     52 impl 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 }