From 743e973e63487c14325c6e8e27884d5366846d2c Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 2 Mar 2023 19:59:00 +0100 Subject: fix: informal table use first body row if no head is found --- src/informal_table.rs | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) (limited to 'src') diff --git a/src/informal_table.rs b/src/informal_table.rs index 624118b..a940de4 100644 --- a/src/informal_table.rs +++ b/src/informal_table.rs @@ -127,17 +127,7 @@ impl TableGroup }) .collect::, _>>()?; - let head = element - .child_elements() - .get_first_tagged_with_name("thead") - .ok_or(TableGroupError::MissingHead)? - .child_elements() - .get_all_tagged_elements_with_name("row") - .into_iter() - .map(|row_element| TableRow::from_elements(row_element.child_elements())) - .collect::, _>>()?; - - let body = element + let mut body = element .child_elements() .get_first_tagged_with_name("tbody") .ok_or(TableGroupError::MissingBody)? @@ -147,6 +137,23 @@ impl TableGroup .map(|row_element| TableRow::from_elements(row_element.child_elements())) .collect::, _>>()?; + let head = element + .child_elements() + .get_first_tagged_with_name("thead") + .map_or_else( + || Ok(vec![body.remove(0)]), + |head_element| { + head_element + .child_elements() + .get_all_tagged_elements_with_name("row") + .into_iter() + .map(|row_element| { + TableRow::from_elements(row_element.child_elements()) + }) + .collect::, _>>() + }, + )?; + Ok(Self { cols, align, @@ -169,10 +176,6 @@ pub enum TableGroupError #[error("Invalid column specification")] InvalidColumnSpec(#[from] ColumnSpecError), - /// Missing head element. - #[error("Missing head")] - MissingHead, - /// Missing body element. #[error("Missing body")] MissingBody, -- cgit v1.2.3-18-g5258