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

Trim next element after DocType #603

Merged
merged 2 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@

### Bug Fixes

- [#603]: Fix a regression from [#581] that an XML comment or a processing
instruction between a <!DOCTYPE> and the root element in the file brokes
deserialization of structs by returning `DeError::ExpectedStart`

### Misc Changes

[#581]: https://github.com/tafia/quick-xml/pull/581
[#601]: https://github.com/tafia/quick-xml/pull/601
[#603]: https://github.com/tafia/quick-xml/pull/603
[#606]: https://github.com/tafia/quick-xml/pull/606


Expand Down
2 changes: 1 addition & 1 deletion src/de/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2880,7 +2880,7 @@ impl StartTrimmer {
#[inline(always)]
fn trim<'a>(&mut self, event: Event<'a>) -> Option<PayloadEvent<'a>> {
let (event, trim_next_event) = match event {
Event::DocType(e) => (PayloadEvent::DocType(e), false),
Event::DocType(e) => (PayloadEvent::DocType(e), true),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment describing, why we need trim here because this is not obvious (otherwise it would be done initially)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Immediately following a Doctype event, the Serde parser expects to read an initial structure from the XML file - it expects a 'Start' tag event. Any comments following the doctype in the stream is parsed to Text/CData events, which leads the parse to fail with an 'ExpectedStart' panic. By skipping (trimming) any Text/CData events the next event the serde deserializer sees will be a Start event, which is always required to start building up a new Struct from the xml.

The Doctype should trim events here just like the Start/End events. Only the CData/Text events should not trim (otherwise any CData/Text following them will be ignored)

Event::Start(e) => (PayloadEvent::Start(e), true),
Event::End(e) => (PayloadEvent::End(e), true),
Event::Eof => (PayloadEvent::Eof, true),
Expand Down
74 changes: 74 additions & 0 deletions tests/serde-de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6496,3 +6496,77 @@ mod resolve {
);
}
}

/// Tests for https://github.com/tafia/quick-xml/pull/603.
///
/// According to <https://www.w3.org/TR/xml11/#NT-prolog> comments,
/// processing instructions and spaces are possible after XML declaration or DTD.
/// Their existence should not break deserializing
///
/// ```text
/// [22] prolog ::= XMLDecl Misc* (doctypedecl Misc*)?
/// [27] Misc ::= Comment | PI | S
/// ```
mod xml_prolog {
use super::*;
use pretty_assertions::assert_eq;
use std::collections::HashMap;

#[test]
fn spaces() {
assert_eq!(
from_str::<HashMap<(), ()>>(
r#"
<?xml version="1.1"?>

<!DOCTYPE dict>

<doc>
</doc>
"#
)
.unwrap(),
HashMap::new()
);
}

#[test]
fn comments() {
assert_eq!(
from_str::<HashMap<(), ()>>(
r#"
<?xml version="1.1"?>
<!-- comment between xml declaration and doctype -->
<!-- another comment -->
<!DOCTYPE dict>
<!-- comment between doctype and root element -->
<!-- another comment -->
<doc>
</doc>
"#,
)
.unwrap(),
HashMap::new()
);
}

#[test]
fn pi() {
assert_eq!(
from_str::<HashMap<(), ()>>(
r#"
<?xml version="1.1"?>
<?pi?>
<?another pi?>
<!DOCTYPE dict>
<?pi?>
<?another pi?>
<doc>
</doc>
"#,
)
.unwrap(),
HashMap::new()
);
}
}