Skip to content

Commit

Permalink
skip initial whitespaces after DocType
Browse files Browse the repository at this point in the history
sample_4 test is not enabled yet because of CData
  • Loading branch information
BlueGreenMagick committed Oct 25, 2021
1 parent 5724ece commit c65c0f2
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/events/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ pub enum Event<'a> {
Decl(BytesDecl<'a>),
/// Processing instruction `<?...?>`.
PI(BytesText<'a>),
/// Doctype `<!DOCTYPE...>`.
/// Doctype `<!DOCTYPE ...>`.
DocType(BytesText<'a>),
/// End of XML document.
Eof,
Expand Down
7 changes: 6 additions & 1 deletion src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,12 @@ impl<R: BufRead> Reader<R> {
Ok(Event::CData(BytesText::from_plain(&buf[8..buf.len() - 2])))
} else if uncased_starts_with(buf, b"!DOCTYPE") {
debug_assert!(len >= 8, "Minimum length guaranteed by read_bang_elem");
Ok(Event::DocType(BytesText::from_escaped(&buf[8..])))
let start = buf[8..]
.iter()
.position(|b| !is_whitespace(*b))
.unwrap_or_else(|| buf.len() - 8);
debug_assert!(start < buf.len() - 8, "DocType must have a name");
Ok(Event::DocType(BytesText::from_escaped(&buf[8 + start..])))
} else {
unreachable!("Proper bang start guaranteed by read_bang_elem");
}
Expand Down
2 changes: 1 addition & 1 deletion src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl<W: Write> Writer<W> {
}
Event::Decl(ref e) => self.write_wrapped(b"<?", e, b"?>"),
Event::PI(ref e) => self.write_wrapped(b"<?", e, b"?>"),
Event::DocType(ref e) => self.write_wrapped(b"<!DOCTYPE", e, b">"),
Event::DocType(ref e) => self.write_wrapped(b"<!DOCTYPE ", e, b">"),
Event::Eof => Ok(()),
};
if let Some(i) = self.indent.as_mut() {
Expand Down
2 changes: 1 addition & 1 deletion tests/documents/html5.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
DocType( html)
DocType(html)
Characters(
)
StartElement(a, attr-error: error while parsing attribute at position 7: Attribute value must start with a quote.)
Expand Down
1 change: 1 addition & 0 deletions tests/documents/sample_4_full.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
StartDocument(1.0, utf-8)
DocType(data SYSTEM "abcd.dtd")
StartElement({urn:x}p:data [z=">"])
Whitespace("\n ")
Comment(" abcd &lt; &gt; &amp; ")
Expand Down
11 changes: 6 additions & 5 deletions tests/documents/sample_4_short.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
StartDocument(1.0, utf-8)
DocType(DOCTYPE data SYSTEM "abcd.dtd")
DocType(data SYSTEM "abcd.dtd")
StartElement({urn:x}p:data [z=">"])
Comment( abcd &lt; &gt; &amp; )
StartElement(a)
Characters("test")
Characters(test)
EndElement(a)
StartElement(b)
Characters("kkss\" = ddd\' >")
Characters(kkss" = ddd' >)
EndElement(b)
Characters("<a>ddddd</b>!e3--><!-- ddckx")
CData(<a>ddddd</b>!e3--><!-- ddckx)
StartElement(c)
EndElement(c)
Characters("<![CDATA[zzzz]]>")
CData(<![CDATA[zzzz]]>)
EndElement({urn:x}p:data)
EndDocument
1 change: 0 additions & 1 deletion tests/serde-migrated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ fn test_namespaces() {
}

#[test]
#[ignore] // FIXME
fn test_doctype() {
#[derive(PartialEq, Serialize, Deserialize, Debug)]
struct Envelope {
Expand Down

0 comments on commit c65c0f2

Please sign in to comment.