| 
									
										
										
										
											2022-02-15 14:16:21 +03:30
										 |  |  |  | /*
 | 
					
						
							|  |  |  |  |  * Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org> | 
					
						
							| 
									
										
										
										
											2024-05-30 20:44:41 +02:00
										 |  |  |  |  * Copyright (c) 2024, Simon Wanner <simon@skyrising.xyz> | 
					
						
							| 
									
										
										
										
											2022-02-15 14:16:21 +03:30
										 |  |  |  |  * | 
					
						
							|  |  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  |  */ | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-06 16:06:53 +12:00
										 |  |  |  | #include <AK/FlyString.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-15 14:16:21 +03:30
										 |  |  |  | #include <LibJS/Runtime/TypedArray.h>
 | 
					
						
							| 
									
										
										
											
												LibWeb: Remove unecessary dependence on Window from assorted classes
These classes only needed Window to get at its realm. Pass a realm
directly to construct Crypto, Encoding, HRT, IntersectionObserver,
NavigationTiming, Page, RequestIdleCallback, Selection, Streams, URL,
and XML classes.
											
										 
											2022-09-25 18:11:21 -06:00
										 |  |  |  | #include <LibWeb/Bindings/Intrinsics.h>
 | 
					
						
							| 
									
										
										
										
											2024-04-27 12:09:58 +12:00
										 |  |  |  | #include <LibWeb/Bindings/TextDecoderPrototype.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-15 14:16:21 +03:30
										 |  |  |  | #include <LibWeb/Encoding/TextDecoder.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-24 16:14:37 +01:00
										 |  |  |  | #include <LibWeb/WebIDL/AbstractOperations.h>
 | 
					
						
							| 
									
										
										
										
											2023-11-23 20:07:25 +13:00
										 |  |  |  | #include <LibWeb/WebIDL/Buffers.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-15 14:16:21 +03:30
										 |  |  |  | 
 | 
					
						
							|  |  |  |  | namespace Web::Encoding { | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  | GC_DEFINE_ALLOCATOR(TextDecoder); | 
					
						
							| 
									
										
										
										
											2023-11-19 19:47:52 +01:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-30 20:44:41 +02:00
										 |  |  |  | // https://encoding.spec.whatwg.org/#dom-textdecoder
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  | WebIDL::ExceptionOr<GC::Ref<TextDecoder>> TextDecoder::construct_impl(JS::Realm& realm, FlyString label, Optional<TextDecoderOptions> const& options) | 
					
						
							| 
									
										
										
										
											2022-09-04 11:03:16 +02:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-03-03 18:04:58 +00:00
										 |  |  |  |     auto& vm = realm.vm(); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-05-30 20:44:41 +02:00
										 |  |  |  |     // 1. Let encoding be the result of getting an encoding from label.
 | 
					
						
							|  |  |  |  |     auto encoding = TextCodec::get_standardized_encoding(label); | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 2. If encoding is failure or replacement, then throw a RangeError.
 | 
					
						
							|  |  |  |  |     if (!encoding.has_value() || encoding->equals_ignoring_ascii_case("replacement"sv)) | 
					
						
							|  |  |  |  |         return WebIDL::SimpleException { WebIDL::SimpleExceptionType::RangeError, TRY_OR_THROW_OOM(vm, String::formatted("Invalid encoding {}", label)) }; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 3. Set this’s encoding to encoding.
 | 
					
						
							|  |  |  |  |     // https://encoding.spec.whatwg.org/#dom-textdecoder-encoding
 | 
					
						
							|  |  |  |  |     // The encoding getter steps are to return this’s encoding’s name, ASCII lowercased.
 | 
					
						
							| 
									
										
										
										
											2025-04-06 10:19:35 -04:00
										 |  |  |  |     auto lowercase_encoding_name = encoding.value().to_ascii_lowercase_string(); | 
					
						
							| 
									
										
										
										
											2024-05-30 20:44:41 +02:00
										 |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 4. If options["fatal"] is true, then set this’s error mode to "fatal".
 | 
					
						
							|  |  |  |  |     auto fatal = options.value_or({}).fatal; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // 5. Set this’s ignore BOM to options["ignoreBOM"].
 | 
					
						
							|  |  |  |  |     auto ignore_bom = options.value_or({}).ignore_bom; | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  |     // NOTE: This should happen in decode(), but we don't support streaming yet and share decoders across calls.
 | 
					
						
							| 
									
										
										
										
											2024-06-02 15:56:36 +02:00
										 |  |  |  |     auto decoder = TextCodec::decoder_for_exact_name(encoding.value()); | 
					
						
							| 
									
										
										
										
											2024-05-30 20:44:41 +02:00
										 |  |  |  |     VERIFY(decoder.has_value()); | 
					
						
							| 
									
										
										
										
											2022-09-04 11:03:16 +02:00
										 |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-14 05:50:17 +13:00
										 |  |  |  |     return realm.create<TextDecoder>(realm, *decoder, lowercase_encoding_name, fatal, ignore_bom); | 
					
						
							| 
									
										
										
										
											2022-09-04 11:03:16 +02:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | // https://encoding.spec.whatwg.org/#dom-textdecoder
 | 
					
						
							| 
									
										
										
										
											2023-09-06 16:06:53 +12:00
										 |  |  |  | TextDecoder::TextDecoder(JS::Realm& realm, TextCodec::Decoder& decoder, FlyString encoding, bool fatal, bool ignore_bom) | 
					
						
							| 
									
										
										
											
												LibWeb: Remove unecessary dependence on Window from assorted classes
These classes only needed Window to get at its realm. Pass a realm
directly to construct Crypto, Encoding, HRT, IntersectionObserver,
NavigationTiming, Page, RequestIdleCallback, Selection, Streams, URL,
and XML classes.
											
										 
											2022-09-25 18:11:21 -06:00
										 |  |  |  |     : PlatformObject(realm) | 
					
						
							| 
									
										
										
										
											2022-09-04 11:03:16 +02:00
										 |  |  |  |     , m_decoder(decoder) | 
					
						
							|  |  |  |  |     , m_encoding(move(encoding)) | 
					
						
							|  |  |  |  |     , m_fatal(fatal) | 
					
						
							|  |  |  |  |     , m_ignore_bom(ignore_bom) | 
					
						
							|  |  |  |  | { | 
					
						
							|  |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | TextDecoder::~TextDecoder() = default; | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |  | void TextDecoder::initialize(JS::Realm& realm) | 
					
						
							| 
									
										
										
										
											2023-01-10 06:28:20 -05:00
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-03-16 13:13:08 +01:00
										 |  |  |  |     WEB_SET_PROTOTYPE_FOR_INTERFACE(TextDecoder); | 
					
						
							| 
									
										
										
										
											2025-04-20 16:22:57 +02:00
										 |  |  |  |     Base::initialize(realm); | 
					
						
							| 
									
										
										
										
											2023-01-10 06:28:20 -05:00
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-15 14:16:21 +03:30
										 |  |  |  | // https://encoding.spec.whatwg.org/#dom-textdecoder-decode
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  |  | WebIDL::ExceptionOr<String> TextDecoder::decode(Optional<GC::Root<WebIDL::BufferSource>> const& input, Optional<TextDecodeOptions> const&) const | 
					
						
							| 
									
										
										
										
											2022-02-15 14:16:21 +03:30
										 |  |  |  | { | 
					
						
							| 
									
										
										
										
											2023-05-30 09:48:23 +03:30
										 |  |  |  |     if (!input.has_value()) | 
					
						
							|  |  |  |  |         return TRY_OR_THROW_OOM(vm(), m_decoder.to_utf8({})); | 
					
						
							|  |  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-15 14:16:21 +03:30
										 |  |  |  |     // FIXME: Implement the streaming stuff.
 | 
					
						
							| 
									
										
										
										
											2023-11-23 20:07:25 +13:00
										 |  |  |  |     auto data_buffer_or_error = WebIDL::get_buffer_source_copy(*input.value()->raw_object()); | 
					
						
							| 
									
										
										
										
											2022-07-22 21:01:36 +02:00
										 |  |  |  |     if (data_buffer_or_error.is_error()) | 
					
						
							| 
									
										
										
										
											2025-08-07 19:31:52 -04:00
										 |  |  |  |         return WebIDL::OperationError::create(realm(), "Failed to copy bytes from ArrayBuffer"_utf16); | 
					
						
							| 
									
										
										
										
											2022-07-22 21:01:36 +02:00
										 |  |  |  |     auto& data_buffer = data_buffer_or_error.value(); | 
					
						
							| 
									
										
										
										
											2024-05-27 16:58:36 +02:00
										 |  |  |  |     auto result = TRY_OR_THROW_OOM(vm(), m_decoder.to_utf8({ data_buffer.data(), data_buffer.size() })); | 
					
						
							|  |  |  |  |     if (this->fatal() && result.contains(0xfffd)) | 
					
						
							|  |  |  |  |         return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, "Decoding failed"sv }; | 
					
						
							|  |  |  |  |     return result; | 
					
						
							| 
									
										
										
										
											2022-02-15 14:16:21 +03:30
										 |  |  |  | } | 
					
						
							|  |  |  |  | 
 | 
					
						
							|  |  |  |  | } |