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.cppindex 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::Navigcontent_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.iniindex 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-bText/input/wpt-import/html/canvas/element/manual/imagebitmap/createImageBitmap-exif-orientation_none.htmlText/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 transposeRef/input/wpt-import/css/css-position/position-absolute-in-inline-006.htmldiff --git a/Tests/LibWeb/Text/expected/wpt-import/encoding/utf-32.txt b/Tests/LibWeb/Text/expected/wpt-import/encoding/utf-32.txtnew file mode 100644index 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 filediff --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.htmlnew file mode 100644index 00000000000..db551fa8947Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-big-endian-bom.html differdiff --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.xmlnew file mode 100644index 00000000000..c97662aa163Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-big-endian-bom.xml differdiff --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.htmlnew file mode 100644index 00000000000..fe32ab0408cBinary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-big-endian-nobom.html differdiff --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.xmlnew file mode 100644index 00000000000..f704501ccaeBinary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-big-endian-nobom.xml differdiff --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.htmlnew file mode 100644index 00000000000..432b96f26a0Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-little-endian-bom.html differdiff --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.xmlnew file mode 100644index 00000000000..f896b511c71Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-little-endian-bom.xml differdiff --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.htmlnew file mode 100644index 00000000000..2de355aab17Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-little-endian-nobom.html differdiff --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.xmlnew file mode 100644index 00000000000..465f44df5f7Binary files /dev/null and b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/utf-32-little-endian-nobom.xml differdiff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/utf-32.html b/Tests/LibWeb/Text/input/wpt-import/encoding/utf-32.htmlnew file mode 100644index 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>