diff options
author | Linus Groh <mail@linusgroh.de> | 2021-12-12 18:05:11 +0000 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-12-12 20:58:36 +0100 |
commit | f37d00c07b313c8a6821b8add97d803dbd049f58 (patch) | |
tree | 1c5decb0c9d15fc20654237a6ab7b1ddac0c8b13 /Userland/Libraries/LibWeb/Encoding | |
parent | 35d3a1e77b3f628da225ca4ec5d0ba050082624a (diff) | |
download | serenity-f37d00c07b313c8a6821b8add97d803dbd049f58.zip |
LibWeb: Implement TextEncoder.prototype.encode()
Diffstat (limited to 'Userland/Libraries/LibWeb/Encoding')
-rw-r--r-- | Userland/Libraries/LibWeb/Encoding/TextEncoder.cpp | 36 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Encoding/TextEncoder.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/Encoding/TextEncoder.idl | 2 |
3 files changed, 39 insertions, 1 deletions
diff --git a/Userland/Libraries/LibWeb/Encoding/TextEncoder.cpp b/Userland/Libraries/LibWeb/Encoding/TextEncoder.cpp new file mode 100644 index 0000000000..a3a92231be --- /dev/null +++ b/Userland/Libraries/LibWeb/Encoding/TextEncoder.cpp @@ -0,0 +1,36 @@ +/* + * Copyright (c) 2021, Linus Groh <linusg@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#include <LibJS/Runtime/TypedArray.h> +#include <LibWeb/Bindings/Wrapper.h> +#include <LibWeb/Encoding/TextEncoder.h> + +namespace Web::Encoding { + +// https://encoding.spec.whatwg.org/#dom-textencoder-encode +JS::Uint8Array* TextEncoder::encode(String const& input) const +{ + auto& global_object = wrapper()->global_object(); + + // NOTE: The AK::String returned from PrimitiveString::string() is always UTF-8, regardless of the internal string type, so most of these steps are no-ops. + + // 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(); + + // FIXME: Support `TypedArray::create()` with existing `ArrayBuffer`, so that we don't have to allocate two `ByteBuffer`s. + auto* typed_array = JS::Uint8Array::create(global_object, byte_buffer.size()); + typed_array->viewed_array_buffer()->buffer() = move(byte_buffer); + return typed_array; +} + +} diff --git a/Userland/Libraries/LibWeb/Encoding/TextEncoder.h b/Userland/Libraries/LibWeb/Encoding/TextEncoder.h index b30d9264ce..2f97d536ba 100644 --- a/Userland/Libraries/LibWeb/Encoding/TextEncoder.h +++ b/Userland/Libraries/LibWeb/Encoding/TextEncoder.h @@ -32,6 +32,8 @@ public: return TextEncoder::create(); } + JS::Uint8Array* encode(String const& input) const; + protected: // https://encoding.spec.whatwg.org/#dom-textencoder TextEncoder() = default; diff --git a/Userland/Libraries/LibWeb/Encoding/TextEncoder.idl b/Userland/Libraries/LibWeb/Encoding/TextEncoder.idl index 09713478b7..8463e8a105 100644 --- a/Userland/Libraries/LibWeb/Encoding/TextEncoder.idl +++ b/Userland/Libraries/LibWeb/Encoding/TextEncoder.idl @@ -2,7 +2,7 @@ interface TextEncoder { constructor(); - // [NewObject] Uint8Array encode(optional USVString input = ""); + [NewObject] Uint8Array encode(optional USVString input = ""); // TextEncoderEncodeIntoResult encodeInto(USVString source, [AllowShared] Uint8Array destination); // readonly attribute DOMString encoding; |