1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:08:11 +00:00

LibWeb: Implement a very basic version of TextDecoder

We had a very basic implementation of TextEncoder, let's add a
TextDecoder next to that :^)
This commit is contained in:
Ali Mohammad Pur 2022-02-15 14:16:21 +03:30 committed by Ali Mohammad Pur
parent 57997ed336
commit 16c0646b9d
6 changed files with 109 additions and 0 deletions

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/FlyString.h>
#include <LibJS/Runtime/TypedArray.h>
#include <LibWeb/Bindings/IDLAbstractOperations.h>
#include <LibWeb/Bindings/Wrapper.h>
#include <LibWeb/Encoding/TextDecoder.h>
namespace Web::Encoding {
// https://encoding.spec.whatwg.org/#dom-textdecoder-decode
DOM::ExceptionOr<String> TextDecoder::decode(JS::Handle<JS::Object> const& input) const
{
// FIXME: Implement the streaming stuff.
auto data_buffer = Bindings::IDL::get_buffer_source_copy(*input.cell());
if (!data_buffer.has_value())
return DOM::OperationError::create("Failed to copy bytes from ArrayBuffer");
return m_decoder.to_utf8({ data_buffer->data(), data_buffer->size() });
}
}