Truly independent web browser
LibWeb: Decode JSON documents as UTF-8 by default
Shannon Boothc680b998a897
diff --git a/Libraries/LibWeb/DOM/DocumentLoading.cpp b/Libraries/LibWeb/DOM/DocumentLoading.cppindex 70e90f52336..f39d151d79b 100644--- a/Libraries/LibWeb/DOM/DocumentLoading.cpp+++ b/Libraries/LibWeb/DOM/DocumentLoading.cpp@@ -288,7 +288,15 @@ static WebIDL::ExceptionOr<GC::Ref<DOM::Document>> load_text_document(HTML::Navi// FIXME: Parse as we receive the document data, instead of waiting for the whole document to be fetched first.auto container_document = navigation_params.navigable ? navigation_params.navigable->container_document() : nullptr;auto process_body = GC::create_function(GC::Heap::the(), [document, container_document, url = navigation_params.response->url().value(), response_header_list = navigation_params.response->header_list(), mime = type](ByteBuffer data) {- auto [encoding, confidence] = HTML::run_encoding_sniffing_algorithm(document, data, mime, container_document);+ // 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.+ 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+ encoding = HTML::run_encoding_sniffing_algorithm(document, data, mime, container_document).encoding;dbgln_if(HTML_PARSER_DEBUG, "The encoding sniffing algorithm returned encoding '{}'", encoding);auto run_text_parser = [document, data = move(data), url, response_header_list, encoding = move(encoding)] {diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.iniindex 91268200de3..4c382d8c10d 100644--- a/Tests/LibWeb/TestConfig.ini+++ b/Tests/LibWeb/TestConfig.ini@@ -693,6 +693,7 @@ Text/input/wpt-import/html/canvas/element/manual/imagebitmap/createImageBitmap-eText/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/json-document-utf8.htmlText/input/wpt-import/encoding/utf-32.html[Skipped]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.txtnew file mode 100644index 00000000000..0a9e90b55a8--- /dev/null+++ b/Tests/LibWeb/Text/expected/wpt-import/encoding/json-document-utf8.txt@@ -0,0 +1,9 @@+Harness status: OK++Found 4 tests++4 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;charset=windows-1251 honors the explicit charset\ No newline at end of filediff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/json-document-utf8.html b/Tests/LibWeb/Text/input/wpt-import/encoding/json-document-utf8.htmlnew file mode 100644index 00000000000..18ce3e29d43--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/encoding/json-document-utf8.html@@ -0,0 +1,41 @@+<!DOCTYPE html>+<meta charset="windows-1251">+<title>JSON documents are decoded as UTF-8 by default</title>+<link rel="help" href="https://datatracker.ietf.org/doc/html/rfc8259#section-8.1">+<script src="../resources/testharness.js"></script>+<script src="../resources/testharnessreport.js"></script>+<body>+<script>+const utf8 = '{"text":"\u2603\u00e9"}';++function loadJSON(query) {+ return new Promise((resolve, reject) => {+ const frame = document.createElement("iframe");+ frame.onload = () => {+ const text = frame.contentDocument.body.textContent;+ frame.remove();+ resolve(text);+ };+ frame.onerror = () => { frame.remove(); reject(new Error("load failed")); };+ frame.src = "resources/json-charset.py" + query;+ document.body.appendChild(frame);+ });+}++promise_test(async () => {+ assert_equals(await loadJSON(""), utf8);+}, "application/json with no charset is decoded as UTF-8 under a windows-1251 parent frame");++promise_test(async () => {+ assert_equals(await loadJSON("?charset=utf-8"), utf8);+}, "application/json;charset=utf-8 is decoded as UTF-8");++promise_test(async () => {+ assert_equals(await loadJSON("?type=text/json"), utf8);+}, "text/json with no charset is decoded as UTF-8");++promise_test(async () => {+ assert_not_equals(await loadJSON("?charset=windows-1251"), utf8);+}, "application/json;charset=windows-1251 honors the explicit charset");+</script>+</body>diff --git a/Tests/LibWeb/Text/input/wpt-import/encoding/resources/json-charset.py b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/json-charset.pynew file mode 100644index 00000000000..87cbb5cf2e5--- /dev/null+++ b/Tests/LibWeb/Text/input/wpt-import/encoding/resources/json-charset.py@@ -0,0 +1,8 @@+def main(request, response):+ content_type = request.GET.first(b"type", b"application/json")+ charset = request.GET.first(b"charset", None)+ if charset is not None:+ content_type += b";charset=" + charset+ response.headers.set(b"Content-Type", content_type)+ response.headers.set(b"X-Content-Type-Options", b"nosniff")+ response.content = '{"text":"☃é"}'.encode("utf-8")