mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 23:37:35 +00:00
LibTextCodec+Everywhere: Return Optional<Decoder&> from decoder_for()
This commit is contained in:
parent
2f4c463920
commit
f2a9426885
16 changed files with 42 additions and 39 deletions
|
@ -199,8 +199,8 @@ Tokenizer::Tokenizer(StringView input, StringView encoding)
|
|||
{
|
||||
// https://www.w3.org/TR/css-syntax-3/#css-filter-code-points
|
||||
auto filter_code_points = [](StringView input, auto encoding) -> ErrorOr<String> {
|
||||
auto* decoder = TextCodec::decoder_for(encoding);
|
||||
VERIFY(decoder);
|
||||
auto decoder = TextCodec::decoder_for(encoding);
|
||||
VERIFY(decoder.has_value());
|
||||
|
||||
StringBuilder builder { input.length() };
|
||||
bool last_was_carriage_return = false;
|
||||
|
|
|
@ -15,7 +15,7 @@ namespace Web::Encoding {
|
|||
WebIDL::ExceptionOr<JS::NonnullGCPtr<TextDecoder>> TextDecoder::construct_impl(JS::Realm& realm, DeprecatedFlyString encoding)
|
||||
{
|
||||
auto decoder = TextCodec::decoder_for(encoding);
|
||||
if (!decoder)
|
||||
if (!decoder.has_value())
|
||||
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, DeprecatedString::formatted("Invalid encoding {}", encoding) };
|
||||
|
||||
return MUST_OR_THROW_OOM(realm.heap().allocate<TextDecoder>(realm, realm, *decoder, move(encoding), false, false));
|
||||
|
|
|
@ -514,7 +514,7 @@ void HTMLScriptElement::resource_did_load()
|
|||
// If the resource has an explicit encoding (i.e from a HTTP Content-Type header)
|
||||
// we have to re-encode it to UTF-8.
|
||||
if (resource()->has_encoding()) {
|
||||
if (auto* codec = TextCodec::decoder_for(resource()->encoding().value())) {
|
||||
if (auto codec = TextCodec::decoder_for(resource()->encoding().value()); codec.has_value()) {
|
||||
data = codec->to_utf8(data).to_byte_buffer();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2798,8 +2798,8 @@ HTMLTokenizer::HTMLTokenizer()
|
|||
|
||||
HTMLTokenizer::HTMLTokenizer(StringView input, DeprecatedString const& encoding)
|
||||
{
|
||||
auto* decoder = TextCodec::decoder_for(encoding);
|
||||
VERIFY(decoder);
|
||||
auto decoder = TextCodec::decoder_for(encoding);
|
||||
VERIFY(decoder.has_value());
|
||||
m_decoded_input = decoder->to_utf8(input);
|
||||
m_utf8_view = Utf8View(m_decoded_input);
|
||||
m_utf8_iterator = m_utf8_view.begin();
|
||||
|
|
|
@ -1420,7 +1420,7 @@ JS_DEFINE_NATIVE_FUNCTION(Window::atob)
|
|||
// The bytes object might contain bytes greater than 128, encode them in UTF8
|
||||
// NOTE: Any 8-bit encoding -> utf-8 decoder will work for this
|
||||
auto text_decoder = TextCodec::decoder_for("windows-1252"sv);
|
||||
VERIFY(text_decoder);
|
||||
VERIFY(text_decoder.has_value());
|
||||
auto text = text_decoder->to_utf8(decoded.release_value());
|
||||
|
||||
return JS::PrimitiveString::create(vm, DeprecatedString(text));
|
||||
|
|
|
@ -161,8 +161,8 @@ WebIDL::ExceptionOr<DeprecatedString> WorkerGlobalScope::atob(DeprecatedString c
|
|||
|
||||
// 3. Return decodedData.
|
||||
// decode_base64() returns a byte string. LibJS uses UTF-8 for strings. Use Latin1Decoder to convert bytes 128-255 to UTF-8.
|
||||
auto* decoder = TextCodec::decoder_for("windows-1252"sv);
|
||||
VERIFY(decoder);
|
||||
auto decoder = TextCodec::decoder_for("windows-1252"sv);
|
||||
VERIFY(decoder.has_value());
|
||||
return decoder->to_utf8(decoded_data.value());
|
||||
}
|
||||
|
||||
|
|
|
@ -83,7 +83,7 @@ static DeprecatedString mime_type_from_content_type(DeprecatedString const& cont
|
|||
|
||||
static bool is_valid_encoding(StringView encoding)
|
||||
{
|
||||
return TextCodec::decoder_for(encoding);
|
||||
return TextCodec::decoder_for(encoding).has_value();
|
||||
}
|
||||
|
||||
void Resource::did_load(Badge<ResourceLoader>, ReadonlyBytes data, HashMap<DeprecatedString, DeprecatedString, CaseInsensitiveStringTraits> const& headers, Optional<u32> status_code)
|
||||
|
|
|
@ -219,10 +219,10 @@ DeprecatedString XMLHttpRequest::get_text_response() const
|
|||
charset = "UTF-8"sv;
|
||||
|
||||
// 5. Return the result of running decode on xhr’s received bytes using fallback encoding charset.
|
||||
auto* decoder = TextCodec::decoder_for(charset.value());
|
||||
auto decoder = TextCodec::decoder_for(charset.value());
|
||||
|
||||
// If we don't support the decoder yet, let's crash instead of attempting to return something, as the result would be incorrect and create obscure bugs.
|
||||
VERIFY(decoder);
|
||||
VERIFY(decoder.has_value());
|
||||
|
||||
return TextCodec::convert_input_to_utf8_using_given_decoder_unless_there_is_a_byte_order_mark(*decoder, m_received_bytes);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue