Repository: tafia/quick-xml (rust)

Issue:
Writer::new_with_indent() inserts white space into <![CDATA[ content tags
Problem: the pretty print functionality should not insert spaces new lines before and after `<![CDATA[` tags.

Example input:
```xml
<?xml version="1.0" encoding="UTF-8"?><jobs><job><jobid><![CDATA[00d46e4494e1]]></jobid></job></jobs>
```
Output with `Writer::new_with_indent(out_file, b" "[0], 2);`
```xml
<?xml version="1.0" encoding="UTF-8"?>
<jobs>
  <job>
    <jobid>
      <![CDATA[00d46e4494e1]]>
    </jobid>
  </job>
</jobs>
```
This has now changed the content of the jobid tag, which is bad.

Correct with `xmllint --format`:
```xml
<?xml version="1.0" encoding="UTF-8"?>
<jobs>
  <job>
    <jobid><![CDATA[00d46e4494e1]]></jobid>
  </job>
</jobs>
```
This has correctly preserved the content of the jobid tag.

Pull request description:
inline CData events when pretty printing
Close #197

Patch:
diff --git a/src/writer.rs b/src/writer.rs
index 879509d..873607a 100644
--- a/src/writer.rs
+++ b/src/writer.rs
@@ -113,7 +113,12 @@ impl<W: Write> Writer<W> {
                 self.write(&e.escaped())
             }
             Event::Comment(ref e) => self.write_wrapped(b"<!--", e, b"-->"),
-            Event::CData(ref e) => self.write_wrapped(b"<![CDATA[", e, b"]]>"),
+            Event::CData(ref e) => {
+                next_should_line_break = false;
+                self.write(b"<![CDATA[")?;
+                self.write(e)?;
+                self.write(b"]]>")
+            }
             Event::Decl(ref e) => self.write_wrapped(b"<?", e, b"?>"),
             Event::PI(ref e) => self.write_wrapped(b"<?", e, b"?>"),
             Event::DocType(ref e) => self.write_wrapped(b"<!DOCTYPE", e, b">"),


Test patch:
diff --git a/tests/documents/test_writer_indent_cdata.xml b/tests/documents/test_writer_indent_cdata.xml
new file mode 100644
index 0000000..6f5539e
--- /dev/null
+++ b/tests/documents/test_writer_indent_cdata.xml
@@ -0,0 +1,6 @@
+<?xml version="1.0"?>
+<jobs>
+    <job>
+        <jobid><![CDATA[00d46e4494e1]]></jobid>
+    </job>
+</jobs>
\ No newline at end of file
diff --git a/tests/unit_tests.rs b/tests/unit_tests.rs
index 71ee9d4..8893914 100644
--- a/tests/unit_tests.rs
+++ b/tests/unit_tests.rs
@@ -258,6 +258,25 @@ fn test_writer_indent() {
     assert_eq!(result, txt.as_bytes());
 }
 
+#[test]
+fn test_writer_indent_cdata() {
+    let txt = include_str!("../tests/documents/test_writer_indent_cdata.xml");
+    let mut reader = Reader::from_str(txt);
+    reader.trim_text(true);
+    let mut writer = Writer::new_with_indent(Cursor::new(Vec::new()), b' ', 4);
+    let mut buf = Vec::new();
+    loop {
+        match reader.read_event(&mut buf) {
+            Ok(Eof) => break,
+            Ok(e) => assert!(writer.write_event(e).is_ok()),
+            Err(e) => panic!(e),
+        }
+    }
+
+    let result = writer.into_inner().into_inner();
+    assert_eq!(result, txt.as_bytes());
+}
+
 #[test]
 fn test_write_empty_element_attrs() {
     let str_from = r#"<source attr="val"/>"#;