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
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#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>
|
2024-04-27 12:09:58 +12:00
|
|
|
|
#include <LibWeb/Bindings/TextEncoderPrototype.h>
|
2021-12-12 18:05:11 +00:00
|
|
|
|
#include <LibWeb/Encoding/TextEncoder.h>
|
2023-02-19 16:53:44 +01:00
|
|
|
|
#include <LibWeb/WebIDL/ExceptionOr.h>
|
2021-12-12 18:05:11 +00:00
|
|
|
|
|
|
|
|
|
namespace Web::Encoding {
|
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC_DEFINE_ALLOCATOR(TextEncoder);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
|
WebIDL::ExceptionOr<GC::Ref<TextEncoder>> TextEncoder::construct_impl(JS::Realm& realm)
|
2022-09-04 10:56:37 +02:00
|
|
|
|
{
|
2024-11-14 05:50:17 +13:00
|
|
|
|
return realm.create<TextEncoder>(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
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextEncoder::~TextEncoder() = default;
|
|
|
|
|
|
2023-08-07 08:41:28 +02:00
|
|
|
|
void TextEncoder::initialize(JS::Realm& realm)
|
2023-01-10 06:28:20 -05:00
|
|
|
|
{
|
2024-03-16 13:13:08 +01:00
|
|
|
|
WEB_SET_PROTOTYPE_FOR_INTERFACE(TextEncoder);
|
2025-04-20 16:22:57 +02:00
|
|
|
|
Base::initialize(realm);
|
2023-01-10 06:28:20 -05:00
|
|
|
|
}
|
|
|
|
|
|
2021-12-12 18:05:11 +00:00
|
|
|
|
// https://encoding.spec.whatwg.org/#dom-textencoder-encode
|
2024-11-15 04:01:23 +13:00
|
|
|
|
GC::Ref<JS::Uint8Array> TextEncoder::encode(String const& input) const
|
2021-12-12 18:05:11 +00:00
|
|
|
|
{
|
2023-10-06 07:03:44 +02:00
|
|
|
|
// NOTE: The AK::String is always UTF-8, 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.
|
|
|
|
|
|
2023-09-06 16:10:23 +12:00
|
|
|
|
auto byte_buffer = MUST(ByteBuffer::copy(input.bytes()));
|
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
|
|
|
|
}
|
|
|
|
|
|
2023-10-06 07:03:44 +02:00
|
|
|
|
// https://encoding.spec.whatwg.org/#dom-textencoder-encodeinto
|
2024-11-15 04:01:23 +13:00
|
|
|
|
TextEncoderEncodeIntoResult TextEncoder::encode_into(String const& source, GC::Root<WebIDL::BufferSource> const& destination) const
|
2023-10-06 07:03:44 +02:00
|
|
|
|
{
|
2025-04-18 18:35:44 +02:00
|
|
|
|
// AD-HOC: Return early if destination is detached. This is not explicitly handled in the spec,
|
|
|
|
|
// however no bytes are copied as destinations size is always zero in this case.
|
|
|
|
|
// See: https://github.com/whatwg/encoding/issues/324
|
|
|
|
|
if (destination->viewed_array_buffer()->is_detached())
|
|
|
|
|
return { 0, 0 };
|
|
|
|
|
|
2025-01-09 16:30:30 +00:00
|
|
|
|
auto data = destination->viewed_array_buffer()->buffer().bytes().slice(destination->byte_offset(), destination->byte_length());
|
2023-10-06 07:03:44 +02:00
|
|
|
|
|
|
|
|
|
// 1. Let read be 0.
|
2024-02-26 18:54:36 +01:00
|
|
|
|
WebIDL::UnsignedLongLong read = 0;
|
2023-10-06 07:03:44 +02:00
|
|
|
|
// 2. Let written be 0.
|
2024-02-26 18:54:36 +01:00
|
|
|
|
WebIDL::UnsignedLongLong written = 0;
|
2023-10-06 07:03:44 +02:00
|
|
|
|
|
|
|
|
|
// NOTE: The AK::String is always UTF-8, so most of these steps are no-ops.
|
|
|
|
|
// 3. Let encoder be an instance of the UTF-8 encoder.
|
|
|
|
|
// 4. Let unused be the I/O queue of scalar values « end-of-queue ».
|
|
|
|
|
// 5. Convert source to an I/O queue of scalar values.
|
|
|
|
|
auto code_points = source.code_points();
|
|
|
|
|
auto it = code_points.begin();
|
|
|
|
|
|
|
|
|
|
// 6. While true:
|
|
|
|
|
while (true) {
|
|
|
|
|
// 6.1. Let item be the result of reading from source.
|
|
|
|
|
// 6.2. Let result be the result of running encoder’s handler on unused and item.
|
|
|
|
|
// 6.3. If result is finished, then break.
|
|
|
|
|
if (it.done())
|
|
|
|
|
break;
|
|
|
|
|
auto item = *it;
|
|
|
|
|
auto result = it.underlying_code_point_bytes();
|
|
|
|
|
|
|
|
|
|
// 6.4. Otherwise:
|
|
|
|
|
// 6.4.1. If destination’s byte length − written is greater than or equal to the number of bytes in result, then:
|
2025-01-09 16:30:30 +00:00
|
|
|
|
if (destination->byte_length() - written >= result.size()) {
|
2023-10-06 07:03:44 +02:00
|
|
|
|
// 6.4.1.1. If item is greater than U+FFFF, then increment read by 2.
|
|
|
|
|
if (item > 0xffff) {
|
|
|
|
|
read += 2;
|
|
|
|
|
}
|
|
|
|
|
// 6.4.1.2. Otherwise, increment read by 1.
|
|
|
|
|
else {
|
|
|
|
|
read++;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 6.4.1.3. Write the bytes in result into destination, with startingOffset set to written.
|
|
|
|
|
// 6.4.1.4. Increment written by the number of bytes in result.
|
2025-04-18 18:35:44 +02:00
|
|
|
|
// WARNING: See the warning for SharedArrayBuffer objects at https://encoding.spec.whatwg.org/#sharedarraybuffer-warning.
|
2023-10-06 07:03:44 +02:00
|
|
|
|
for (auto byte : result)
|
|
|
|
|
data[written++] = byte;
|
|
|
|
|
}
|
|
|
|
|
// 6.4.2. Otherwise, break.
|
|
|
|
|
else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
++it;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 7. Return «[ "read" → read, "written" → written ]».
|
|
|
|
|
return { read, written };
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-12 18:05:11 +00:00
|
|
|
|
}
|