/src/serenity/Userland/Libraries/LibWeb/Encoding/TextDecoder.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org> |
3 | | * Copyright (c) 2024, Simon Wanner <simon@skyrising.xyz> |
4 | | * |
5 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #include <AK/FlyString.h> |
9 | | #include <LibJS/Runtime/TypedArray.h> |
10 | | #include <LibWeb/Bindings/Intrinsics.h> |
11 | | #include <LibWeb/Bindings/TextDecoderPrototype.h> |
12 | | #include <LibWeb/Encoding/TextDecoder.h> |
13 | | #include <LibWeb/WebIDL/AbstractOperations.h> |
14 | | #include <LibWeb/WebIDL/Buffers.h> |
15 | | |
16 | | namespace Web::Encoding { |
17 | | |
18 | | JS_DEFINE_ALLOCATOR(TextDecoder); |
19 | | |
20 | | // https://encoding.spec.whatwg.org/#dom-textdecoder |
21 | | WebIDL::ExceptionOr<JS::NonnullGCPtr<TextDecoder>> TextDecoder::construct_impl(JS::Realm& realm, FlyString label, Optional<TextDecoderOptions> const& options) |
22 | 0 | { |
23 | 0 | auto& vm = realm.vm(); |
24 | | |
25 | | // 1. Let encoding be the result of getting an encoding from label. |
26 | 0 | auto encoding = TextCodec::get_standardized_encoding(label); |
27 | | |
28 | | // 2. If encoding is failure or replacement, then throw a RangeError. |
29 | 0 | if (!encoding.has_value() || encoding->equals_ignoring_ascii_case("replacement"sv)) |
30 | 0 | return WebIDL::SimpleException { WebIDL::SimpleExceptionType::RangeError, TRY_OR_THROW_OOM(vm, String::formatted("Invalid encoding {}", label)) }; |
31 | | |
32 | | // 3. Set this’s encoding to encoding. |
33 | | // https://encoding.spec.whatwg.org/#dom-textdecoder-encoding |
34 | | // The encoding getter steps are to return this’s encoding’s name, ASCII lowercased. |
35 | 0 | auto lowercase_encoding_name = MUST(String::from_byte_string(encoding.value().to_lowercase_string())); |
36 | | |
37 | | // 4. If options["fatal"] is true, then set this’s error mode to "fatal". |
38 | 0 | auto fatal = options.value_or({}).fatal; |
39 | | |
40 | | // 5. Set this’s ignore BOM to options["ignoreBOM"]. |
41 | 0 | auto ignore_bom = options.value_or({}).ignore_bom; |
42 | | |
43 | | // NOTE: This should happen in decode(), but we don't support streaming yet and share decoders across calls. |
44 | 0 | auto decoder = TextCodec::decoder_for_exact_name(encoding.value()); |
45 | 0 | VERIFY(decoder.has_value()); |
46 | | |
47 | 0 | return realm.heap().allocate<TextDecoder>(realm, realm, *decoder, lowercase_encoding_name, fatal, ignore_bom); |
48 | 0 | } |
49 | | |
50 | | // https://encoding.spec.whatwg.org/#dom-textdecoder |
51 | | TextDecoder::TextDecoder(JS::Realm& realm, TextCodec::Decoder& decoder, FlyString encoding, bool fatal, bool ignore_bom) |
52 | 0 | : PlatformObject(realm) |
53 | 0 | , m_decoder(decoder) |
54 | 0 | , m_encoding(move(encoding)) |
55 | 0 | , m_fatal(fatal) |
56 | 0 | , m_ignore_bom(ignore_bom) |
57 | 0 | { |
58 | 0 | } |
59 | | |
60 | 0 | TextDecoder::~TextDecoder() = default; |
61 | | |
62 | | void TextDecoder::initialize(JS::Realm& realm) |
63 | 0 | { |
64 | 0 | Base::initialize(realm); |
65 | 0 | WEB_SET_PROTOTYPE_FOR_INTERFACE(TextDecoder); |
66 | 0 | } |
67 | | |
68 | | // https://encoding.spec.whatwg.org/#dom-textdecoder-decode |
69 | | WebIDL::ExceptionOr<String> TextDecoder::decode(Optional<JS::Handle<WebIDL::BufferSource>> const& input, Optional<TextDecodeOptions> const&) const |
70 | 0 | { |
71 | 0 | if (!input.has_value()) |
72 | 0 | return TRY_OR_THROW_OOM(vm(), m_decoder.to_utf8({})); |
73 | | |
74 | | // FIXME: Implement the streaming stuff. |
75 | 0 | auto data_buffer_or_error = WebIDL::get_buffer_source_copy(*input.value()->raw_object()); |
76 | 0 | if (data_buffer_or_error.is_error()) |
77 | 0 | return WebIDL::OperationError::create(realm(), "Failed to copy bytes from ArrayBuffer"_string); |
78 | 0 | auto& data_buffer = data_buffer_or_error.value(); |
79 | 0 | auto result = TRY_OR_THROW_OOM(vm(), m_decoder.to_utf8({ data_buffer.data(), data_buffer.size() })); |
80 | 0 | if (this->fatal() && result.contains(0xfffd)) |
81 | 0 | return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Decoding failed"sv }; |
82 | 0 | return result; |
83 | 0 | } |
84 | | |
85 | | } |