diff options
author | Ali Mohammad Pur <ali.mpfard@gmail.com> | 2022-02-15 14:16:21 +0330 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2022-02-16 22:48:32 +0330 |
commit | 16c0646b9d7da1c7b3482993873755e5e6a5d81f (patch) | |
tree | abfe1d3b1d5e5ca03fed2e26b4598bc559bac814 /Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp | |
parent | 57997ed3363b7df5a00db46958054fc20d789dcb (diff) | |
download | serenity-16c0646b9d7da1c7b3482993873755e5e6a5d81f.zip |
LibWeb: Implement a very basic version of TextDecoder
We had a very basic implementation of TextEncoder, let's add a
TextDecoder next to that :^)
Diffstat (limited to 'Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp')
-rw-r--r-- | Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp b/Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp new file mode 100644 index 0000000000..28575cd9c7 --- /dev/null +++ b/Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp @@ -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() }); +} + +} |