| 
									
										
										
										
											2024-07-14 12:31:50 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2024, Jamie Mansfield <jmansfield@cadixdev.org> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/DOMStringListPrototype.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Bindings/Intrinsics.h>
 | 
					
						
							|  |  |  | #include <LibWeb/HTML/DOMStringList.h>
 | 
					
						
							|  |  |  | #include <LibWeb/WebIDL/ExceptionOr.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::HTML { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC_DEFINE_ALLOCATOR(DOMStringList); | 
					
						
							| 
									
										
										
										
											2024-07-14 12:31:50 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC::Ref<DOMStringList> DOMStringList::create(JS::Realm& realm, Vector<String> list) | 
					
						
							| 
									
										
										
										
											2024-07-14 12:31:50 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2024-11-14 05:50:17 +13:00
										 |  |  |     return realm.create<DOMStringList>(realm, list); | 
					
						
							| 
									
										
										
										
											2024-07-14 12:31:50 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DOMStringList::DOMStringList(JS::Realm& realm, Vector<String> list) | 
					
						
							|  |  |  |     : Bindings::PlatformObject(realm) | 
					
						
							|  |  |  |     , m_list(move(list)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     m_legacy_platform_object_flags = LegacyPlatformObjectFlags { .supports_indexed_properties = true }; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DOMStringList::initialize(JS::Realm& realm) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     WEB_SET_PROTOTYPE_FOR_INTERFACE(DOMStringList); | 
					
						
							| 
									
										
										
										
											2025-04-20 16:22:57 +02:00
										 |  |  |     Base::initialize(realm); | 
					
						
							| 
									
										
										
										
											2024-07-14 12:31:50 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-domstringlist-length
 | 
					
						
							|  |  |  | u32 DOMStringList::length() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // The length getter steps are to return this's associated list's size.
 | 
					
						
							|  |  |  |     return m_list.size(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-domstringlist-item
 | 
					
						
							|  |  |  | Optional<String> DOMStringList::item(u32 index) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // The item(index) method steps are to return the indexth item in this's associated list, or null if index plus one
 | 
					
						
							|  |  |  |     // is greater than this's associated list's size.
 | 
					
						
							|  |  |  |     if (index + 1 > m_list.size()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return m_list.at(index); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://html.spec.whatwg.org/multipage/common-dom-interfaces.html#dom-domstringlist-contains
 | 
					
						
							|  |  |  | bool DOMStringList::contains(StringView string) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // The contains(string) method steps are to return true if this's associated list contains string, and false otherwise.
 | 
					
						
							|  |  |  |     return m_list.contains_slow(string); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-29 11:11:52 +01:00
										 |  |  | Optional<JS::Value> DOMStringList::item_value(size_t index) const | 
					
						
							| 
									
										
										
										
											2024-07-14 12:31:50 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (index + 1 > m_list.size()) | 
					
						
							| 
									
										
										
										
											2024-07-29 11:11:52 +01:00
										 |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2024-07-14 12:31:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return JS::PrimitiveString::create(vm(), m_list.at(index)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |