| 
									
										
										
										
											2022-02-15 14:16:21 +03:30
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2022, Ali Mohammad Pur <mpfard@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-08 19:23:00 -05:00
										 |  |  | #include <AK/DeprecatedFlyString.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>
 | 
					
						
							| 
									
										
										
										
											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>
 | 
					
						
							| 
									
										
										
										
											2022-02-15 14:16:21 +03:30
										 |  |  | 
 | 
					
						
							|  |  |  | namespace Web::Encoding { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-08 19:23:00 -05:00
										 |  |  | WebIDL::ExceptionOr<JS::NonnullGCPtr<TextDecoder>> TextDecoder::construct_impl(JS::Realm& realm, DeprecatedFlyString encoding) | 
					
						
							| 
									
										
										
										
											2022-09-04 11:03:16 +02:00
										 |  |  | { | 
					
						
							|  |  |  |     auto decoder = TextCodec::decoder_for(encoding); | 
					
						
							|  |  |  |     if (!decoder) | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  |         return WebIDL::SimpleException { WebIDL::SimpleExceptionType::TypeError, DeprecatedString::formatted("Invalid encoding {}", encoding) }; | 
					
						
							| 
									
										
										
										
											2022-09-04 11:03:16 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-12-14 17:40:33 +00:00
										 |  |  |     return realm.heap().allocate<TextDecoder>(realm, realm, *decoder, move(encoding), false, false); | 
					
						
							| 
									
										
										
										
											2022-09-04 11:03:16 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://encoding.spec.whatwg.org/#dom-textdecoder
 | 
					
						
							| 
									
										
										
										
											2023-01-08 19:23:00 -05:00
										 |  |  | TextDecoder::TextDecoder(JS::Realm& realm, TextCodec::Decoder& decoder, DeprecatedFlyString 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-01-28 12:33:35 -05:00
										 |  |  | JS::ThrowCompletionOr<void> TextDecoder::initialize(JS::Realm& realm) | 
					
						
							| 
									
										
										
										
											2023-01-10 06:28:20 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-28 12:33:35 -05:00
										 |  |  |     MUST_OR_THROW_OOM(Base::initialize(realm)); | 
					
						
							| 
									
										
										
										
											2023-01-10 06:28:20 -05:00
										 |  |  |     set_prototype(&Bindings::ensure_web_prototype<Bindings::TextDecoderPrototype>(realm, "TextDecoder")); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:33:35 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2023-01-10 06:28:20 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-15 14:16:21 +03:30
										 |  |  | // https://encoding.spec.whatwg.org/#dom-textdecoder-decode
 | 
					
						
							| 
									
										
										
										
											2022-12-04 18:02:33 +00:00
										 |  |  | WebIDL::ExceptionOr<DeprecatedString> TextDecoder::decode(JS::Handle<JS::Object> const& input) const | 
					
						
							| 
									
										
										
										
											2022-02-15 14:16:21 +03:30
										 |  |  | { | 
					
						
							|  |  |  |     // FIXME: Implement the streaming stuff.
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-24 16:14:37 +01:00
										 |  |  |     auto data_buffer_or_error = WebIDL::get_buffer_source_copy(*input.cell()); | 
					
						
							| 
									
										
										
										
											2022-07-22 21:01:36 +02:00
										 |  |  |     if (data_buffer_or_error.is_error()) | 
					
						
							| 
									
										
										
											
												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
										 |  |  |         return WebIDL::OperationError::create(realm(), "Failed to copy bytes from ArrayBuffer"); | 
					
						
							| 
									
										
										
										
											2022-07-22 21:01:36 +02:00
										 |  |  |     auto& data_buffer = data_buffer_or_error.value(); | 
					
						
							|  |  |  |     return m_decoder.to_utf8({ data_buffer.data(), data_buffer.size() }); | 
					
						
							| 
									
										
										
										
											2022-02-15 14:16:21 +03:30
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |