Add any_important and any_normal methods to PropertyDeclarationBlock

This commit is contained in:
Simon Sapin 2016-08-31 13:41:38 +02:00
parent 7ef4930472
commit 95033e1c0d
3 changed files with 25 additions and 5 deletions

View file

@ -292,6 +292,26 @@ pub struct PropertyDeclarationBlock {
pub important_count: u32, pub important_count: u32,
} }
impl PropertyDeclarationBlock {
/// Returns wheather this block contains any declaration with `!important`.
///
/// This is based on the `important_count` counter,
/// which should be maintained whenever `declarations` is changed.
// FIXME: make fields private and maintain it here in methods?
pub fn any_important(&self) -> bool {
self.important_count > 0
}
/// Returns wheather this block contains any declaration without `!important`.
///
/// This is based on the `important_count` counter,
/// which should be maintained whenever `declarations` is changed.
// FIXME: make fields private and maintain it here in methods?
pub fn any_normal(&self) -> bool {
self.declarations.len() > self.important_count as usize
}
}
impl ToCss for PropertyDeclarationBlock { impl ToCss for PropertyDeclarationBlock {
// https://drafts.csswg.org/cssom/#serialize-a-css-declaration-block // https://drafts.csswg.org/cssom/#serialize-a-css-declaration-block
fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write { fn to_css<W>(&self, dest: &mut W) -> fmt::Result where W: fmt::Write {

View file

@ -398,7 +398,7 @@ impl Stylist {
// Step 4: Normal style attributes. // Step 4: Normal style attributes.
if let Some(sa) = style_attribute { if let Some(sa) = style_attribute {
if sa.declarations.len() > sa.important_count as usize { if sa.any_normal() {
relations |= AFFECTED_BY_STYLE_ATTRIBUTE; relations |= AFFECTED_BY_STYLE_ATTRIBUTE;
Push::push( Push::push(
applicable_declarations, applicable_declarations,
@ -419,7 +419,7 @@ impl Stylist {
// Step 6: `!important` style attributes. // Step 6: `!important` style attributes.
if let Some(sa) = style_attribute { if let Some(sa) = style_attribute {
if sa.important_count > 0 { if sa.any_important() {
relations |= AFFECTED_BY_STYLE_ATTRIBUTE; relations |= AFFECTED_BY_STYLE_ATTRIBUTE;
Push::push( Push::push(
applicable_declarations, applicable_declarations,
@ -764,9 +764,9 @@ impl SelectorMap {
for rule in rules.iter() { for rule in rules.iter() {
let block = &rule.declarations.mixed_declarations; let block = &rule.declarations.mixed_declarations;
let any_declaration_for_importance = if importance.important() { let any_declaration_for_importance = if importance.important() {
block.important_count > 0 block.any_important()
} else { } else {
block.declarations.len() > block.important_count as usize block.any_normal()
}; };
if any_declaration_for_importance && if any_declaration_for_importance &&
matches_complex_selector(&*rule.selector, matches_complex_selector(&*rule.selector,

View file

@ -473,7 +473,7 @@ impl<'le> TElement for GeckoElement<'le> {
None None
} else { } else {
let opt_ptr = GeckoDeclarationBlock::with(declarations, |declarations| { let opt_ptr = GeckoDeclarationBlock::with(declarations, |declarations| {
// Use a raw poointer to extend the lifetime // Use a raw pointer to extend the lifetime
declarations.declarations.as_ref().map(|r| r as *const Arc<_>) declarations.declarations.as_ref().map(|r| r as *const Arc<_>)
}); });
opt_ptr.map(|ptr| unsafe { &*ptr }) opt_ptr.map(|ptr| unsafe { &*ptr })