Skip to content

Truly independent web browser

LibWeb: Always decode JSON documents as UTF-8

Shannon Booth4782667398ce
diff --git a/Libraries/LibWeb/DOM/DocumentLoading.cpp b/Libraries/LibWeb/DOM/DocumentLoading.cpp
index ca24075f562..d6c82cdb29a 100644
--- a/Libraries/LibWeb/DOM/DocumentLoading.cpp
+++ b/Libraries/LibWeb/DOM/DocumentLoading.cpp
@@ -303,15 +303,21 @@ static WebIDL::ExceptionOr<GC::Ref<DOM::Document>> load_text_document(HTML::Navi
// The rules for how to convert the bytes of the plain text document into actual characters, and the rules for
// actually rendering the text to the user, are defined by the specifications for the computed MIME type of the
// resource (i.e., type).
- // NB: JSON text is UTF-8 [RFC8259]. Like other browsers, honor a BOM or an explicit charset parameter.
+ // NB: JSON text is UTF-8 [RFC8259], whatever a BOM or the charset parameter say.
ByteString encoding;
- if (mime.is_json())
- encoding = HTML::run_bom_sniff(data).value_or(Fetch::Infrastructure::legacy_extract_an_encoding(mime, "UTF-8"sv));
- else
+ Utf16String decoded;
+ if (mime.is_json()) {
+ encoding = "UTF-8"sv;
+ decoded = TextCodec::utf8_decode_to_utf16(data).release_value_but_fixme_should_propagate_errors();
+ } else {
encoding = HTML::run_encoding_sniffing_algorithm(document, data, mime, container_document).encoding;
- dbgln_if(HTML_PARSER_DEBUG, "The encoding sniffing algorithm returned encoding '{}'", encoding);
+ dbgln_if(HTML_PARSER_DEBUG, "The encoding sniffing algorithm returned encoding '{}'", encoding);
+ auto decoder = TextCodec::decoder_for(encoding);
+ VERIFY(decoder.has_value());
+ decoded = TextCodec::decode_to_utf16(data, *decoder).release_value_but_fixme_should_propagate_errors();
+ }
- auto run_text_parser = GC::create_function(GC::Heap::the(), GC::captures(document, move(data), url, response_header_list, move(encoding)), [](auto& document, auto& data, auto& url, auto& response_header_list, auto& encoding) {
+ auto run_text_parser = GC::create_function(GC::Heap::the(), GC::captures(document, move(decoded), url, response_header_list, move(encoding)), [](auto& document, auto& decoded, auto& url, auto& response_header_list, auto& encoding) {
auto parser = HTML::HTMLParser::create_with_open_input_stream(document);
parser->tokenizer().update_insertion_point();
@@ -331,9 +337,6 @@ static WebIDL::ExceptionOr<GC::Ref<DOM::Document>> load_text_document(HTML::Navi
}
parser->tokenizer().switch_to(HTML::HTMLTokenizer::State::PLAINTEXT);
- auto decoder = TextCodec::decoder_for(encoding);
- VERIFY(decoder.has_value());
- auto decoded = TextCodec::decode_to_utf16(data, *decoder).release_value_but_fixme_should_propagate_errors();
parser->tokenizer().insert_input_at_insertion_point(decoded);
parser->tokenizer().insert_eof();
parser->run(url);
diff --git a/Tests/LibWeb/Text/expected/wpt-import/encoding/json-document-utf8.txt b/Tests/LibWeb/Text/expected/wpt-import/encoding/json-document-utf8.txt
index 1b589515009..9dd0d4d9773 100644
--- a/Tests/LibWeb/Text/expected/wpt-import/encoding/json-document-utf8.txt
+++ b/Tests/LibWeb/Text/expected/wpt-import/encoding/json-document-utf8.txt
@@ -2,10 +2,9 @@ Harness status: OK
Found 5 tests
-4 Pass
-1 Fail
+5 Pass
Pass application/json with no charset is decoded as UTF-8 under a windows-1251 parent frame
Pass application/json;charset=utf-8 is decoded as UTF-8
Pass text/json with no charset is decoded as UTF-8
Pass APPLICATION/JSON with no charset is decoded as UTF-8
-Fail application/json;charset=windows-1251 is decoded as UTF-8
\ No newline at end of file
+Pass application/json;charset=windows-1251 is decoded as UTF-8
\ No newline at end of file