mirror of
https://github.com/servo/servo.git
synced 2025-07-23 07:13:52 +01:00
Factor out PropertyDeclaration only iterator.
This commit is contained in:
parent
d40b26202d
commit
7330dab51f
1 changed files with 28 additions and 6 deletions
|
@ -12,6 +12,7 @@ use cssparser::{Parser, AtRuleParser, DeclarationParser, Delimiter};
|
||||||
use error_reporting::ParseErrorReporter;
|
use error_reporting::ParseErrorReporter;
|
||||||
use parser::{PARSING_MODE_DEFAULT, ParsingMode, ParserContext, log_css_error};
|
use parser::{PARSING_MODE_DEFAULT, ParsingMode, ParserContext, log_css_error};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
|
use std::slice::Iter;
|
||||||
use style_traits::ToCss;
|
use style_traits::ToCss;
|
||||||
use stylesheets::{CssRuleType, Origin, UrlExtraData};
|
use stylesheets::{CssRuleType, Origin, UrlExtraData};
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -54,6 +55,25 @@ pub struct PropertyDeclarationBlock {
|
||||||
longhands: LonghandIdSet,
|
longhands: LonghandIdSet,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Iterator for PropertyDeclaration to be generated from PropertyDeclarationBlock.
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct PropertyDeclarationIterator<'a> {
|
||||||
|
iter: Iter<'a, (PropertyDeclaration, Importance)>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> Iterator for PropertyDeclarationIterator<'a> {
|
||||||
|
type Item = &'a PropertyDeclaration;
|
||||||
|
#[inline]
|
||||||
|
fn next(&mut self) -> Option<&'a PropertyDeclaration> {
|
||||||
|
// we use this function because a closure won't be `Clone`
|
||||||
|
fn get_declaration(dec: &(PropertyDeclaration, Importance))
|
||||||
|
-> &PropertyDeclaration {
|
||||||
|
&dec.0
|
||||||
|
}
|
||||||
|
self.iter.next().map(get_declaration as fn(_) -> _)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl fmt::Debug for PropertyDeclarationBlock {
|
impl fmt::Debug for PropertyDeclarationBlock {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
self.declarations.fmt(f)
|
self.declarations.fmt(f)
|
||||||
|
@ -93,6 +113,13 @@ impl PropertyDeclarationBlock {
|
||||||
&self.declarations
|
&self.declarations
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Iterate over only PropertyDeclaration.
|
||||||
|
pub fn declarations_iter(&self) -> PropertyDeclarationIterator {
|
||||||
|
PropertyDeclarationIterator {
|
||||||
|
iter: self.declarations.iter(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns whether this block contains any declaration with `!important`.
|
/// Returns whether this block contains any declaration with `!important`.
|
||||||
///
|
///
|
||||||
/// This is based on the `important_count` counter,
|
/// This is based on the `important_count` counter,
|
||||||
|
@ -369,15 +396,10 @@ impl PropertyDeclarationBlock {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(shorthand) => {
|
Ok(shorthand) => {
|
||||||
// we use this function because a closure won't be `Clone`
|
|
||||||
fn get_declaration(dec: &(PropertyDeclaration, Importance))
|
|
||||||
-> &PropertyDeclaration {
|
|
||||||
&dec.0
|
|
||||||
}
|
|
||||||
if !self.declarations.iter().all(|decl| decl.0.shorthands().contains(&shorthand)) {
|
if !self.declarations.iter().all(|decl| decl.0.shorthands().contains(&shorthand)) {
|
||||||
return Err(fmt::Error)
|
return Err(fmt::Error)
|
||||||
}
|
}
|
||||||
let iter = self.declarations.iter().map(get_declaration as fn(_) -> _);
|
let iter = self.declarations_iter();
|
||||||
match shorthand.get_shorthand_appendable_value(iter) {
|
match shorthand.get_shorthand_appendable_value(iter) {
|
||||||
Some(AppendableValue::Css { css, .. }) => {
|
Some(AppendableValue::Css { css, .. }) => {
|
||||||
dest.write_str(css)
|
dest.write_str(css)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue