From eaf7c8025b32878d1d75199f28cc24f79907cc32 Mon Sep 17 00:00:00 2001 From: Yonghye Kwon Date: Wed, 22 Jul 2026 07:20:24 +0900 Subject: [PATCH] fix: read dataBar cfvo and color children written as start/end tags Some producers (e.g. openpyxl) write and inside as rather than self-closing . DataBar's reader only matched Event::Empty, so both collections came back empty and consumers lost the bar color. Match Event::Start as well, mirroring ColorScale's reader. Co-Authored-By: Claude Fable 5 Signed-off-by: Yonghye Kwon --- src/structs/data_bar.rs | 53 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/src/structs/data_bar.rs b/src/structs/data_bar.rs index de08d275..bad98904 100644 --- a/src/structs/data_bar.rs +++ b/src/structs/data_bar.rs @@ -85,16 +85,17 @@ impl DataBar { ) { xml_read_loop!( reader, - Event::Empty(ref e) => { + ref n @ (Event::Empty(ref e) | Event::Start(ref e)) => { + let is_empty = matches!(n, Event::Empty(_)); match e.name().into_inner() { b"cfvo" => { let mut obj = ConditionalFormatValueObject::default(); - obj.set_attributes(reader, e, true); + obj.set_attributes(reader, e, is_empty); self.cfvo_collection.push(obj); } b"color" => { let mut obj = Color::default(); - obj.set_attributes(reader, e, true); + obj.set_attributes(reader, e, is_empty); self.color_collection.push(obj); } _ => (), @@ -126,3 +127,49 @@ impl DataBar { write_end_tag(writer, "dataBar"); } } + +#[cfg(test)] +mod tests { + use super::*; + + fn read_data_bar(xml: &str) -> DataBar { + let mut reader = Reader::from_reader(std::io::BufReader::new(xml.as_bytes())); + let mut buf = Vec::new(); + loop { + match reader.read_event_into(&mut buf) { + Ok(Event::Start(ref e)) | Ok(Event::Empty(ref e)) + if e.name().into_inner() == b"dataBar" => + { + let mut obj = DataBar::default(); + obj.set_attributes(&mut reader, e); + return obj; + } + Ok(Event::Eof) => panic!("dataBar element not found"), + _ => (), + } + buf.clear(); + } + } + + #[test] + fn read_child_elements_with_end_tags() { + // Writers such as openpyxl emit children as + // (Start + End events instead of Empty). + let obj = read_data_bar( + r#""#, + ); + assert_eq!(obj.get_cfvo_collection().len(), 2); + assert_eq!(obj.get_color_collection().len(), 1); + assert_eq!(obj.get_color_collection()[0].get_argb_str(), "FF1E2761"); + } + + #[test] + fn read_child_elements_self_closing() { + let obj = read_data_bar( + r#""#, + ); + assert_eq!(obj.get_cfvo_collection().len(), 2); + assert_eq!(obj.get_color_collection().len(), 1); + assert_eq!(obj.get_color_collection()[0].get_argb_str(), "FF638EC6"); + } +}