From 2f00fa6664e43ce3e3eb23ce7050fd1d75a47802 Mon Sep 17 00:00:00 2001 From: Kevin Ballard Date: Tue, 1 May 2018 19:27:22 -0700 Subject: Stop indenting the child of the tag This matches how Apple's plist output works. In order to do this without completely taking over indentation duties ourselves, we just include the tag in the prologue, so xml-rs doesn't know about it, and then go ahead and emit an end event for the anyway since xml-rs doesn't particularly care. --- src/xml/writer.rs | 49 ++++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 27 deletions(-) (limited to 'src/xml') diff --git a/src/xml/writer.rs b/src/xml/writer.rs index 14392dd..e168128 100644 --- a/src/xml/writer.rs +++ b/src/xml/writer.rs @@ -1,7 +1,6 @@ use base64; use std::borrow::Cow; use std::io::Write; -use xml_rs::attribute::Attribute; use xml_rs::escape::escape_str_pcdata; use xml_rs::name::Name; use xml_rs::namespace::Namespace; @@ -89,6 +88,10 @@ impl EventWriter { fn maybe_end_plist(&mut self) -> Result<()> { // If there are no more open tags then write the element if self.stack.len() == 1 { + // We didn't tell the xml_writer about the tag so it thinks we're already at + // the root. As such, it's not going to prettify our output, so we need to include + // the newline ourselves. + self.xml_writer.write(WriteXmlEvent::Characters("\n"))?; self.end_element("plist")?; if let Some(Element::Root) = self.stack.pop() { } else { @@ -129,18 +132,10 @@ impl PlistEventWriter for EventWriter { // Write prologue let prologue = r#" + "#; self.xml_writer.write(WriteXmlEvent::Characters(prologue))?; - let version_name = Name::local("version"); - let version_attr = Attribute::new(version_name, "1.0"); - - self.xml_writer.write(WriteXmlEvent::StartElement { - name: Name::local("plist"), - attributes: Cow::Borrowed(&[version_attr]), - namespace: Cow::Borrowed(&self.empty_namespace), - })?; - self.stack.push(Element::Root); } } @@ -239,23 +234,23 @@ mod tests { let comparison = " -\t -\t\tAuthor -\t\tWilliam Shakespeare -\t\tLines -\t\t -\t\t\tIt is a tale told by an idiot, -\t\t\tFull of sound and fury, signifying nothing. -\t\t -\t\tDeath -\t\t1564 -\t\tHeight -\t\t1.6 -\t\tData -\t\tAAAAvgAAAAMAAAAeAAAA -\t\tBirthdate -\t\t1981-05-16T11:32:06Z -\t + +\tAuthor +\tWilliam Shakespeare +\tLines +\t +\t\tIt is a tale told by an idiot, +\t\tFull of sound and fury, signifying nothing. +\t +\tDeath +\t1564 +\tHeight +\t1.6 +\tData +\tAAAAvgAAAAMAAAAeAAAA +\tBirthdate +\t1981-05-16T11:32:06Z + "; let s = String::from_utf8(cursor.into_inner()).unwrap(); -- cgit v1.2.3