/* This Source Code Form is subject to the terms of the Mozilla Public *License,v.2.0.IfacopyoftheMPLwasnotdistributedwiththis
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
usecrate::derives::*; usecrate::media_queries::MediaList; usecrate::selector_map::{PrecomputedHashMap, PrecomputedHashSet}; usecrate::shared_lock::{DeepCloneWithLock, Locked}; usecrate::shared_lock::{SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard}; usecrate::stylesheets::CssRules; usecrate::values::{computed, DashedIdent}; usecrate::Atom; use cssparser::Parser; use cssparser::SourceLocation; #[cfg(feature = "gecko")] use malloc_size_of::{MallocSizeOfOps, MallocUnconditionalShallowSizeOf}; use selectors::kleene_value::KleeneValue; use servo_arc::Arc; use std::fmt::{self, Write}; use style_traits::{CssStringWriter, CssWriter, ParseError, ToCss};
/// An [`@media`][media] rule. /// /// [media]: https://drafts.csswg.org/css-conditional/#at-ruledef-media #[derive(Debug, ToShmem)] pubstruct MediaRule { /// The list of media queries used by this media rule. pub media_queries: Arc<Locked<MediaList>>, /// The nested rules to this media rule. pub rules: Arc<Locked<CssRules>>, /// The source position where this media rule was found. pub source_location: SourceLocation,
}
impl MediaRule { /// Measure heap usage. #[cfg(feature = "gecko")] pubfn size_of(&self, guard: &SharedRwLockReadGuard, ops: &mut MallocSizeOfOps) -> usize { // Measurement of other fields may be added later. self.rules.unconditional_shallow_size_of(ops)
+ self.rules.read_with(guard).size_of(guard, ops)
}
}
impl ToCssWithGuard for MediaRule { // Serialization of MediaRule is not specced. // https://drafts.csswg.org/cssom/#serialize-a-css-rule CSSMediaRule fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut CssStringWriter) -> fmt::Result {
dest.write_str("@media ")?; self.media_queries
.read_with(guard)
.to_css(&mut CssWriter::new(dest))?; self.rules.read_with(guard).to_css_block(guard, dest)
}
}
/// A `@custom-media` rule. /// https://drafts.csswg.org/mediaqueries-5/#custom-mq #[derive(Debug, ToShmem)] pubstruct CustomMediaRule { /// The name of the custom media rule. pub name: DashedIdent, /// The list of media conditions used by this media rule. pub condition: CustomMediaCondition, /// The source position where this media rule was found. pub source_location: SourceLocation,
}
impl ToCssWithGuard for CustomMediaRule { // Serialization of MediaRule is not specced. // https://drafts.csswg.org/cssom/#serialize-a-css-rule CSSMediaRule fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut CssStringWriter) -> fmt::Result {
dest.write_str("@custom-media ")?; self.name.to_css(&mut CssWriter::new(dest))?;
dest.write_char(' ')?; matchself.condition {
CustomMediaCondition::True => dest.write_str("true"),
CustomMediaCondition::False => dest.write_str("false"),
CustomMediaCondition::MediaList(ref m) => {
m.read_with(guard).to_css(&mut CssWriter::new(dest))
},
}
}
}
/// The currently effective @custom-media conditions. pubtype CustomMediaMap = PrecomputedHashMap<Atom, CustomMediaCondition>;
/// A struct that can evaluate custom media conditions. pubstruct CustomMediaEvaluator<'a> {
map: Option<(&'a CustomMediaMap, &'a SharedRwLockReadGuard<'a>)>, /// Set of media queries we're currently evaluating, needed for cycle detection.
currently_evaluating: PrecomputedHashSet<Atom>,
}
impl<'a> CustomMediaEvaluator<'a> { /// Construct a new custom media evaluator with the given map and guard. pubfn new(map: &'a CustomMediaMap, guard: &'a SharedRwLockReadGuard<'a>) -> Self { Self {
map: Some((map, guard)),
currently_evaluating: Default::default(),
}
}
/// Evaluates a custom media query. pubfn matches(&mutself, ident: &DashedIdent, context: &computed::Context) -> KleeneValue { let Some((map, guard)) = self.map else { return KleeneValue::Unknown;
}; let Some(condition) = map.get(&ident.0) else { return KleeneValue::Unknown;
}; let media = match condition {
CustomMediaCondition::True => return KleeneValue::True,
CustomMediaCondition::False => return KleeneValue::False,
CustomMediaCondition::MediaList(ref m) => m,
}; if !self.currently_evaluating.insert(ident.0.clone()) { // Found a cycle while evaluating this rule. return KleeneValue::False;
} let result = media.read_with(guard).matches(context, self); self.currently_evaluating.remove(&ident.0);
result.into()
}
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.17 Sekunden
(vorverarbeitet am 2026-08-25)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.