Skip to content

Truly independent web browser

LibWeb: Set the character encoding of loaded XML documents

Shannon Booth0bdc1f386d04
diff --git a/Libraries/LibWeb/DOM/DocumentLoading.cpp b/Libraries/LibWeb/DOM/DocumentLoading.cpp
index 6c7b2830460..70e90f52336 100644
--- a/Libraries/LibWeb/DOM/DocumentLoading.cpp
+++ b/Libraries/LibWeb/DOM/DocumentLoading.cpp
@@ -203,19 +203,24 @@ static WebIDL::ExceptionOr<GC::Ref<DOM::Document>> load_xml_document(HTML::Navig
content_encoding = *maybe_encoding;
auto process_body = GC::create_function(GC::Heap::the(), [document, url = navigation_params.response->url().value(), response_header_list = navigation_params.response->header_list(), content_encoding = move(content_encoding), mime = type](ByteBuffer data) {
- Optional<TextCodec::Decoder> decoder;
+ Optional<StringView> encoding;
// The actual HTTP headers and other metadata, not the headers as mutated or implied by the algorithms given in this specification,
// are the ones that must be used when determining the character encoding according to the rules given in the above specifications.
if (content_encoding.has_value())
- decoder = TextCodec::decoder_for(content_encoding->bytes_as_string_view());
+ encoding = TextCodec::get_standardized_encoding(content_encoding->bytes_as_string_view());
// https://www.w3.org/TR/xml/#charencoding
// [...] it is a fatal error [...] for an entity which begins with neither a Byte Order Mark nor an encoding
// declaration to use an encoding other than UTF-8.
- if (!decoder.has_value())
- decoder = TextCodec::decoder_for("UTF-8"sv);
- VERIFY(decoder.has_value());
+ if (!encoding.has_value())
+ encoding = "UTF-8"sv;
+
+ // Once the character encoding is established, the document's character encoding must be set to that character encoding.
+ // NB: Decoding lets a BOM override the encoding.
+ auto bom_encoding = HTML::run_bom_sniff(data);
+ document->set_encoding(Utf16String::from_utf8(bom_encoding.has_value() ? bom_encoding->view() : *encoding));
+
// Well-formed XML documents contain only properly encoded characters
- auto source = TextCodec::decode_to_utf8(data, *decoder, TextCodec::ErrorMode::Fatal);
+ auto source = TextCodec::decode_to_utf8(data, *TextCodec::decoder_for(*encoding), TextCodec::ErrorMode::Fatal);
if (source.is_error()) {
// FIXME: Insert error message into the document.
dbgln("Failed to decode XML document: {}", source.error());
diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.ini
index df7ebc8101b..91268200de3 100644
--- a/Tests/LibWeb/TestConfig.ini
+++ b/Tests/LibWeb/TestConfig.ini
@@ -692,6 +692,9 @@ Text/input/wpt-import/html/canvas/element/manual/imagebitmap/createImageBitmap-b
Text/input/wpt-import/html/canvas/element/manual/imagebitmap/createImageBitmap-exif-orientation_none.html
Text/input/HTML/ImageBitmap-retains-dimensions-when-serialized-or-transferred.html
+; Read the contentDocument of same-origin iframes, which a page with a file: URL can't reach.
+Text/input/wpt-import/encoding/utf-32.html
+
[Skipped]
; Needs vertical-rl inline geometry fidelity beyond the binary transpose
Ref/input/wpt-import/css/css-position/position-absolute-in-inline-006.html
diff --git a/Tests/LibWeb/Text/expected/wpt-import/encoding/utf-32.txt b/Tests/LibWeb/Text/expected/wpt-import/encoding/utf-32.txt
new file mode 100644
index 00000000000..2b3dd293035
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/wpt-import/encoding/utf-32.txt
@@ -0,0 +1,13 @@
+Harness status: OK
+
+Found 8 tests
+
+8 Pass
+Pass Expect resources/utf-32-big-endian-bom.html to parse as UTF-8
+Pass Expect resources/utf-32-big-endian-bom.xml to parse as UTF-8
+Pass Expect resources/utf-32-big-endian-nobom.html to parse as UTF-8
+Pass Expect resources/utf-32-big-endian-nobom.xml to parse as UTF-8
+Pass Expect resources/utf-32-little-endian-bom.html to parse as UTF-16LE
+Pass Expect resources/utf-32-little-endian-bom.xml to parse as UTF-16LE
+Pass Expect resources/utf-32-little-endian-nobom.html to parse as UTF-8
+Pass Expect resources/utf-32-little-endian-nobom.xml to parse as UTF-8
\ No newline at end of file
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-big-endian-bom.html b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-big-endian-bom.html
new file mode 100644
index 00000000000..db551fa8947
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-big-endian-bom.html differ
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-big-endian-bom.xml b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-big-endian-bom.xml
new file mode 100644
index 00000000000..c97662aa163
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-big-endian-bom.xml differ
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-big-endian-nobom.html b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-big-endian-nobom.html
new file mode 100644
index 00000000000..fe32ab0408c
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-big-endian-nobom.html differ
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-big-endian-nobom.xml b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-big-endian-nobom.xml
new file mode 100644
index 00000000000..f704501ccae
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-big-endian-nobom.xml differ
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-little-endian-bom.html b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-little-endian-bom.html
new file mode 100644
index 00000000000..432b96f26a0
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-little-endian-bom.html differ
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-little-endian-bom.xml b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-little-endian-bom.xml
new file mode 100644
index 00000000000..f896b511c71
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-little-endian-bom.xml differ
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-little-endian-nobom.html b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-little-endian-nobom.html
new file mode 100644
index 00000000000..2de355aab17
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-little-endian-nobom.html differ
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-little-endian-nobom.xml b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-little-endian-nobom.xml
new file mode 100644
index 00000000000..465f44df5f7
Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-little-endian-nobom.xml differ
diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/utf-32.html b/Tests/LibWeb/Text/input/wpt-import/encoding/utf-32.html
new file mode 100644
index 00000000000..c7fdbe74b68
--- /dev/null
+++ b/Tests/LibWeb/Text/input/wpt-import/encoding/utf-32.html
@@ -0,0 +1,71 @@
+<!DOCTYPE html>
+<meta charset=utf-8>
+<title>Character Decoding: UTF-32 (not supported) subresource of UTF-8 document</title>
+<script src="../resources/testharness.js"></script>
+<script src="../resources/testharnessreport.js"></script>
+<body>
+<script>
+
+// Since UTF-32 is not supported:
+// * HTML resources will use the parent encoding (UTF-8)
+// * XML resources will default to UTF-8
+// ... except for the UTF-32LE-with-BOM case, where the UTF-32
+// BOM will be mistaken for a UTF-16LE BOM (FF FE 00 00), in which
+// case it will be interpreted as UTF-16LE.
+
+const samples = [
+ {file: 'resources/utf-32-big-endian-bom.html',
+ characterSet: 'UTF-8',
+ contentType: 'text/html'
+ },
+ {file: 'resources/utf-32-big-endian-bom.xml',
+ characterSet: 'UTF-8',
+ contentType: 'application/xml'
+ },
+ {file: 'resources/utf-32-big-endian-nobom.html',
+ characterSet: 'UTF-8',
+ contentType: 'text/html'
+ },
+ {file: 'resources/utf-32-big-endian-nobom.xml',
+ characterSet: 'UTF-8',
+ contentType: 'application/xml'
+ },
+
+ {file: 'resources/utf-32-little-endian-bom.html',
+ characterSet: 'UTF-16LE',
+ contentType: 'text/html'
+ },
+ {file: 'resources/utf-32-little-endian-bom.xml',
+ characterSet: 'UTF-16LE',
+ contentType: 'application/xml'
+ },
+ {file: 'resources/utf-32-little-endian-nobom.html',
+ characterSet: 'UTF-8',
+ contentType: 'text/html'
+ },
+ {file: 'resources/utf-32-little-endian-nobom.xml',
+ characterSet: 'UTF-8',
+ contentType: 'application/xml'
+ }
+];
+
+samples.forEach(expected => async_test(t => {
+ const iframe = document.createElement('iframe');
+ iframe.src = expected.file;
+ iframe.onload = t.step_func_done(() => {
+ const doc = iframe.contentDocument;
+ assert_equals(doc.contentType, expected.contentType);
+ assert_equals(doc.characterSet, expected.characterSet);
+ // The following is a little quirky as non-well-formed XML isn't defined in sufficient detail to
+ // be able to use more precise assertions.
+ assert_true(
+ !('dataset' in doc.documentElement) ||
+ doc.documentElement.dataset['parsed'] !== 'yes',
+ 'Should not have parsed as (X)HTML');
+ });
+ document.body.appendChild(iframe);
+ t.add_cleanup(() => iframe.remove());
+}, `Expect ${expected.file} to parse as ${expected.characterSet}`));
+
+</script>
+</body>