TextDecoder.cpp 811 B

123456789101112131415161718192021222324252627
  1. /*
  2. * Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/FlyString.h>
  7. #include <LibJS/Runtime/TypedArray.h>
  8. #include <LibWeb/Bindings/IDLAbstractOperations.h>
  9. #include <LibWeb/Bindings/Wrapper.h>
  10. #include <LibWeb/Encoding/TextDecoder.h>
  11. namespace Web::Encoding {
  12. // https://encoding.spec.whatwg.org/#dom-textdecoder-decode
  13. DOM::ExceptionOr<String> TextDecoder::decode(JS::Handle<JS::Object> const& input) const
  14. {
  15. // FIXME: Implement the streaming stuff.
  16. auto data_buffer = Bindings::IDL::get_buffer_source_copy(*input.cell());
  17. if (!data_buffer.has_value())
  18. return DOM::OperationError::create("Failed to copy bytes from ArrayBuffer");
  19. return m_decoder.to_utf8({ data_buffer->data(), data_buffer->size() });
  20. }
  21. }