File tree Expand file tree Collapse file tree 3 files changed +15
-3
lines changed Expand file tree Collapse file tree 3 files changed +15
-3
lines changed Original file line number Diff line number Diff line change 5
5
* Merged ` ParserConfig2 ` back into ` ParserConfig `
6
6
* Added option to the writer to pass through XML markup unmodified
7
7
* ` xml-analyze ` binary has been moved to examples
8
+ * Writer escapes ` -- ` in comments and ` ]]> ` in CDATA
8
9
9
10
## Version 0.8.27
10
11
Original file line number Diff line number Diff line change @@ -400,11 +400,19 @@ impl Emitter {
400
400
if self . config . cdata_to_characters {
401
401
self . emit_characters ( target, content)
402
402
} else {
403
- // TODO: escape ']]>' characters in CDATA as two adjacent CDATA blocks
404
403
target. write_all ( b"<![CDATA[" ) ?;
405
- target. write_all ( content. as_bytes ( ) ) ?;
406
- target. write_all ( b"]]>" ) ?;
407
404
405
+ for chunk in content. split_inclusive ( "]]>" ) {
406
+ let chunk_safe = chunk. strip_suffix ( "]]>" ) ;
407
+ let emit_escaped = chunk_safe. is_some ( ) ;
408
+
409
+ target. write_all ( chunk_safe. unwrap_or ( chunk) . as_bytes ( ) ) ?;
410
+ if emit_escaped {
411
+ target. write_all ( b"]]]]><![CDATA[>" ) ?;
412
+ }
413
+ }
414
+
415
+ target. write_all ( b"]]>" ) ?;
408
416
self . after_text ( ) ;
409
417
410
418
Ok ( ( ) )
Original file line number Diff line number Diff line change @@ -286,6 +286,7 @@ fn accidental_cdata_suffix_in_characters_is_escaped() {
286
286
unwrap_all ! {
287
287
w. write( XmlEvent :: start_element( "root" ) ) ;
288
288
w. write( XmlEvent :: characters( "[[a]]>b" ) ) ;
289
+ w. write( XmlEvent :: cdata( "c]]>data" ) ) ;
289
290
w. write( XmlEvent :: end_element( ) )
290
291
}
291
292
}
@@ -296,6 +297,8 @@ fn accidental_cdata_suffix_in_characters_is_escaped() {
296
297
assert ! ( matches!( r. next( ) . unwrap( ) , XmlEvent :: StartDocument { .. } ) ) ;
297
298
assert ! ( matches!( r. next( ) . unwrap( ) , XmlEvent :: StartElement { .. } ) ) ;
298
299
assert_eq ! ( r. next( ) . unwrap( ) , XmlEvent :: Characters ( "[[a]]>b" . into( ) ) ) ;
300
+ assert_eq ! ( r. next( ) . unwrap( ) , XmlEvent :: CData ( "c]]" . into( ) ) ) ;
301
+ assert_eq ! ( r. next( ) . unwrap( ) , XmlEvent :: CData ( ">data" . into( ) ) ) ;
299
302
assert ! ( matches!( r. next( ) . unwrap( ) , XmlEvent :: EndElement { .. } ) ) ;
300
303
assert ! ( matches!( r. next( ) . unwrap( ) , XmlEvent :: EndDocument ) ) ;
301
304
}
You can’t perform that action at this time.
0 commit comments