@@ -53,22 +53,21 @@ pub(crate) struct XmlElement {
5353}
5454
5555impl XmlElement {
56- pub ( crate ) fn new ( name : & str ) -> Self {
56+ pub ( crate ) fn new < ' a > ( name : impl Into < Cow < ' a , str > > ) -> Self {
57+ let mut name = name. into ( ) ;
58+ xml_escape ( & mut name) ;
59+
5760 XmlElement {
5861 name : name. to_string ( ) ,
5962 ..Default :: default ( )
6063 }
6164 }
6265
63- pub ( crate ) fn with_attr < ' a > (
64- mut self ,
65- name : impl AsRef < str > + ' a ,
66- value : impl AsRef < str > + ' a ,
67- ) -> Self {
66+ pub ( crate ) fn with_attr < ' a > ( mut self , name : impl Into < Cow < ' a , str > > , value : impl Into < Cow < ' a , str > > ) -> Self {
6867 self . attrs . get_or_insert_with ( Vec :: new) . push (
6968 XmlAttribute {
70- name : Cow :: from ( name. as_ref ( ) ) ,
71- value : Cow :: from ( value. as_ref ( ) ) ,
69+ name : name. into ( ) ,
70+ value : value. into ( ) ,
7271 }
7372 . to_xml ( ) ,
7473 ) ;
@@ -82,49 +81,26 @@ impl XmlElement {
8281
8382 pub ( crate ) fn with_text_element < ' a > (
8483 mut self ,
85- name : & ' a str ,
86- text : impl AsRef < str > + ' a ,
84+ name : impl Into < Cow < ' a , str > > ,
85+ text : impl Into < Cow < ' a , str > > ,
8786 ) -> Self {
88- self . body . push (
89- XmlElement {
90- name : name. to_owned ( ) ,
91- body : vec ! [
92- XmlText {
93- text: Cow :: from( text. as_ref( ) ) ,
94- }
95- . to_xml( ) ,
96- ] ,
97- ..Default :: default ( )
98- }
99- . to_xml ( ) ,
100- ) ;
87+ self . body . push ( XmlElement :: new ( name) . with_text ( text) . to_xml ( ) ) ;
10188 self
10289 }
10390
104- pub ( crate ) fn with_text < ' a > ( mut self , text : impl AsRef < str > + ' a ) -> Self {
105- self . body . push (
106- XmlText {
107- text : Cow :: from ( text. as_ref ( ) ) ,
108- }
109- . to_xml ( ) ,
110- ) ;
91+ pub ( crate ) fn with_text < ' a > ( mut self , text : impl Into < Cow < ' a , str > > ) -> Self {
92+ self . body . push ( XmlText { text : text. into ( ) } . to_xml ( ) ) ;
11193 self
11294 }
11395}
11496
11597impl ToXml for XmlElement {
11698 fn to_xml ( & self ) -> String {
117- let mut name = Cow :: from ( & self . name ) ;
118- xml_escape ( & mut name) ;
119-
12099 let attrs = self . attrs . as_ref ( ) . map_or ( "" . to_string ( ) , |a| a. join ( " " ) ) ;
121100
122101 let body = self . body . join ( "" ) ;
123102
124- format ! (
125- "<{name}{}{attrs}>{body}</{name}>" ,
126- if !attrs. is_empty( ) { " " } else { "" }
127- )
103+ format ! ( "<{}{}{attrs}>{body}</{}>" , self . name, if !attrs. is_empty( ) { " " } else { "" } , self . name)
128104 }
129105}
130106
@@ -157,10 +133,7 @@ mod tests {
157133 assert_eq ! ( t5. as_ref( ) , ">" ) ;
158134 assert_eq ! ( t6. as_ref( ) , "&"" ) ;
159135 assert_eq ! ( t7. as_ref( ) , "&ü"" ) ;
160- assert_eq ! (
161- t8. as_ref( ) ,
162- "<test foo="bar">baz</test>"
163- ) ;
136+ assert_eq ! ( t8. as_ref( ) , "<test foo="bar">baz</test>" ) ;
164137 }
165138
166139 #[ test]
@@ -170,10 +143,7 @@ mod tests {
170143
171144 #[ test]
172145 fn generates_xml_text ( ) {
173- assert_eq ! (
174- XmlElement :: new( "foo" ) . with_text( "<bar&>" ) . to_xml( ) ,
175- "<foo><bar&></foo>"
176- ) ;
146+ assert_eq ! ( XmlElement :: new( "foo" ) . with_text( "<bar&>" ) . to_xml( ) , "<foo><bar&></foo>" ) ;
177147 }
178148
179149 #[ test]
@@ -190,13 +160,7 @@ mod tests {
190160
191161 #[ test]
192162 fn generates_xml_attribute ( ) {
193- assert_eq ! (
194- XmlElement :: new( "foo" )
195- . with_attr( "a" , "b" )
196- . with_text( "bar" )
197- . to_xml( ) ,
198- "<foo a=\" b\" >bar</foo>"
199- ) ;
163+ assert_eq ! ( XmlElement :: new( "foo" ) . with_attr( "a" , "b" ) . with_text( "bar" ) . to_xml( ) , "<foo a=\" b\" >bar</foo>" ) ;
200164 }
201165
202166 #[ test]
@@ -213,12 +177,7 @@ mod tests {
213177
214178 #[ test]
215179 fn generates_nested_xml_element ( ) {
216- assert_eq ! (
217- XmlElement :: new( "foo" )
218- . with_element( & XmlElement :: new( "a" ) )
219- . to_xml( ) ,
220- "<foo><a></a></foo>"
221- ) ;
180+ assert_eq ! ( XmlElement :: new( "foo" ) . with_element( & XmlElement :: new( "a" ) ) . to_xml( ) , "<foo><a></a></foo>" ) ;
222181 }
223182
224183 #[ test]
@@ -275,4 +234,12 @@ mod tests {
275234 "<a foo=\" b<>ar\" ><b c=\" "d&e\" ></b></a>"
276235 ) ;
277236 }
237+
238+ #[ test]
239+ fn escapes_with_text_element ( ) {
240+ assert_eq ! (
241+ XmlElement :: new( "a&b" ) . with_text_element( "c&d" , "f&g" ) . to_xml( ) ,
242+ "<a&b><c&d>f&g</c&d></a&b>"
243+ ) ;
244+ }
278245}
0 commit comments