mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-26 01:20:25 +00:00
LibWeb: Change Document's m_encoding to Optional<String>
This modifies the Document class to use Optional<String> for the encoding. If the encoding is unknown, the Optional will not have a value. It also implements the has_encoding() and encoding_or_default() instance methods, the latter of which will return "UTF-8" as a fallback if no encoding is present. The usage of Optional<String> instead of the null string is part of an effort to explicitly indicate that a string could not have a value. This also modifies the former callers of encoding() to use encoding_or_default(). Furthermore, the encoding will now only be set if it is actually known, rather than just guessed by earlier code.
This commit is contained in:
parent
a7681dbeea
commit
67a9ebc817
Notes:
sideshowbarker
2024-07-18 17:48:39 +09:00
Author: https://github.com/MaxWipfli Commit: https://github.com/SerenityOS/serenity/commit/67a9ebc8176 Pull-request: https://github.com/SerenityOS/serenity/pull/7055 Issue: https://github.com/SerenityOS/serenity/issues/6910 Reviewed-by: https://github.com/Dexesttp Reviewed-by: https://github.com/awesomekling Reviewed-by: https://github.com/linusg
2 changed files with 11 additions and 9 deletions
|
@ -237,13 +237,15 @@ public:
|
||||||
const String& content_type() const { return m_content_type; }
|
const String& content_type() const { return m_content_type; }
|
||||||
void set_content_type(const String& content_type) { m_content_type = content_type; }
|
void set_content_type(const String& content_type) { m_content_type = content_type; }
|
||||||
|
|
||||||
const String& encoding() const { return m_encoding; }
|
bool has_encoding() const { return m_encoding.has_value(); }
|
||||||
void set_encoding(const String& encoding) { m_encoding = encoding; }
|
const Optional<String>& encoding() const { return m_encoding; }
|
||||||
|
String encoding_or_default() const { return m_encoding.value_or("UTF-8"); }
|
||||||
|
void set_encoding(const Optional<String>& encoding) { m_encoding = encoding; }
|
||||||
|
|
||||||
// NOTE: These are intended for the JS bindings
|
// NOTE: These are intended for the JS bindings
|
||||||
const String& character_set() const { return encoding(); }
|
String character_set() const { return encoding_or_default(); }
|
||||||
const String& charset() const { return encoding(); }
|
String charset() const { return encoding_or_default(); }
|
||||||
const String& input_encoding() const { return encoding(); }
|
String input_encoding() const { return encoding_or_default(); }
|
||||||
|
|
||||||
bool ready_for_post_load_tasks() const { return m_ready_for_post_load_tasks; }
|
bool ready_for_post_load_tasks() const { return m_ready_for_post_load_tasks; }
|
||||||
void set_ready_for_post_load_tasks(bool ready) { m_ready_for_post_load_tasks = ready; }
|
void set_ready_for_post_load_tasks(bool ready) { m_ready_for_post_load_tasks = ready; }
|
||||||
|
@ -327,7 +329,7 @@ private:
|
||||||
|
|
||||||
String m_ready_state { "loading" };
|
String m_ready_state { "loading" };
|
||||||
String m_content_type { "application/xml" };
|
String m_content_type { "application/xml" };
|
||||||
String m_encoding { "UTF-8" };
|
Optional<String> m_encoding;
|
||||||
|
|
||||||
bool m_ready_for_post_load_tasks { false };
|
bool m_ready_for_post_load_tasks { false };
|
||||||
|
|
||||||
|
|
|
@ -113,7 +113,7 @@ bool FrameLoader::parse_document(DOM::Document& document, const ByteBuffer& data
|
||||||
{
|
{
|
||||||
auto& mime_type = document.content_type();
|
auto& mime_type = document.content_type();
|
||||||
if (mime_type == "text/html" || mime_type == "image/svg+xml") {
|
if (mime_type == "text/html" || mime_type == "image/svg+xml") {
|
||||||
HTML::HTMLDocumentParser parser(document, data, document.encoding());
|
HTML::HTMLDocumentParser parser(document, data, document.encoding_or_default());
|
||||||
parser.run(document.url());
|
parser.run(document.url());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -251,12 +251,12 @@ void FrameLoader::resource_did_load()
|
||||||
if (resource()->has_encoding()) {
|
if (resource()->has_encoding()) {
|
||||||
dbgln("This content has MIME type '{}', encoding '{}'", resource()->mime_type(), resource()->encoding().value());
|
dbgln("This content has MIME type '{}', encoding '{}'", resource()->mime_type(), resource()->encoding().value());
|
||||||
} else {
|
} else {
|
||||||
dbgln("This content has MIME type '{}', encoding unknown (defaulting to 'utf-8')", resource()->mime_type());
|
dbgln("This content has MIME type '{}', encoding unknown", resource()->mime_type());
|
||||||
}
|
}
|
||||||
|
|
||||||
auto document = DOM::Document::create();
|
auto document = DOM::Document::create();
|
||||||
document->set_url(url);
|
document->set_url(url);
|
||||||
document->set_encoding(resource()->encoding().value_or("utf-8"));
|
document->set_encoding(resource()->encoding());
|
||||||
document->set_content_type(resource()->mime_type());
|
document->set_content_type(resource()->mime_type());
|
||||||
|
|
||||||
frame().set_document(document);
|
frame().set_document(document);
|
||||||
|
|
Loading…
Reference in a new issue