quick-xml icon indicating copy to clipboard operation
quick-xml copied to clipboard

Newlines in attributes are lost

Open idubrov opened this issue 7 years ago • 2 comments

Newlines in attributes are not encoded into XML escape sequence and are lost or re-parsing.

(https://stackoverflow.com/questions/2004386/how-to-save-newlines-in-xml-attribute)

idubrov avatar Feb 05 '18 02:02 idubrov

This is indeed looks like a bug.

tafia avatar Mar 01 '18 08:03 tafia

@idubrov Is this what you mean? I've wrote a test code on current master (https://github.com/tafia/quick-xml/commit/15ee0189da4515daebd2e4dfadf1b446958290ba) and made sure that it passed. Am I doing right?

#[test]
fn test_newlines_in_attribute_should_not_be_lost() {
    let mut r = Reader::from_str("<tag1 attr1='line 1\nline 2'></tag1>");
    r.trim_text(true);
    let mut buf = Vec::new();
    match r.read_event(&mut buf) {
        Ok(Event::Start(ref e)) => match e.name() {
            b"tag1" => {
                let v = e.attributes().map(|a| a.unwrap().value).collect::<Vec<_>>();
                assert_eq!(v[0].clone().into_owned(), b"line 1\nline 2");
            }
            _ => (),
        },
        _ => (),
    }
}

TakaakiFuruse avatar Mar 15 '20 07:03 TakaakiFuruse

Fixed long time ago.

Mingun avatar Dec 06 '22 19:12 Mingun