Repository: tafia/quick-xml
Pull request #242 (2020-11-13 15:16:21)

Accept html5 doctype
Make doctype check case-insensitive


Patch:
diff --git a/src/reader.rs b/src/reader.rs
index bb6eb8c..938f9f2 100644
--- a/src/reader.rs
+++ b/src/reader.rs
@@ -375,7 +375,7 @@ impl<B: BufRead> Reader<B> {
                         &buf[buf_start + 8..buf.len() - 2],
                     )))
                 }
-                b"DOCTYPE" => {
+                x if x.eq_ignore_ascii_case(b"DOCTYPE") => {
                     let mut count = buf.iter().skip(buf_start).filter(|&&b| b == b'<').count();
                     while count > 0 {
                         buf.push(b'>');


Test patch:
diff --git a/tests/documents/html5.html b/tests/documents/html5.html
new file mode 100644
index 0000000..c0fc1df
--- /dev/null
+++ b/tests/documents/html5.html
@@ -0,0 +1,3 @@
+<!doctype html>
+<a href=foo.html>Hey</a>
+&nbsp;
diff --git a/tests/documents/html5.txt b/tests/documents/html5.txt
new file mode 100644
index 0000000..5f0d6bc
--- /dev/null
+++ b/tests/documents/html5.txt
@@ -0,0 +1,8 @@
+DocType( html)
+Characters(
+)
+StartElement(a, attr-error: error while parsing attribute at position 7: Attribute value must start with a quote.)
+Characters(Hey)
+EndElement(a)
+InvalidUtf8([10, 38, 110, 98, 115, 112, 59, 10]; invalid utf-8 sequence of 1 bytes from index 1)
+EndDocument
diff --git a/tests/xmlrs_reader_tests.rs b/tests/xmlrs_reader_tests.rs
index 10df35c..76e8ac7 100644
--- a/tests/xmlrs_reader_tests.rs
+++ b/tests/xmlrs_reader_tests.rs
@@ -40,6 +40,15 @@ fn sample_2_full() {
     );
 }
 
+#[test]
+fn html5() {
+    test(
+        include_bytes!("documents/html5.html"),
+        include_bytes!("documents/html5.txt"),
+        false,
+    );
+}
+
 // #[test]
 // fn sample_3_short() {
 //     test(
@@ -160,7 +169,7 @@ fn issue_93_large_characters_in_entity_references() {
         r#"<hello>&𤶼;</hello>"#.as_bytes(),
         r#"
             |StartElement(hello)
-            |1:10 Error while escaping character at range 1..5: Unrecognized escape symbol: Ok("𤶼")
+            |1:10 FailedUnescape([38, 240, 164, 182, 188, 59]; Error while escaping character at range 1..5: Unrecognized escape symbol: Ok("𤶼"))
             |EndElement(hello)
             |EndDocument
         "#
@@ -383,9 +392,12 @@ fn xmlrs_display(opt_event: &Result<(Option<&[u8]>, Event)>) -> String {
         Ok((_, Event::Comment(ref e))) => format!("Comment({})", from_utf8(e).unwrap()),
         Ok((_, Event::CData(ref e))) => format!("CData({})", from_utf8(e).unwrap()),
         Ok((_, Event::Text(ref e))) => match e.unescaped() {
-            Ok(c) => format!("Characters({})", from_utf8(&*c).unwrap()),
-            Err(ref e) => format!("{}", e),
-        },
+            Ok(c) => match from_utf8(&*c) {
+                Ok(c) => format!("Characters({})", c),
+                Err(ref err) => format!("InvalidUtf8({:?}; {})", e.escaped(), err),
+            }
+            Err(ref err) => format!("FailedUnescape({:?}; {})", e.escaped(), err)
+        }
         Ok((_, Event::Decl(ref e))) => {
             let version_cow = e.version().unwrap();
             let version = from_utf8(version_cow.as_ref()).unwrap();