2022-02-15 14:16:21 +03:30
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/FlyString.h>
|
|
|
|
#include <LibJS/Runtime/TypedArray.h>
|
LibWeb: Remove unecessary dependence on Window from assorted classes
These classes only needed Window to get at its realm. Pass a realm
directly to construct Crypto, Encoding, HRT, IntersectionObserver,
NavigationTiming, Page, RequestIdleCallback, Selection, Streams, URL,
and XML classes.
2022-09-25 18:11:21 -06:00
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2022-02-15 14:16:21 +03:30
|
|
|
#include <LibWeb/Encoding/TextDecoder.h>
|
2022-09-24 16:14:37 +01:00
|
|
|
#include <LibWeb/WebIDL/AbstractOperations.h>
|
2022-02-15 14:16:21 +03:30
|
|
|
|
|
|
|
namespace Web::Encoding {
|
|
|
|
|
LibWeb: Remove unecessary dependence on Window from assorted classes
These classes only needed Window to get at its realm. Pass a realm
directly to construct Crypto, Encoding, HRT, IntersectionObserver,
NavigationTiming, Page, RequestIdleCallback, Selection, Streams, URL,
and XML classes.
2022-09-25 18:11:21 -06:00
|
|
|
WebIDL::ExceptionOr<JS::NonnullGCPtr<TextDecoder>> TextDecoder::construct_impl(JS::Realm& realm, FlyString encoding)
|
2022-09-04 11:03:16 +02:00
|
|
|
{
|
|
|
|
auto decoder = TextCodec::decoder_for(encoding);
|
|
|
|
if (!decoder)
|
2022-09-25 17:03:42 +01:00
|
|
|
return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, String::formatted("Invalid encoding {}", encoding) };
|
2022-09-04 11:03:16 +02:00
|
|
|
|
LibWeb: Remove unecessary dependence on Window from assorted classes
These classes only needed Window to get at its realm. Pass a realm
directly to construct Crypto, Encoding, HRT, IntersectionObserver,
NavigationTiming, Page, RequestIdleCallback, Selection, Streams, URL,
and XML classes.
2022-09-25 18:11:21 -06:00
|
|
|
return JS::NonnullGCPtr(*realm.heap().allocate<TextDecoder>(realm, realm, *decoder, move(encoding), false, false));
|
2022-09-04 11:03:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://encoding.spec.whatwg.org/#dom-textdecoder
|
LibWeb: Remove unecessary dependence on Window from assorted classes
These classes only needed Window to get at its realm. Pass a realm
directly to construct Crypto, Encoding, HRT, IntersectionObserver,
NavigationTiming, Page, RequestIdleCallback, Selection, Streams, URL,
and XML classes.
2022-09-25 18:11:21 -06:00
|
|
|
TextDecoder::TextDecoder(JS::Realm& realm, TextCodec::Decoder& decoder, FlyString encoding, bool fatal, bool ignore_bom)
|
|
|
|
: PlatformObject(realm)
|
2022-09-04 11:03:16 +02:00
|
|
|
, m_decoder(decoder)
|
|
|
|
, m_encoding(move(encoding))
|
|
|
|
, m_fatal(fatal)
|
|
|
|
, m_ignore_bom(ignore_bom)
|
|
|
|
{
|
LibWeb: Remove unecessary dependence on Window from assorted classes
These classes only needed Window to get at its realm. Pass a realm
directly to construct Crypto, Encoding, HRT, IntersectionObserver,
NavigationTiming, Page, RequestIdleCallback, Selection, Streams, URL,
and XML classes.
2022-09-25 18:11:21 -06:00
|
|
|
set_prototype(&Bindings::cached_web_prototype(realm, "TextDecoder"));
|
2022-09-04 11:03:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
TextDecoder::~TextDecoder() = default;
|
|
|
|
|
2022-02-15 14:16:21 +03:30
|
|
|
// https://encoding.spec.whatwg.org/#dom-textdecoder-decode
|
2022-09-25 17:03:42 +01:00
|
|
|
WebIDL::ExceptionOr<String> TextDecoder::decode(JS::Handle<JS::Object> const& input) const
|
2022-02-15 14:16:21 +03:30
|
|
|
{
|
|
|
|
// FIXME: Implement the streaming stuff.
|
|
|
|
|
2022-09-24 16:14:37 +01:00
|
|
|
auto data_buffer_or_error = WebIDL::get_buffer_source_copy(*input.cell());
|
2022-07-22 21:01:36 +02:00
|
|
|
if (data_buffer_or_error.is_error())
|
LibWeb: Remove unecessary dependence on Window from assorted classes
These classes only needed Window to get at its realm. Pass a realm
directly to construct Crypto, Encoding, HRT, IntersectionObserver,
NavigationTiming, Page, RequestIdleCallback, Selection, Streams, URL,
and XML classes.
2022-09-25 18:11:21 -06:00
|
|
|
return WebIDL::OperationError::create(realm(), "Failed to copy bytes from ArrayBuffer");
|
2022-07-22 21:01:36 +02:00
|
|
|
auto& data_buffer = data_buffer_or_error.value();
|
|
|
|
return m_decoder.to_utf8({ data_buffer.data(), data_buffer.size() });
|
2022-02-15 14:16:21 +03:30
|
|
|
}
|
|
|
|
|
|
|
|
}
|