Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

End-of-Line Handling seems not confront to xml specification #806

Open
yorkz1994 opened this issue Sep 24, 2024 · 1 comment
Open

End-of-Line Handling seems not confront to xml specification #806

yorkz1994 opened this issue Sep 24, 2024 · 1 comment

Comments

@yorkz1994
Copy link

yorkz1994 commented Sep 24, 2024

Take this xml content for example, note: \r\n is used for line break.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<t>ABC
DEF</t>

And with below code to print Text event:

fn main() {
    let buf = BufReader::new(File::open("a.xml").unwrap());
    let mut read = Reader::from_reader(buf);
    let mut buf = Vec::new();
    loop {
        buf.clear();
        match read.read_event_into(&mut buf) {
            Ok(Event::Eof) => break,
            Ok(Event::Text(t)) => {
                let s = t.unescape().unwrap().to_string();
                println!("{s:?}");
            }
            Err(e) => panic!("{e}"),
            _ => {}
        }
    }
}

Output:

"\r\n"
"ABC\r\nDEF

Expected output should not have \r:

"\n"
"ABC\nDEF

Because as xml specification

2.11 End-of-Line Handling
XML parsed entities are often stored in computer files which, for editing convenience, are organized into lines. These lines are typically separated by some combination of the characters CARRIAGE RETURN (#xD) and LINE FEED (#xA).

To simplify the tasks of applications, the XML processor MUST behave as if it normalized all line breaks in external parsed entities (including the document entity) on input, before parsing, by translating both the two-character sequence #xD #xA and any #xD that is not followed by #xA to a single #xA character.

I found golang xml library has below code for handling it.

// We must rewrite unescaped \r and \r\n into \n.
		if b == '\r' {
			d.buf.WriteByte('\n')
		} else if b1 == '\r' && b == '\n' {
			// Skip \r\n--we already wrote \n.
		} else {
			d.buf.WriteByte(b)
		}
@Mingun
Copy link
Collaborator

Mingun commented Sep 24, 2024

Yes, normalization of input does not performed at all. Related issues is #371, #670 and #749

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants