/* 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/. */
//! A list of CSS rules.
usecrate::shared_lock::{DeepCloneWithLock, Locked}; usecrate::shared_lock::{SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard}; usecrate::str::CssStringWriter; usecrate::stylesheets::loader::StylesheetLoader; usecrate::stylesheets::rule_parser::InsertRuleContext; usecrate::stylesheets::stylesheet::StylesheetContents; usecrate::stylesheets::{AllowImportRules, CssRule, CssRuleTypes, RulesMutateError}; #[cfg(feature = "gecko")] use malloc_size_of::{MallocShallowSizeOf, MallocSizeOfOps}; use servo_arc::Arc; use std::fmt::{self, Write};
usesuper::CssRuleType;
/// A list of CSS rules. #[derive(Debug, ToShmem)] pubstruct CssRules(pub Vec<CssRule>);
impl CssRules { /// Whether this CSS rules is empty. pubfn is_empty(&self) -> bool { self.0.is_empty()
}
}
impl CssRules { /// Measure heap usage. #[cfg(feature = "gecko")] pubfn size_of(&self, guard: &SharedRwLockReadGuard, ops: &mut MallocSizeOfOps) -> usize { letmut n = self.0.shallow_size_of(ops); for rule inself.0.iter() {
n += rule.size_of(guard, ops);
}
n
}
/// Trivially construct a new set of CSS rules. pubfn new(rules: Vec<CssRule>, shared_lock: &SharedRwLock) -> Arc<Locked<CssRules>> {
Arc::new(shared_lock.wrap(CssRules(rules)))
}
/// Returns whether all the rules in this list are namespace or import /// rules. fn only_ns_or_import(&self) -> bool { self.0.iter().all(|r| match *r {
CssRule::Namespace(..) | CssRule::Import(..) => true,
_ => false,
})
}
/// Serializes this CSSRules to CSS text as a block of rules. /// /// This should be speced into CSSOM spec at some point. See /// <https://github.com/w3c/csswg-drafts/issues/1985> pubfn to_css_block(
&self,
guard: &SharedRwLockReadGuard,
dest: &mut CssStringWriter,
) -> fmt::Result {
dest.write_str(" {")?; self.to_css_block_without_opening(guard, dest)
}
/// As above, but without the opening curly bracket. That's needed for nesting. pubfn to_css_block_without_opening(
&self,
guard: &SharedRwLockReadGuard,
dest: &mut CssStringWriter,
) -> fmt::Result { for rule inself.0.iter() {
dest.write_str("\n ")?;
rule.to_css(guard, dest)?;
}
dest.write_str("\n}")
}
}
/// A trait to implement helpers for `Arc<Locked<CssRules>>`. pubtrait CssRulesHelpers { /// <https://drafts.csswg.org/cssom/#insert-a-css-rule> /// /// Written in this funky way because parsing an @import rule may cause us /// to clone a stylesheet from the same document due to caching in the CSS /// loader. /// /// TODO(emilio): We could also pass the write guard down into the loader /// instead, but that seems overkill. fn insert_rule(
&self,
lock: &SharedRwLock,
rule: &str,
parent_stylesheet_contents: &StylesheetContents,
index: usize,
nested: CssRuleTypes,
parse_relative_rule_type: Option<CssRuleType>,
loader: Option<&dyn StylesheetLoader>,
allow_import_rules: AllowImportRules,
) -> Result<CssRule, RulesMutateError>;
}
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.