TextEncoder.h 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /*
  2. * Copyright (c) 2021, Linus Groh <linusg@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/Forward.h>
  8. #include <AK/NonnullRefPtr.h>
  9. #include <AK/RefCounted.h>
  10. #include <LibJS/Forward.h>
  11. #include <LibWeb/Bindings/PlatformObject.h>
  12. #include <LibWeb/Forward.h>
  13. #include <LibWeb/WebIDL/Buffers.h>
  14. namespace Web::Encoding {
  15. // https://encoding.spec.whatwg.org/#dictdef-textencoderencodeintoresult
  16. struct TextEncoderEncodeIntoResult {
  17. unsigned long long read;
  18. unsigned long long written;
  19. };
  20. // https://encoding.spec.whatwg.org/#textencoder
  21. class TextEncoder final : public Bindings::PlatformObject {
  22. WEB_PLATFORM_OBJECT(TextEncoder, Bindings::PlatformObject);
  23. JS_DECLARE_ALLOCATOR(TextEncoder);
  24. public:
  25. static WebIDL::ExceptionOr<JS::NonnullGCPtr<TextEncoder>> construct_impl(JS::Realm&);
  26. virtual ~TextEncoder() override;
  27. JS::NonnullGCPtr<JS::Uint8Array> encode(String const& input) const;
  28. TextEncoderEncodeIntoResult encode_into(String const& source, JS::Handle<WebIDL::BufferSource> const& destination) const;
  29. static FlyString const& encoding();
  30. protected:
  31. // https://encoding.spec.whatwg.org/#dom-textencoder
  32. explicit TextEncoder(JS::Realm&);
  33. virtual void initialize(JS::Realm&) override;
  34. };
  35. }