Skip to content

Commit 358cc58

Browse files
authored
Merge pull request #603 from danjpgriffin/master
Trim next element after DocType
2 parents 5fc695e + d49f2d5 commit 358cc58

File tree

3 files changed

+81
-1
lines changed

3 files changed

+81
-1
lines changed

Changelog.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@
1818

1919
### Bug Fixes
2020

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

27+
[#581]: https://github.com/tafia/quick-xml/pull/581
2328
[#601]: https://github.com/tafia/quick-xml/pull/601
29+
[#603]: https://github.com/tafia/quick-xml/pull/603
2430
[#606]: https://github.com/tafia/quick-xml/pull/606
2531

2632

src/de/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2880,7 +2880,7 @@ impl StartTrimmer {
28802880
#[inline(always)]
28812881
fn trim<'a>(&mut self, event: Event<'a>) -> Option<PayloadEvent<'a>> {
28822882
let (event, trim_next_event) = match event {
2883-
Event::DocType(e) => (PayloadEvent::DocType(e), false),
2883+
Event::DocType(e) => (PayloadEvent::DocType(e), true),
28842884
Event::Start(e) => (PayloadEvent::Start(e), true),
28852885
Event::End(e) => (PayloadEvent::End(e), true),
28862886
Event::Eof => (PayloadEvent::Eof, true),

tests/serde-de.rs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6496,3 +6496,77 @@ mod resolve {
64966496
);
64976497
}
64986498
}
6499+
6500+
/// Tests for https://github.com/tafia/quick-xml/pull/603.
6501+
///
6502+
/// According to <https://www.w3.org/TR/xml11/#NT-prolog> comments,
6503+
/// processing instructions and spaces are possible after XML declaration or DTD.
6504+
/// Their existence should not break deserializing
6505+
///
6506+
/// ```text
6507+
/// [22] prolog ::= XMLDecl Misc* (doctypedecl Misc*)?
6508+
/// [27] Misc ::= Comment | PI | S
6509+
/// ```
6510+
mod xml_prolog {
6511+
use super::*;
6512+
use pretty_assertions::assert_eq;
6513+
use std::collections::HashMap;
6514+
6515+
#[test]
6516+
fn spaces() {
6517+
assert_eq!(
6518+
from_str::<HashMap<(), ()>>(
6519+
r#"
6520+
<?xml version="1.1"?>
6521+
6522+
<!DOCTYPE dict>
6523+
6524+
<doc>
6525+
</doc>
6526+
"#
6527+
)
6528+
.unwrap(),
6529+
HashMap::new()
6530+
);
6531+
}
6532+
6533+
#[test]
6534+
fn comments() {
6535+
assert_eq!(
6536+
from_str::<HashMap<(), ()>>(
6537+
r#"
6538+
<?xml version="1.1"?>
6539+
<!-- comment between xml declaration and doctype -->
6540+
<!-- another comment -->
6541+
<!DOCTYPE dict>
6542+
<!-- comment between doctype and root element -->
6543+
<!-- another comment -->
6544+
<doc>
6545+
</doc>
6546+
"#,
6547+
)
6548+
.unwrap(),
6549+
HashMap::new()
6550+
);
6551+
}
6552+
6553+
#[test]
6554+
fn pi() {
6555+
assert_eq!(
6556+
from_str::<HashMap<(), ()>>(
6557+
r#"
6558+
<?xml version="1.1"?>
6559+
<?pi?>
6560+
<?another pi?>
6561+
<!DOCTYPE dict>
6562+
<?pi?>
6563+
<?another pi?>
6564+
<doc>
6565+
</doc>
6566+
"#,
6567+
)
6568+
.unwrap(),
6569+
HashMap::new()
6570+
);
6571+
}
6572+
}

0 commit comments

Comments
 (0)