1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-15 00:14:58 +00:00
serenity/Userland/Libraries/LibWeb/Encoding/TextEncoder.h
Shannon Booth 04c094343f LibWeb+Meta: Add wrapper for the BufferSource/ArrayBufferView IDL types
These wrappers will make it much easier to do various operations on the
different ArrayBuffer-related classes in LibWeb compared to the current
solution, which is to just accept a Handle<Object> everywhere (and use
"any" in the *.idl files).

Co-Authored-By: Matthew Olsson <mattco@serenityos.org>
2023-11-24 08:43:35 +01:00

47 lines
1.3 KiB
C++

/*
* Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Forward.h>
#include <AK/NonnullRefPtr.h>
#include <AK/RefCounted.h>
#include <LibJS/Forward.h>
#include <LibWeb/Bindings/PlatformObject.h>
#include <LibWeb/Forward.h>
#include <LibWeb/WebIDL/Buffers.h>
namespace Web::Encoding {
// https://encoding.spec.whatwg.org/#dictdef-textencoderencodeintoresult
struct TextEncoderEncodeIntoResult {
unsigned long long read;
unsigned long long written;
};
// https://encoding.spec.whatwg.org/#textencoder
class TextEncoder final : public Bindings::PlatformObject {
WEB_PLATFORM_OBJECT(TextEncoder, Bindings::PlatformObject);
JS_DECLARE_ALLOCATOR(TextEncoder);
public:
static WebIDL::ExceptionOr<JS::NonnullGCPtr<TextEncoder>> construct_impl(JS::Realm&);
virtual ~TextEncoder() override;
JS::NonnullGCPtr<JS::Uint8Array> encode(String const& input) const;
TextEncoderEncodeIntoResult encode_into(String const& source, JS::Handle<WebIDL::BufferSource> const& destination) const;
static FlyString const& encoding();
protected:
// https://encoding.spec.whatwg.org/#dom-textencoder
explicit TextEncoder(JS::Realm&);
virtual void initialize(JS::Realm&) override;
};
}