Skip to content

Commit 19747ea

Browse files
committed
feat: write self closing tags
1 parent 87a2404 commit 19747ea

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

src/write.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ fn write_node(writer: &mut Writer<Cursor<Vec<u8>>>, node: Node) {
1212
for (k, v) in node.attrs {
1313
start.push_attribute((k.as_str(), v.as_str()));
1414
}
15+
if node.children.is_empty() && node.text.is_none() {
16+
writer.write_event(Event::Empty(start)).unwrap();
17+
return;
18+
}
1519
writer.write_event(Event::Start(start)).unwrap();
1620
if let Some(text) = node.text {
1721
writer
@@ -118,4 +122,24 @@ mod tests {
118122
remove_file("tests/test_write.xml").unwrap();
119123
assert_eq!(file_str, expected);
120124
}
125+
#[test]
126+
fn test_write_self_closing_tag() {
127+
let mut root = Node {
128+
name: f_str!("root"),
129+
attrs: HashMap::new(),
130+
children: Vec::new(),
131+
text: None,
132+
};
133+
let mut attrs = HashMap::new();
134+
attrs.insert(f_str!("attr"), f_str!("value"));
135+
root.children.push(Node {
136+
name: f_str!("child"),
137+
attrs,
138+
children: Vec::new(),
139+
text: None,
140+
});
141+
let expected = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<root>\n <child attr=\"value\"/>\n</root>";
142+
let result = write_string(root, Some(4), Some(true));
143+
assert_eq!(result, expected);
144+
}
121145
}

0 commit comments

Comments
 (0)