Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

encoding_for_document_xml_declaration now matches 6-byte <?x patterns per the HTML Standard and Added 6 negative tests

BibekPathakf917e513a058
diff --git a/moli-encoding/src/document.rs b/moli-encoding/src/document.rs
index e19586380..42b66dbc3 100644
--- a/moli-encoding/src/document.rs
+++ b/moli-encoding/src/document.rs
@@ -236,26 +236,29 @@ fn encoding_for_document_bom(bytes: &[u8]) -> Option<&'static Encoding> {
/// Detect BOM-less UTF-16LE/BE documents that begin with an XML declaration.
///
-/// The WHATWG encoding standard's sniffing algorithm requires detecting UTF-16LE
-/// and UTF-16BE byte patterns for `<?` (the start of an XML declaration) even
-/// without BOM or transport charset, so that the document's encoding can be
-/// determined correctly.
+/// The HTML Standard requires the following case-sensitive six-byte patterns:
+/// UTF-16LE: 3C 00 3F 00 78 00 ("\<?x")
+/// UTF-16BE: 00 3C 00 3F 00 78 ("\<?x")
+///
+/// These match the start of `<?xml` without requiring a BOM or transport charset.
fn encoding_for_document_xml_declaration(bytes: &[u8]) -> Option<&'static Encoding> {
- // UTF-16LE: 0x3C 0x00 0x3F 0x00 = "<?" in UTF-16LE
- if bytes.len() >= 4
+ if bytes.len() >= 6
&& bytes[0] == 0x3C
&& bytes[1] == 0x00
&& bytes[2] == 0x3F
&& bytes[3] == 0x00
+ && bytes[4] == 0x78
+ && bytes[5] == 0x00
{
return Some(encoding_rs::UTF_16LE);
}
- // UTF-16BE: 0x00 0x3C 0x00 0x3F = "<?" in UTF-16BE
- if bytes.len() >= 4
+ if bytes.len() >= 6
&& bytes[0] == 0x00
&& bytes[1] == 0x3C
&& bytes[2] == 0x00
&& bytes[3] == 0x3F
+ && bytes[4] == 0x00
+ && bytes[5] == 0x78
{
return Some(encoding_rs::UTF_16BE);
}
diff --git a/moli-encoding/src/tests.rs b/moli-encoding/src/tests.rs
index bb47b731c..4d0dd6e71 100644
--- a/moli-encoding/src/tests.rs
+++ b/moli-encoding/src/tests.rs
@@ -523,3 +523,57 @@ fn bom_less_utf16be_xml_declaration_is_detected() {
assert_eq!(encoding, "UTF-16BE");
assert!(text.contains("hi"), "decoded as {text:?}");
}
+
+#[test]
+fn bom_less_utf16le_non_xml_not_detected() {
+ let input = utf16le_bytes("<?y>");
+
+ let (_, encoding) = decode_html_document(&input, &[]);
+
+ assert_eq!(encoding, "windows-1252");
+}
+
+#[test]
+fn bom_less_utf16le_uppercase_x_not_detected() {
+ let input = utf16le_bytes("<?X>");
+
+ let (_, encoding) = decode_html_document(&input, &[]);
+
+ assert_eq!(encoding, "windows-1252");
+}
+
+#[test]
+fn bom_less_utf16le_truncated_not_detected() {
+ let input: Vec<u8> = vec![0x3C, 0x00, 0x3F, 0x00];
+
+ let (_, encoding) = decode_html_document(&input, &[]);
+
+ assert_eq!(encoding, "windows-1252");
+}
+
+#[test]
+fn bom_less_utf16be_non_xml_not_detected() {
+ let input = utf16be_bytes("<?y>");
+
+ let (_, encoding) = decode_html_document(&input, &[]);
+
+ assert_eq!(encoding, "windows-1252");
+}
+
+#[test]
+fn bom_less_utf16be_uppercase_x_not_detected() {
+ let input = utf16be_bytes("<?X>");
+
+ let (_, encoding) = decode_html_document(&input, &[]);
+
+ assert_eq!(encoding, "windows-1252");
+}
+
+#[test]
+fn bom_less_utf16be_truncated_not_detected() {
+ let input: Vec<u8> = vec![0x00, 0x3C, 0x00, 0x3F];
+
+ let (_, encoding) = decode_html_document(&input, &[]);
+
+ assert_eq!(encoding, "windows-1252");
+}