2021-12-12 18:05:11 +00:00
/*
2022-08-22 19:00:49 +01:00
* Copyright ( c ) 2021 - 2022 , Linus Groh < linusg @ serenityos . org >
2021-12-12 18:05:11 +00:00
*
* SPDX - License - Identifier : BSD - 2 - Clause
*/
2021-12-12 18:06:07 +00:00
# include <AK/FlyString.h>
2021-12-12 18:05:11 +00:00
# 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>
2021-12-12 18:05:11 +00:00
# include <LibWeb/Encoding/TextEncoder.h>
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
JS : : NonnullGCPtr < TextEncoder > TextEncoder : : construct_impl ( JS : : Realm & realm )
2022-09-04 10:56:37 +02:00
{
2022-12-14 17:40:33 +00:00
return realm . heap ( ) . allocate < TextEncoder > ( realm , realm ) ;
2022-09-04 10:56:37 +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
TextEncoder : : TextEncoder ( JS : : Realm & realm )
: PlatformObject ( realm )
2022-09-04 10:56:37 +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
set_prototype ( & Bindings : : cached_web_prototype ( realm , " TextEncoder " ) ) ;
2022-09-04 10:56:37 +02:00
}
TextEncoder : : ~ TextEncoder ( ) = default ;
2021-12-12 18:05:11 +00:00
// https://encoding.spec.whatwg.org/#dom-textencoder-encode
2022-12-04 18:02:33 +00:00
JS : : Uint8Array * TextEncoder : : encode ( DeprecatedString const & input ) const
2021-12-12 18:05:11 +00:00
{
2022-12-04 18:02:33 +00:00
// NOTE: The AK::DeprecatedString returned from PrimitiveString::string() is always UTF-8, regardless of the internal string type, so most of these steps are no-ops.
2021-12-12 18:05:11 +00:00
// 1. Convert input to an I/O queue of scalar values.
// 2. Let output be the I/O queue of bytes « end-of-queue ».
// 3. While true:
// 1. Let item be the result of reading from input.
// 2. Let result be the result of processing an item with item, an instance of the UTF-8 encoder, input, output, and "fatal".
// 3. Assert: result is not an error.
// 4. If result is finished, then convert output into a byte sequence and return a Uint8Array object wrapping an ArrayBuffer containing output.
auto byte_buffer = input . to_byte_buffer ( ) ;
2022-02-07 13:34:32 +01:00
auto array_length = byte_buffer . size ( ) ;
2022-12-13 20:49:50 +00:00
auto array_buffer = JS : : ArrayBuffer : : create ( realm ( ) , move ( byte_buffer ) ) ;
2022-09-04 10:56:37 +02:00
return JS : : Uint8Array : : create ( realm ( ) , array_length , * array_buffer ) ;
2021-12-12 18:05:11 +00:00
}
2021-12-12 18:06:07 +00:00
// https://encoding.spec.whatwg.org/#dom-textencoder-encoding
FlyString const & TextEncoder : : encoding ( )
{
static FlyString encoding = " utf-8 " sv ;
return encoding ;
}
2021-12-12 18:05:11 +00:00
}