Skip to content

Commit

Permalink
Don't trim whitespace from XML plist strings
Browse files Browse the repository at this point in the history
  • Loading branch information
ebarnard committed Feb 17, 2023
1 parent aa38213 commit b1cd6ad
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/serde_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,7 @@ fn check_common_plist(dict: &Dictionary) {
assert_eq!(lines.len(), 2);
assert_eq!(
lines[0].as_string().unwrap(),
"It is a tale told by an idiot,"
"It is a tale told by an idiot, "
);
assert_eq!(
lines[1].as_string().unwrap(),
Expand Down
2 changes: 1 addition & 1 deletion src/stream/binary_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ mod tests {
Real(1.6),
String("Lines".into()),
StartArray(Some(2)),
String("It is a tale told by an idiot,".into()),
String("It is a tale told by an idiot, ".into()),
String("Full of sound and fury, signifying nothing.".into()),
EndCollection,
String("Death".into()),
Expand Down
16 changes: 12 additions & 4 deletions src/stream/xml_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct ReaderState<R: Read>(EventReader<BufReader<R>>);
impl<R: Read> XmlReader<R> {
pub fn new(reader: R) -> XmlReader<R> {
let mut xml_reader = EventReader::from_reader(BufReader::new(reader));
xml_reader.trim_text(true);
xml_reader.trim_text(false);
xml_reader.check_end_names(true);
xml_reader.expand_empty_elements(true);

Expand Down Expand Up @@ -179,8 +179,16 @@ impl<R: Read> ReaderState<R> {
_ => (),
},
XmlEvent::Eof => return Ok(None),
XmlEvent::Text(_) => {
return Err(self.with_pos(ErrorKind::UnexpectedXmlCharactersExpectedElement))
XmlEvent::Text(text) => {
let unescaped = text
.unescape()
.map_err(|err| self.with_pos(ErrorKind::from(err)))?;

if !unescaped.chars().all(char::is_whitespace) {
return Err(
self.with_pos(ErrorKind::UnexpectedXmlCharactersExpectedElement)
);
}
}
XmlEvent::PI(_)
| XmlEvent::Decl(_)
Expand Down Expand Up @@ -214,7 +222,7 @@ mod tests {
String("William Shakespeare".into()),
String("Lines".into()),
StartArray(None),
String("It is a tale told by an idiot,".into()),
String("It is a tale told by an idiot, ".into()),
String("Full of sound and fury, signifying nothing.".into()),
EndCollection,
String("Death".into()),
Expand Down
Binary file modified tests/data/binary.plist
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/data/xml.plist
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<string>William Shakespeare</string>
<key>Lines</key>
<array>
<string>It is a tale told by an idiot,</string>
<string>It is a tale told by an idiot, </string>
<string>Full of sound and fury, signifying nothing.</string>
</array>
<key>Death</key>
Expand Down

0 comments on commit b1cd6ad

Please sign in to comment.