mirror of
https://github.com/servo/servo.git
synced 2025-08-03 12:40:06 +01:00
style: [css-nesting] Add storage for nested rules
Allow to have a CssRules list inside a StyleRule. This only introduces the storage and serialization code for them, but we still don't parse it. Differential Revision: https://phabricator.services.mozilla.com/D176550
This commit is contained in:
parent
1182296c4b
commit
d4e12b9db6
4 changed files with 54 additions and 12 deletions
|
@ -25,6 +25,16 @@ use std::fmt;
|
||||||
#[derive(Eq, PartialEq)]
|
#[derive(Eq, PartialEq)]
|
||||||
pub struct GeckoStyleSheet(*const DomStyleSheet);
|
pub struct GeckoStyleSheet(*const DomStyleSheet);
|
||||||
|
|
||||||
|
// NOTE(emilio): These are kind of a lie. We allow to make these Send + Sync so that other data
|
||||||
|
// structures can also be Send and Sync, but Gecko's stylesheets are main-thread-reference-counted.
|
||||||
|
//
|
||||||
|
// We assert that we reference-count in the right thread (in the Addref/Release implementations).
|
||||||
|
// Sending these to a different thread can't really happen (it could theoretically really happen if
|
||||||
|
// we allowed @import rules inside a nested style rule, but that can't happen per spec and would be
|
||||||
|
// a parser bug, caught by the asserts).
|
||||||
|
unsafe impl Send for GeckoStyleSheet {}
|
||||||
|
unsafe impl Sync for GeckoStyleSheet {}
|
||||||
|
|
||||||
impl fmt::Debug for GeckoStyleSheet {
|
impl fmt::Debug for GeckoStyleSheet {
|
||||||
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let contents = self.contents();
|
let contents = self.contents();
|
||||||
|
|
|
@ -102,6 +102,15 @@ impl CssRules {
|
||||||
dest: &mut CssStringWriter,
|
dest: &mut CssStringWriter,
|
||||||
) -> fmt::Result {
|
) -> fmt::Result {
|
||||||
dest.write_str(" {")?;
|
dest.write_str(" {")?;
|
||||||
|
self.to_css_block_without_opening(guard, dest)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// As above, but without the opening curly bracket. That's needed for nesting.
|
||||||
|
pub fn to_css_block_without_opening(
|
||||||
|
&self,
|
||||||
|
guard: &SharedRwLockReadGuard,
|
||||||
|
dest: &mut CssStringWriter,
|
||||||
|
) -> fmt::Result {
|
||||||
for rule in self.0.iter() {
|
for rule in self.0.iter() {
|
||||||
dest.write_str("\n ")?;
|
dest.write_str("\n ")?;
|
||||||
rule.to_css(guard, dest)?;
|
rule.to_css(guard, dest)?;
|
||||||
|
|
|
@ -788,6 +788,7 @@ impl<'a, 'b, 'i> QualifiedRuleParser<'i> for NestedRuleParser<'a, 'b> {
|
||||||
Ok(CssRule::Style(Arc::new(self.shared_lock.wrap(StyleRule {
|
Ok(CssRule::Style(Arc::new(self.shared_lock.wrap(StyleRule {
|
||||||
selectors,
|
selectors,
|
||||||
block,
|
block,
|
||||||
|
rules: None, // TODO(nesting)
|
||||||
source_location: start.source_location(),
|
source_location: start.source_location(),
|
||||||
}))))
|
}))))
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,8 +6,8 @@
|
||||||
|
|
||||||
use crate::properties::PropertyDeclarationBlock;
|
use crate::properties::PropertyDeclarationBlock;
|
||||||
use crate::selector_parser::SelectorImpl;
|
use crate::selector_parser::SelectorImpl;
|
||||||
use crate::shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked};
|
use crate::shared_lock::{DeepCloneParams, DeepCloneWithLock, Locked, SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard};
|
||||||
use crate::shared_lock::{SharedRwLock, SharedRwLockReadGuard, ToCssWithGuard};
|
use crate::stylesheets::CssRules;
|
||||||
use crate::str::CssStringWriter;
|
use crate::str::CssStringWriter;
|
||||||
use cssparser::SourceLocation;
|
use cssparser::SourceLocation;
|
||||||
#[cfg(feature = "gecko")]
|
#[cfg(feature = "gecko")]
|
||||||
|
@ -25,6 +25,8 @@ pub struct StyleRule {
|
||||||
pub selectors: SelectorList<SelectorImpl>,
|
pub selectors: SelectorList<SelectorImpl>,
|
||||||
/// The declaration block with the properties it contains.
|
/// The declaration block with the properties it contains.
|
||||||
pub block: Arc<Locked<PropertyDeclarationBlock>>,
|
pub block: Arc<Locked<PropertyDeclarationBlock>>,
|
||||||
|
/// The nested rules to this style rule. Only non-`None` when nesting is enabled.
|
||||||
|
pub rules: Option<Arc<Locked<CssRules>>>,
|
||||||
/// The location in the sheet where it was found.
|
/// The location in the sheet where it was found.
|
||||||
pub source_location: SourceLocation,
|
pub source_location: SourceLocation,
|
||||||
}
|
}
|
||||||
|
@ -35,11 +37,15 @@ impl DeepCloneWithLock for StyleRule {
|
||||||
&self,
|
&self,
|
||||||
lock: &SharedRwLock,
|
lock: &SharedRwLock,
|
||||||
guard: &SharedRwLockReadGuard,
|
guard: &SharedRwLockReadGuard,
|
||||||
_params: &DeepCloneParams,
|
params: &DeepCloneParams,
|
||||||
) -> StyleRule {
|
) -> StyleRule {
|
||||||
StyleRule {
|
StyleRule {
|
||||||
selectors: self.selectors.clone(),
|
selectors: self.selectors.clone(),
|
||||||
block: Arc::new(lock.wrap(self.block.read_with(guard).clone())),
|
block: Arc::new(lock.wrap(self.block.read_with(guard).clone())),
|
||||||
|
rules: self.rules.as_ref().map(|rules| {
|
||||||
|
let rules = rules.read_with(guard);
|
||||||
|
Arc::new(lock.wrap(rules.deep_clone_with_lock(lock, guard, params)))
|
||||||
|
}),
|
||||||
source_location: self.source_location.clone(),
|
source_location: self.source_location.clone(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -53,6 +59,9 @@ impl StyleRule {
|
||||||
n += self.selectors.0.size_of(ops);
|
n += self.selectors.0.size_of(ops);
|
||||||
n += self.block.unconditional_shallow_size_of(ops) +
|
n += self.block.unconditional_shallow_size_of(ops) +
|
||||||
self.block.read_with(guard).size_of(ops);
|
self.block.read_with(guard).size_of(ops);
|
||||||
|
if let Some(ref rules) = self.rules {
|
||||||
|
n += rules.unconditional_shallow_size_of(ops) + rules.read_with(guard).size_of(guard, ops)
|
||||||
|
}
|
||||||
n
|
n
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -61,19 +70,32 @@ impl ToCssWithGuard for StyleRule {
|
||||||
/// https://drafts.csswg.org/cssom/#serialize-a-css-rule CSSStyleRule
|
/// https://drafts.csswg.org/cssom/#serialize-a-css-rule CSSStyleRule
|
||||||
fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut CssStringWriter) -> fmt::Result {
|
fn to_css(&self, guard: &SharedRwLockReadGuard, dest: &mut CssStringWriter) -> fmt::Result {
|
||||||
use cssparser::ToCss;
|
use cssparser::ToCss;
|
||||||
|
|
||||||
// Step 1
|
// Step 1
|
||||||
self.selectors.to_css(dest)?;
|
self.selectors.to_css(dest)?;
|
||||||
|
dest.write_str(" {")?;
|
||||||
|
|
||||||
// Step 2
|
// Step 2
|
||||||
dest.write_str(" { ")?;
|
|
||||||
// Step 3
|
|
||||||
let declaration_block = self.block.read_with(guard);
|
let declaration_block = self.block.read_with(guard);
|
||||||
declaration_block.to_css(dest)?;
|
let has_declarations = !declaration_block.declarations().is_empty();
|
||||||
// Step 4
|
|
||||||
if !declaration_block.declarations().is_empty() {
|
// Step 3
|
||||||
dest.write_char(' ')?;
|
if let Some(ref rules) = self.rules {
|
||||||
|
let rules = rules.read_with(guard);
|
||||||
|
// Step 6 (here because it's more convenient)
|
||||||
|
if !rules.is_empty() {
|
||||||
|
if has_declarations {
|
||||||
|
dest.write_str("\n ")?;
|
||||||
|
declaration_block.to_css(dest)?;
|
||||||
|
}
|
||||||
|
return rules.to_css_block_without_opening(guard, dest)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
// Step 5
|
|
||||||
dest.write_char('}')
|
// Steps 4 & 5
|
||||||
|
if has_declarations {
|
||||||
|
dest.write_char(' ')?;
|
||||||
|
declaration_block.to_css(dest)?;
|
||||||
|
}
|
||||||
|
dest.write_str(" }")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue