Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/de/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,14 @@ where
QNameDeserializer::from_attr(QName(&slice[key]), decoder, &mut self.de.key_buf)?;
seed.deserialize(de).map(Some)
} else {
self.skip_whitespaces()?;
// If we have dedicated "$text" field, whitespace-only text may be significant.
// That also means, that type with `$text` fields behaves as if its element has
// `xml:space="preserve"` attribute: you must not have pretty-print indents
// inside this element.
if !self.has_text_field {
self.skip_whitespaces()?;
}

// try getting from events (<key>value</key>)
match self.de.peek()? {
// If we have dedicated "$text" field, it will not be passed to "$value" field
Expand Down
9 changes: 9 additions & 0 deletions src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4641,6 +4641,15 @@ mod tests {
assert_eq!(de.next().unwrap(), DeEvent::Eof);
}

#[test]
fn space() {
let mut de = make_de("<tag> </tag>");
assert_eq!(de.next().unwrap(), DeEvent::Start(BytesStart::new("tag")));
assert_eq!(de.next().unwrap(), DeEvent::Text(" ".into()));
assert_eq!(de.next().unwrap(), DeEvent::End(BytesEnd::new("tag")));
assert_eq!(de.next().unwrap(), DeEvent::Eof);
}

// start::text::text has no difference from start::text

#[test]
Expand Down
36 changes: 4 additions & 32 deletions tests/serde-de-seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -831,14 +831,7 @@ mod fixed_name {
}

from_str::<List>(
r#"
<root>
<item/>
<item/>
text
<![CDATA[cdata]]>
</root>
"#,
r#"<root><item/><item/>text<![CDATA[cdata]]></root>"#,
)
.unwrap();
}
Expand Down Expand Up @@ -1688,14 +1681,7 @@ mod fixed_name {
}

let data: List = from_str(
r#"
<root>
<item/>
<item/>
text
<![CDATA[cdata]]>
</root>
"#,
r#"<root><item/><item/>text<![CDATA[cdata]]></root>"#,
)
.unwrap();

Expand Down Expand Up @@ -2946,14 +2932,7 @@ mod variable_name {
}

from_str::<List>(
r#"
<root>
<item/>
<item/>
text
<![CDATA[cdata]]>
</root>
"#,
r#"<root><item/><item/>text<![CDATA[cdata]]></root>"#,
)
.unwrap();
}
Expand Down Expand Up @@ -4015,14 +3994,7 @@ mod variable_name {
}

let data: List = from_str(
r#"
<root>
<item/>
<item/>
text
<![CDATA[cdata]]>
</root>
"#,
r#"<root><item/><item/>text<![CDATA[cdata]]></root>"#,
)
.unwrap();

Expand Down
22 changes: 20 additions & 2 deletions tests/serde-de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,30 @@ mod text {
content: String,
}

let item: Item = from_str(r#"<root>content</root>"#).unwrap();
let item: Item = from_str(r#"<root>content </root>"#).unwrap();

assert_eq!(
item,
Item {
content: "content".into()
content: "content ".into()
}
);
}

#[test]
fn explicit_space() {
#[derive(Debug, Deserialize, PartialEq)]
struct Item {
#[serde(rename = "$text")]
content: String,
}

let item: Item = from_str(r#"<root> </root>"#).unwrap();

assert_eq!(
item,
Item {
content: " ".into()
}
);
}
Expand Down