Whitespaces around character entities
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Newbie friendliness
- 35/100
Research direction
Start by reading the Config::trim_text behavior and the deserializer path that handles text and decoded character entities; the report includes a reproducer using NsReader and quick_xml::de::Deserializer::borrowing. Check existing tests for trimming and entity decoding, then determine whether a configuration option can preserve spaces around entities while trimming only leading and trailing text. Done means the reproducer retains those internal spaces without losing global edge trimming.
Written by the indexing model from the issue text.
Description
I maintain a large codebase that used quick-xml for years across hundreds of structs.
After switching from 0.37.5 to 0.38, I hit the already well-known whitespace issues. 0.38.1 introduced Config::trim_text which saved me from touching thousands of fields, but now I found the next caveat: XML character entities make trim_text(true) trim more than needed:
#!/usr/bin/env cargo
---
[dependencies]
serde = { version = "1", features = ["derive"] }
quick-xml = { version = "0.38", features = ["serialize"] }
quick-xml-old = { package = "quick-xml", version = "0.37", features = ["serialize"] }
---
use quick_xml::reader::NsReader;
use serde::Deserialize;
use std::result::Result;
#[derive(Debug, Deserialize)]
struct Root {
bar: String,
}
pub fn from_str_trim_text<'de, T>(s: &'de str) -> Result<T, Box<dyn std::error::Error>>
where
T: Deserialize<'de>,
{
let mut reader = NsReader::from_str(s);
let config = reader.config_mut();
config.trim_text(true); // Trims also spaces around decoded &
config.expand_empty_elements = true;
let mut de = quick_xml::de::Deserializer::borrowing(reader);
Ok(T::deserialize(&mut de)?)
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let xml = r#"
<root>
<bar>
Foo & Bar & Baz Qux Quux
</bar>
</root>"#;
let old: Root = quick_xml_old::de::from_str(xml)?;
let new: Root = quick_xml::de::from_str(xml)?;
let new_trimming: Root = from_str_trim_text(xml)?;
println!("Old: {}", old.bar); // "Foo & Bar & Baz Qux Quux"
println!("New: {}", new.bar); // "\n Foo & Bar & Baz Qux Quux\n "
println!("New trimming: {}", new_trimming.bar); // "Foo&Bar&Baz Qux Quux"
Ok(())
}
(cargo +nightly -Zscript example.rs)
I know this is documented behavior (thank you), so I'm not calling it a bug. I’m looking for a solution to preserve "internal" spaces (e.g, around the decoded entities) while still trimming leading/trailing whitespace globally.
The only workaround that comes to my mind atm is either introducing a newtype or using deserialize_with, which are both rather error-prone in a mature codebase like mine and require tons of work.
If there is no existing solution right now, I could try to implement a feature (e.g., a new config option?) if the maintainers think it is a good idea.
- Dominant language
- Rust
- Stars
- 1.6k
- Forks
- 299
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 9
Getting set up
- No Dockerfile or Docker Compose file
- No pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from tafia/quick-xml
-
Parser accepts an empty element or attribute namePossibly taken @ychampion claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
Maintainers usually reply within 1 day
-
DecodingReader::set_encoding panics when the XML declaration is longer than 64 bytesPossibly taken @emschwartz claimed this 4 days ago. Openbug encoding
Difficulty 4/5 3-5 days Newbie friendliness 30/100
tafia/quick-xml#1040 · 1 comment ·
Maintainers usually reply within 1 day
-
encoding optimization
Difficulty 4/5 3-5 days Newbie friendliness 55/100
tafia/quick-xml#1039 · 1 comment ·
Maintainers usually reply within 1 day
-
optimization question
Difficulty 4/5 3-5 days Newbie friendliness 52/100
Maintainers usually reply within 1 day
-
enhancement
Difficulty 4/5 1-2 days Newbie friendliness 53/100
tafia/quick-xml#1035 · 1 comment ·
Maintainers usually reply within 1 day
Similar issues
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
storytold/photocraft#14822 ·
Maintainers usually reply within 1 day
-
bug css
Difficulty 2/5 1-3 hours Newbie friendliness 66/100
dginev/latexml-oxide#853 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 2 days
-
[Bug] export_build_environment admits its output directory only after the export ran in the build computerPossibly taken @viniciusdsandrade claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day