From 268f0f75b93a7bd593f866edf31e3f3b4031c254 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Thu, 11 Jan 2018 13:10:33 +0100 Subject: [PATCH] style: Factor out a few invalidation functions that are going to be shared soon. --- .../element/state_and_attributes.rs | 75 ++++++++++++------- 1 file changed, 49 insertions(+), 26 deletions(-) diff --git a/components/style/invalidation/element/state_and_attributes.rs b/components/style/invalidation/element/state_and_attributes.rs index b19198b2120..37d6fc675b2 100644 --- a/components/style/invalidation/element/state_and_attributes.rs +++ b/components/style/invalidation/element/state_and_attributes.rs @@ -93,6 +93,50 @@ impl<'a, 'b: 'a, E: TElement> StateAndAttrInvalidationProcessor<'a, 'b, E> { } } + +/// Whether we should process the descendants of a given element for style +/// invalidation. +pub fn should_process_descendants(data: &ElementData) -> bool { + !data.styles.is_display_none() && + !data.hint.contains(RestyleHint::RESTYLE_DESCENDANTS) +} + +/// Propagates the bits after invalidating a descendant child. +pub fn invalidated_descendants(element: E, child: E) +where + E: TElement, +{ + if child.get_data().is_none() { + return; + } + + // The child may not be a flattened tree child of the current element, + // but may be arbitrarily deep. + // + // Since we keep the traversal flags in terms of the flattened tree, + // we need to propagate it as appropriate. + let mut current = child.traversal_parent(); + while let Some(parent) = current.take() { + unsafe { parent.set_dirty_descendants() }; + current = parent.traversal_parent(); + + if parent == element { + break; + } + } +} + +/// Sets the appropriate restyle hint after invalidating the style of a given +/// element. +pub fn invalidated_self(element: E) +where + E: TElement, +{ + if let Some(mut data) = element.mutate_data() { + data.hint.insert(RestyleHint::RESTYLE_SELF); + } +} + impl<'a, 'b: 'a, E: 'a> InvalidationProcessor<'a, E> for StateAndAttrInvalidationProcessor<'a, 'b, E> where E: TElement, @@ -251,17 +295,15 @@ where fn should_process_descendants(&mut self, element: E) -> bool { if element == self.element { - return !self.data.styles.is_display_none() && - !self.data.hint.contains(RestyleHint::RESTYLE_DESCENDANTS) + return should_process_descendants(&self.data) } let data = match element.borrow_data() { + Some(d) => d, None => return false, - Some(data) => data, }; - !data.styles.is_display_none() && - !data.hint.contains(RestyleHint::RESTYLE_DESCENDANTS) + should_process_descendants(&data) } fn recursion_limit_exceeded(&mut self, element: E) { @@ -276,31 +318,12 @@ where } fn invalidated_descendants(&mut self, element: E, child: E) { - if child.get_data().is_none() { - return; - } - - // The child may not be a flattened tree child of the current element, - // but may be arbitrarily deep. - // - // Since we keep the traversal flags in terms of the flattened tree, - // we need to propagate it as appropriate. - let mut current = child.traversal_parent(); - while let Some(parent) = current.take() { - unsafe { parent.set_dirty_descendants() }; - current = parent.traversal_parent(); - - if parent == element { - break; - } - } + invalidated_descendants(element, child) } fn invalidated_self(&mut self, element: E) { debug_assert_ne!(element, self.element); - if let Some(mut data) = element.mutate_data() { - data.hint.insert(RestyleHint::RESTYLE_SELF); - } + invalidated_self(element); } }