Add TableRowAndGroupIterator

This commit is contained in:
Manish Goregaokar 2018-02-12 15:33:08 -08:00
parent b416bb3aa7
commit 35be0c50f6

View file

@ -860,40 +860,43 @@ enum NextBlockCollapsedBorders<'a> {
FromTable(CollapsedBorder), FromTable(CollapsedBorder),
} }
/// Iterator over all the rows of a table /// Iterator over all the rows of a table, which also
struct TableRowIterator<'a> { /// provides the Fragment for rowgroups if any
struct TableRowAndGroupIterator<'a> {
kids: MutFlowListIterator<'a>, kids: MutFlowListIterator<'a>,
grandkids: Option<MutFlowListIterator<'a>>, group: Option<(&'a Fragment, MutFlowListIterator<'a>)>
} }
impl<'a> TableRowIterator<'a> { impl<'a> TableRowAndGroupIterator<'a> {
fn new(base: &'a mut BaseFlow) -> Self { fn new(base: &'a mut BaseFlow) -> Self {
TableRowIterator { TableRowAndGroupIterator {
kids: base.child_iter_mut(), kids: base.child_iter_mut(),
grandkids: None, group: None,
} }
} }
} }
impl<'a> Iterator for TableRowIterator<'a> { impl<'a> Iterator for TableRowAndGroupIterator<'a> {
type Item = &'a mut TableRowFlow; type Item = (Option<&'a Fragment>, &'a mut TableRowFlow);
#[inline] #[inline]
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
// If we're inside a rowgroup, iterate through the rowgroup's children. // If we're inside a rowgroup, iterate through the rowgroup's children.
if let Some(ref mut grandkids) = self.grandkids { if let Some(ref mut group) = self.group {
if let Some(grandkid) = grandkids.next() { if let Some(grandkid) = group.1.next() {
return Some(grandkid.as_mut_table_row()) return Some((Some(group.0), grandkid.as_mut_table_row()))
} }
} }
// Otherwise, iterate through the table's children. // Otherwise, iterate through the table's children.
self.grandkids = None; self.group = None;
match self.kids.next() { match self.kids.next() {
Some(kid) => { Some(kid) => {
if kid.is_table_rowgroup() { if kid.is_table_rowgroup() {
self.grandkids = Some(kid.mut_base().child_iter_mut()); let mut rowgroup = kid.as_mut_table_rowgroup();
let iter = rowgroup.block_flow.base.child_iter_mut();
self.group = Some((&rowgroup.block_flow.fragment, iter));
self.next() self.next()
} else if kid.is_table_row() { } else if kid.is_table_row() {
Some(kid.as_mut_table_row()) Some((None, kid.as_mut_table_row()))
} else { } else {
self.next() // Skip children that are not rows or rowgroups self.next() // Skip children that are not rows or rowgroups
} }
@ -902,3 +905,20 @@ impl<'a> Iterator for TableRowIterator<'a> {
} }
} }
} }
/// Iterator over all the rows of a table
struct TableRowIterator<'a>(TableRowAndGroupIterator<'a>);
impl<'a> TableRowIterator<'a> {
fn new(base: &'a mut BaseFlow) -> Self {
TableRowIterator(TableRowAndGroupIterator::new(base))
}
}
impl<'a> Iterator for TableRowIterator<'a> {
type Item = &'a mut TableRowFlow;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.0.next().map(|n| n.1)
}
}