LibWeb: Use Content-Type header to set document encoding

Co-authored-by: Shannon Booth <shannon@serenityos.org>
This commit is contained in:
Gingeh 2024-10-20 19:39:50 +11:00 committed by Andrew Kaster
parent 1096b64936
commit 8e342e3e23
Notes: github-actions[bot] 2024-10-23 17:32:00 +00:00
3 changed files with 7 additions and 6 deletions

View file

@ -91,9 +91,9 @@ static WebIDL::ExceptionOr<JS::NonnullGCPtr<DOM::Document>> load_html_document(H
// causes a load event to be fired. // causes a load event to be fired.
else { else {
// FIXME: Parse as we receive the document data, instead of waiting for the whole document to be fetched first. // FIXME: Parse as we receive the document data, instead of waiting for the whole document to be fetched first.
auto process_body = JS::create_heap_function(document->heap(), [document, url = navigation_params.response->url().value()](ByteBuffer data) { auto process_body = JS::create_heap_function(document->heap(), [document, url = navigation_params.response->url().value(), mime_type = navigation_params.response->header_list()->extract_mime_type()](ByteBuffer data) {
Platform::EventLoopPlugin::the().deferred_invoke([document = document, data = move(data), url = url] { Platform::EventLoopPlugin::the().deferred_invoke([document = document, data = move(data), url = url, mime_type] {
auto parser = HTML::HTMLParser::create_with_uncertain_encoding(document, data); auto parser = HTML::HTMLParser::create_with_uncertain_encoding(document, data, mime_type);
parser->run(url); parser->run(url);
}); });
}); });

View file

@ -4373,11 +4373,11 @@ JS::NonnullGCPtr<HTMLParser> HTMLParser::create_for_scripting(DOM::Document& doc
return document.heap().allocate_without_realm<HTMLParser>(document); return document.heap().allocate_without_realm<HTMLParser>(document);
} }
JS::NonnullGCPtr<HTMLParser> HTMLParser::create_with_uncertain_encoding(DOM::Document& document, ByteBuffer const& input) JS::NonnullGCPtr<HTMLParser> HTMLParser::create_with_uncertain_encoding(DOM::Document& document, ByteBuffer const& input, Optional<MimeSniff::MimeType> maybe_mime_type)
{ {
if (document.has_encoding()) if (document.has_encoding())
return document.heap().allocate_without_realm<HTMLParser>(document, input, document.encoding().value().to_byte_string()); return document.heap().allocate_without_realm<HTMLParser>(document, input, document.encoding().value().to_byte_string());
auto encoding = run_encoding_sniffing_algorithm(document, input); auto encoding = run_encoding_sniffing_algorithm(document, input, maybe_mime_type);
dbgln_if(HTML_PARSER_DEBUG, "The encoding sniffing algorithm returned encoding '{}'", encoding); dbgln_if(HTML_PARSER_DEBUG, "The encoding sniffing algorithm returned encoding '{}'", encoding);
return document.heap().allocate_without_realm<HTMLParser>(document, input, encoding); return document.heap().allocate_without_realm<HTMLParser>(document, input, encoding);
} }

View file

@ -12,6 +12,7 @@
#include <LibWeb/HTML/Parser/HTMLTokenizer.h> #include <LibWeb/HTML/Parser/HTMLTokenizer.h>
#include <LibWeb/HTML/Parser/ListOfActiveFormattingElements.h> #include <LibWeb/HTML/Parser/ListOfActiveFormattingElements.h>
#include <LibWeb/HTML/Parser/StackOfOpenElements.h> #include <LibWeb/HTML/Parser/StackOfOpenElements.h>
#include <LibWeb/MimeSniff/MimeType.h>
namespace Web::HTML { namespace Web::HTML {
@ -50,7 +51,7 @@ public:
~HTMLParser(); ~HTMLParser();
static JS::NonnullGCPtr<HTMLParser> create_for_scripting(DOM::Document&); static JS::NonnullGCPtr<HTMLParser> create_for_scripting(DOM::Document&);
static JS::NonnullGCPtr<HTMLParser> create_with_uncertain_encoding(DOM::Document&, ByteBuffer const& input); static JS::NonnullGCPtr<HTMLParser> create_with_uncertain_encoding(DOM::Document&, ByteBuffer const& input, Optional<MimeSniff::MimeType> maybe_mime_type = {});
static JS::NonnullGCPtr<HTMLParser> create(DOM::Document&, StringView input, StringView encoding); static JS::NonnullGCPtr<HTMLParser> create(DOM::Document&, StringView input, StringView encoding);
void run(HTMLTokenizer::StopAtInsertionPoint = HTMLTokenizer::StopAtInsertionPoint::No); void run(HTMLTokenizer::StopAtInsertionPoint = HTMLTokenizer::StopAtInsertionPoint::No);