Add a generic inorder traversal method

This commit is contained in:
Matt Brubeck 2017-08-07 12:40:21 -07:00
parent a624496cc4
commit 0d36930727
3 changed files with 19 additions and 16 deletions

View file

@ -542,6 +542,9 @@ pub trait MutableFlowUtils {
/// Traverses the tree in postorder. /// Traverses the tree in postorder.
fn traverse_postorder<T: PostorderFlowTraversal>(self, traversal: &T); fn traverse_postorder<T: PostorderFlowTraversal>(self, traversal: &T);
/// Traverses the tree in-order.
fn traverse_inorder<T: InorderFlowTraversal>(self, traversal: &mut T, level: u32);
/// Traverse the Absolute flow tree in preorder. /// Traverse the Absolute flow tree in preorder.
/// ///
/// Traverse all your direct absolute descendants, who will then traverse /// Traverse all your direct absolute descendants, who will then traverse
@ -643,7 +646,7 @@ pub trait InorderFlowTraversal {
/// Returns true if this node should be processed and false if neither this node nor its /// Returns true if this node should be processed and false if neither this node nor its
/// descendants should be processed. /// descendants should be processed.
fn should_process(&mut self, flow: &mut Flow) -> bool; fn should_process_subtree(&mut self, flow: &mut Flow) -> bool;
} }
bitflags! { bitflags! {
@ -1378,6 +1381,18 @@ impl<'a> MutableFlowUtils for &'a mut Flow {
} }
} }
/// Traverses the tree in-order.
fn traverse_inorder<T: InorderFlowTraversal>(self, traversal: &mut T, level: u32) {
if !traversal.should_process_subtree(self) {
return;
}
traversal.process(self, level);
for kid in child_iter_mut(self) {
kid.traverse_inorder(traversal, level + 1);
}
}
/// Calls `repair_style` and `bubble_inline_sizes`. You should use this method instead of /// Calls `repair_style` and `bubble_inline_sizes`. You should use this method instead of
/// calling them individually, since there is no reason not to perform both operations. /// calling them individually, since there is no reason not to perform both operations.

View file

@ -130,7 +130,7 @@ impl<'a> InorderFlowTraversal for ResolveGeneratedContent<'a> {
} }
#[inline] #[inline]
fn should_process(&mut self, flow: &mut Flow) -> bool { fn should_process_subtree(&mut self, flow: &mut Flow) -> bool {
flow::base(flow).restyle_damage.intersects(RESOLVE_GENERATED_CONTENT) || flow::base(flow).restyle_damage.intersects(RESOLVE_GENERATED_CONTENT) ||
flow::base(flow).flags.intersects(AFFECTS_COUNTERS | HAS_COUNTER_AFFECTING_CHILDREN) flow::base(flow).flags.intersects(AFFECTS_COUNTERS | HAS_COUNTER_AFFECTING_CHILDREN)
} }

View file

@ -9,7 +9,7 @@ use context::LayoutContext;
use display_list_builder::DisplayListBuildState; use display_list_builder::DisplayListBuildState;
use euclid::{Point2D, Vector2D}; use euclid::{Point2D, Vector2D};
use floats::SpeculatedFloatPlacement; use floats::SpeculatedFloatPlacement;
use flow::{self, Flow, ImmutableFlowUtils, InorderFlowTraversal, MutableFlowUtils}; use flow::{self, Flow, ImmutableFlowUtils, MutableFlowUtils};
use flow::{PostorderFlowTraversal, PreorderFlowTraversal}; use flow::{PostorderFlowTraversal, PreorderFlowTraversal};
use flow::IS_ABSOLUTELY_POSITIONED; use flow::IS_ABSOLUTELY_POSITIONED;
use fragment::{FragmentBorderBoxIterator, CoordinateSystem}; use fragment::{FragmentBorderBoxIterator, CoordinateSystem};
@ -22,20 +22,8 @@ use traversal::{AssignBSizes, AssignISizes, BubbleISizes, BuildDisplayList};
pub use style::sequential::traverse_dom; pub use style::sequential::traverse_dom;
pub fn resolve_generated_content(root: &mut Flow, layout_context: &LayoutContext) { pub fn resolve_generated_content(root: &mut Flow, layout_context: &LayoutContext) {
fn doit(flow: &mut Flow, level: u32, traversal: &mut ResolveGeneratedContent) {
if !traversal.should_process(flow) {
return;
}
traversal.process(flow, level);
for kid in flow::mut_base(flow).children.iter_mut() {
doit(kid, level + 1, traversal)
}
}
let mut traversal = ResolveGeneratedContent::new(&layout_context); let mut traversal = ResolveGeneratedContent::new(&layout_context);
doit(root, 0, &mut traversal) root.traverse_inorder(&mut traversal, 0);
} }
pub fn traverse_flow_tree_preorder(root: &mut Flow, layout_context: &LayoutContext, relayout_mode: RelayoutMode) { pub fn traverse_flow_tree_preorder(root: &mut Flow, layout_context: &LayoutContext, relayout_mode: RelayoutMode) {