TextDecoder.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /*
  2. * Copyright (c) 2022, Ali Mohammad Pur <mpfard@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 <LibJS/Forward.h>
  10. #include <LibTextCodec/Decoder.h>
  11. #include <LibWeb/Bindings/PlatformObject.h>
  12. #include <LibWeb/Forward.h>
  13. #include <LibWeb/WebIDL/ExceptionOr.h>
  14. namespace Web::Encoding {
  15. // https://encoding.spec.whatwg.org/#textdecoderoptions
  16. struct TextDecoderOptions {
  17. bool fatal = false;
  18. bool ignore_bom = false;
  19. };
  20. // https://encoding.spec.whatwg.org/#textdecodeoptions
  21. struct TextDecodeOptions {
  22. bool stream = false;
  23. };
  24. // https://encoding.spec.whatwg.org/#textdecoder
  25. class TextDecoder : public Bindings::PlatformObject {
  26. WEB_PLATFORM_OBJECT(TextDecoder, Bindings::PlatformObject);
  27. JS_DECLARE_ALLOCATOR(TextDecoder);
  28. public:
  29. static WebIDL::ExceptionOr<JS::NonnullGCPtr<TextDecoder>> construct_impl(JS::Realm&, FlyString encoding, Optional<TextDecoderOptions> const& options = {});
  30. virtual ~TextDecoder() override;
  31. WebIDL::ExceptionOr<String> decode(Optional<JS::Handle<WebIDL::BufferSource>> const&, Optional<TextDecodeOptions> const& options = {}) const;
  32. FlyString const& encoding() const { return m_encoding; }
  33. bool fatal() const { return m_fatal; }
  34. bool ignore_bom() const { return m_ignore_bom; }
  35. private:
  36. TextDecoder(JS::Realm&, TextCodec::Decoder&, FlyString encoding, bool fatal, bool ignore_bom);
  37. virtual void initialize(JS::Realm&) override;
  38. TextCodec::Decoder& m_decoder;
  39. FlyString m_encoding;
  40. bool m_fatal { false };
  41. bool m_ignore_bom { false };
  42. };
  43. }