File tree Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Expand file tree Collapse file tree 1 file changed +24
-0
lines changed Original file line number Diff line number Diff 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}
You can’t perform that action at this time.
0 commit comments