Skip to content

Truly independent web browser

LibWeb: Let a BOM decide an XML document's encoding

Shannon Booth2e0d446a2133
diff --git a/Libraries/LibWeb/DOM/DocumentLoading.cpp b/Libraries/LibWeb/DOM/DocumentLoading.cpp
index 69a8ef55cb9..f83058634e8 100644
--- a/Libraries/LibWeb/DOM/DocumentLoading.cpp
+++ b/Libraries/LibWeb/DOM/DocumentLoading.cpp
@@ -84,18 +84,16 @@ static void convert_to_xml_error_document(DOM::Document& document, Utf16String e
bool build_xml_document(DOM::Document& document, ByteBuffer const& data, Optional<StringView> content_encoding)
{
- Optional<TextCodec::Decoder&> decoder;
+ Optional<TextCodec::Decoder&> decoder = TextCodec::bom_sniff_to_decoder(data);
// 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())
+ if (!decoder.has_value() && content_encoding.has_value())
decoder = TextCodec::decoder_for(*content_encoding);
- if (!decoder.has_value()) {
- // 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.
- auto bom_encoding = HTML::run_bom_sniff(data);
- decoder = TextCodec::decoder_for(bom_encoding.value_or("UTF-8"));
- }
+ // 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());
// Well-formed XML documents contain only properly encoded characters
auto source_or_error = decoder->to_utf8(data, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Fatal);
@@ -203,18 +201,16 @@ 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(), content_encoding = move(content_encoding), mime = type](ByteBuffer data) {
- Optional<TextCodec::Decoder&> decoder;
+ Optional<TextCodec::Decoder&> decoder = TextCodec::bom_sniff_to_decoder(data);
// 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())
+ if (!decoder.has_value() && content_encoding.has_value())
decoder = TextCodec::decoder_for(content_encoding->bytes_as_string_view());
- if (!decoder.has_value()) {
- // 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.
- auto bom_encoding = HTML::run_bom_sniff(data);
- decoder = TextCodec::decoder_for(bom_encoding.value_or("UTF-8"));
- }
+ // 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());
// Well-formed XML documents contain only properly encoded characters
auto source = decoder->to_utf8(data, TextCodec::IgnoreBOM::No, TextCodec::ErrorMode::Fatal);
diff --git a/Tests/LibWeb/Text/expected/XML/byte-order-mark-overrides-charset.txt b/Tests/LibWeb/Text/expected/XML/byte-order-mark-overrides-charset.txt
new file mode 100644
index 00000000000..b6fedbc76b1
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/XML/byte-order-mark-overrides-charset.txt
@@ -0,0 +1 @@
+Text: é
diff --git a/Tests/LibWeb/Text/input/XML/byte-order-mark-overrides-charset.html b/Tests/LibWeb/Text/input/XML/byte-order-mark-overrides-charset.html
new file mode 100644
index 00000000000..199e0a53e5c
--- /dev/null
+++ b/Tests/LibWeb/Text/input/XML/byte-order-mark-overrides-charset.html
@@ -0,0 +1,24 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<script>
+ asyncTest(async done => {
+ const utf8AsLatin1 = string => unescape(encodeURIComponent(string));
+ const xhtml = `<html xmlns="http://www.w3.org/1999/xhtml"><body><p id="p">é</p><script>parent.postMessage(document.getElementById("p").textContent, "*");</` + `script></body></html>`;
+
+ const url = await httpTestServer().createEcho("GET", "/byte-order-mark-overrides-charset.xhtml", {
+ status: 200,
+ headers: { "Content-Type": "application/xhtml+xml; charset=windows-1252" },
+ body: btoa("\xEF\xBB\xBF" + utf8AsLatin1(xhtml)),
+ body_encoding: "base64",
+ });
+
+ window.addEventListener("message", event => {
+ println(`Text: ${event.data}`);
+ done();
+ }, { once: true });
+
+ const frame = document.createElement("iframe");
+ frame.src = url;
+ document.body.appendChild(frame);
+ });
+</script>