From 2be13757458120f44173921c0f4f90dd8c38c2d5 Mon Sep 17 00:00:00 2001 From: Xidorn Quan Date: Wed, 11 Jul 2018 10:52:52 +1000 Subject: [PATCH] style: Add a declaration iterator to AllShorthand for simplify code. Bug: 1461285 Reviewed-by: emilio MozReview-Commit-ID: 9w2B3SpdkQo --- .../style/properties/declaration_block.rs | 31 ++----------- .../style/properties/properties.mako.rs | 44 +++++++++++++++++++ 2 files changed, 47 insertions(+), 28 deletions(-) diff --git a/components/style/properties/declaration_block.rs b/components/style/properties/declaration_block.rs index 92fa0bc4134..0d5ba518935 100644 --- a/components/style/properties/declaration_block.rs +++ b/components/style/properties/declaration_block.rs @@ -455,34 +455,9 @@ impl PropertyDeclarationBlock { mode, ); } - match drain.all_shorthand { - AllShorthand::NotSet => {} - AllShorthand::CSSWideKeyword(keyword) => { - for id in ShorthandId::All.longhands() { - let decl = PropertyDeclaration::CSSWideKeyword( - WideKeywordDeclaration { id, keyword }, - ); - changed |= self.push( - decl, - importance, - mode, - ); - } - } - AllShorthand::WithVariables(unparsed) => { - for id in ShorthandId::All.longhands() { - let decl = PropertyDeclaration::WithVariables( - VariableDeclaration { id, value: unparsed.clone() }, - ); - changed |= self.push( - decl, - importance, - mode, - ); - } - } - } - changed + drain.all_shorthand.declarations().fold(changed, |changed, decl| { + changed | self.push(decl, importance, mode) + }) } /// Adds or overrides the declaration for a given property in this block. diff --git a/components/style/properties/properties.mako.rs b/components/style/properties/properties.mako.rs index 7eb36f8d418..dcc85d57509 100644 --- a/components/style/properties/properties.mako.rs +++ b/components/style/properties/properties.mako.rs @@ -2222,6 +2222,50 @@ enum AllShorthand { WithVariables(Arc) } +impl AllShorthand { + /// Iterates property declarations from the given all shorthand value. + #[inline] + fn declarations(&self) -> AllShorthandDeclarationIterator { + AllShorthandDeclarationIterator { + all_shorthand: self, + longhands: ShorthandId::All.longhands(), + } + } +} + +struct AllShorthandDeclarationIterator<'a> { + all_shorthand: &'a AllShorthand, + longhands: NonCustomPropertyIterator, +} + +impl<'a> Iterator for AllShorthandDeclarationIterator<'a> { + type Item = PropertyDeclaration; + + #[inline] + fn next(&mut self) -> Option { + match *self.all_shorthand { + AllShorthand::NotSet => None, + AllShorthand::CSSWideKeyword(ref keyword) => { + Some(PropertyDeclaration::CSSWideKeyword( + WideKeywordDeclaration { + id: self.longhands.next()?, + keyword: *keyword + } + )) + } + AllShorthand::WithVariables(ref unparsed) => { + let id = self.longhands.next()?; + Some(PropertyDeclaration::WithVariables( + VariableDeclaration { + id: self.longhands.next()?, + value: unparsed.clone() + } + )) + } + } + } +} + #[cfg(feature = "gecko")] pub use gecko_properties::style_structs;