summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorMax Wipfli <mail@maxwipfli.ch>2021-05-12 10:21:00 +0200
committerAndreas Kling <kling@serenityos.org>2021-05-18 21:02:07 +0200
commita7681dbeea882456915e01a12095429460a0df30 (patch)
tree3c1e0b3c75118ab4a6c5b84f824d2148d802ddee /Userland
parentce6d6706a677de6091676e1affdb121ab6775717 (diff)
downloadserenity-a7681dbeea882456915e01a12095429460a0df30.zip
LibWeb: Change Resource's m_encoding to Optional<String>
This modifies the Resource class to use Optional<String> for the encoding. If the encoding is unknown, the Optional will not have a value (instead of using the null state of the String class). It also implements a has_encoding() instance method and modifies the callers of Resource::encoding() to use the new API.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibWeb/Loader/FrameLoader.cpp8
-rw-r--r--Userland/Libraries/LibWeb/Loader/Resource.cpp4
-rw-r--r--Userland/Libraries/LibWeb/Loader/Resource.h6
3 files changed, 11 insertions, 7 deletions
diff --git a/Userland/Libraries/LibWeb/Loader/FrameLoader.cpp b/Userland/Libraries/LibWeb/Loader/FrameLoader.cpp
index 01ee018924..00c7826f56 100644
--- a/Userland/Libraries/LibWeb/Loader/FrameLoader.cpp
+++ b/Userland/Libraries/LibWeb/Loader/FrameLoader.cpp
@@ -248,11 +248,15 @@ void FrameLoader::resource_did_load()
}
m_redirects_count = 0;
- dbgln("I believe this content has MIME type '{}', encoding '{}'", resource()->mime_type(), resource()->encoding());
+ if (resource()->has_encoding()) {
+ dbgln("This content has MIME type '{}', encoding '{}'", resource()->mime_type(), resource()->encoding().value());
+ } else {
+ dbgln("This content has MIME type '{}', encoding unknown (defaulting to 'utf-8')", resource()->mime_type());
+ }
auto document = DOM::Document::create();
document->set_url(url);
- document->set_encoding(resource()->encoding());
+ document->set_encoding(resource()->encoding().value_or("utf-8"));
document->set_content_type(resource()->mime_type());
frame().set_document(document);
diff --git a/Userland/Libraries/LibWeb/Loader/Resource.cpp b/Userland/Libraries/LibWeb/Loader/Resource.cpp
index 0892cc61c9..a63ed7a332 100644
--- a/Userland/Libraries/LibWeb/Loader/Resource.cpp
+++ b/Userland/Libraries/LibWeb/Loader/Resource.cpp
@@ -85,14 +85,12 @@ void Resource::did_load(Badge<ResourceLoader>, ReadonlyBytes data, const HashMap
m_mime_type = Core::guess_mime_type_based_on_filename(url().path());
}
+ m_encoding = {};
if (content_type.has_value()) {
auto encoding = encoding_from_content_type(content_type.value());
if (encoding.has_value()) {
dbgln_if(RESOURCE_DEBUG, "Set encoding '{}' from Content-Type", encoding.has_value());
m_encoding = encoding.value();
- } else {
- // FIXME: This doesn't seem nice.
- m_encoding = "utf-8";
}
}
diff --git a/Userland/Libraries/LibWeb/Loader/Resource.h b/Userland/Libraries/LibWeb/Loader/Resource.h
index 1fca238e3e..7a0b4c81f8 100644
--- a/Userland/Libraries/LibWeb/Loader/Resource.h
+++ b/Userland/Libraries/LibWeb/Loader/Resource.h
@@ -52,7 +52,8 @@ public:
void register_client(Badge<ResourceClient>, ResourceClient&);
void unregister_client(Badge<ResourceClient>, ResourceClient&);
- const String& encoding() const { return m_encoding; }
+ bool has_encoding() const { return m_encoding.has_value(); }
+ const Optional<String>& encoding() const { return m_encoding; }
const String& mime_type() const { return m_mime_type; }
void for_each_client(Function<void(ResourceClient&)>);
@@ -70,7 +71,8 @@ private:
bool m_loaded { false };
bool m_failed { false };
String m_error;
- String m_encoding;
+ Optional<String> m_encoding;
+
String m_mime_type;
HashMap<String, String, CaseInsensitiveStringTraits> m_response_headers;
Optional<u32> m_status_code;