| 
									
										
										
										
											2021-04-22 21:11:20 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-09-01 20:50:16 +02:00
										 |  |  |  * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2023-02-28 00:05:39 +00:00
										 |  |  |  * Copyright (c) 2023, Luke Wilde <lukew@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-04-22 21:11:20 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/Function.h>
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  | #include <LibJS/Heap/GCPtr.h>
 | 
					
						
							| 
									
										
										
										
											2024-01-09 16:05:03 -07:00
										 |  |  | #include <LibWeb/Bindings/PlatformObject.h>
 | 
					
						
							| 
									
										
										
										
											2021-04-22 21:11:20 +02:00
										 |  |  | #include <LibWeb/Forward.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::DOM { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // NOTE: HTMLCollection is in the DOM namespace because it's part of the DOM specification.
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // This class implements a live, filtered view of a DOM subtree.
 | 
					
						
							|  |  |  | // When constructing an HTMLCollection, you provide a root node + a filter.
 | 
					
						
							|  |  |  | // The filter is a simple Function object that answers the question
 | 
					
						
							|  |  |  | // "is this Element part of the collection?"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-01-09 16:05:03 -07:00
										 |  |  | class HTMLCollection : public Bindings::PlatformObject { | 
					
						
							|  |  |  |     WEB_PLATFORM_OBJECT(HTMLCollection, Bindings::PlatformObject); | 
					
						
							| 
									
										
										
										
											2023-11-19 19:47:52 +01:00
										 |  |  |     JS_DECLARE_ALLOCATOR(HTMLCollection); | 
					
						
							| 
									
										
										
										
											2021-04-22 21:11:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-05-23 11:25:07 +02:00
										 |  |  |     enum class Scope { | 
					
						
							|  |  |  |         Children, | 
					
						
							|  |  |  |         Descendants, | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2024-05-17 17:14:06 -07:00
										 |  |  |     [[nodiscard]] static JS::NonnullGCPtr<HTMLCollection> create(ParentNode& root, Scope, ESCAPING Function<bool(Element const&)> filter); | 
					
						
							| 
									
										
										
										
											2021-04-22 21:11:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-01 20:50:16 +02:00
										 |  |  |     virtual ~HTMLCollection() override; | 
					
						
							| 
									
										
										
										
											2021-04-22 21:11:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-19 13:41:57 +12:00
										 |  |  |     size_t length() const; | 
					
						
							| 
									
										
										
										
											2021-09-26 15:14:37 +01:00
										 |  |  |     Element* item(size_t index) const; | 
					
						
							| 
									
										
										
										
											2024-04-26 22:27:27 +12:00
										 |  |  |     Element* named_item(FlyString const& key) const; | 
					
						
							| 
									
										
										
										
											2021-04-22 21:11:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-31 21:55:43 +02:00
										 |  |  |     JS::MarkedVector<JS::NonnullGCPtr<Element>> collect_matching_elements() const; | 
					
						
							| 
									
										
										
										
											2021-09-26 15:14:37 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-07-25 15:45:46 +12:00
										 |  |  |     virtual JS::Value item_value(size_t index) const override; | 
					
						
							| 
									
										
										
										
											2023-10-08 13:16:50 +13:00
										 |  |  |     virtual WebIDL::ExceptionOr<JS::Value> named_item_value(FlyString const& name) const override; | 
					
						
							| 
									
										
										
										
											2023-12-24 20:59:00 +01:00
										 |  |  |     virtual Vector<FlyString> supported_property_names() const override; | 
					
						
							| 
									
										
										
										
											2024-07-25 07:49:41 +02:00
										 |  |  |     virtual bool is_supported_property_name(FlyString const&) const override; | 
					
						
							| 
									
										
										
										
											2022-09-01 20:50:16 +02:00
										 |  |  |     virtual bool is_supported_property_index(u32) const override; | 
					
						
							| 
									
										
										
										
											2021-04-22 21:11:20 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | protected: | 
					
						
							| 
									
										
										
										
											2024-05-17 17:14:06 -07:00
										 |  |  |     HTMLCollection(ParentNode& root, Scope, ESCAPING Function<bool(Element const&)> filter); | 
					
						
							| 
									
										
										
										
											2021-04-22 21:11:20 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-07 08:41:28 +02:00
										 |  |  |     virtual void initialize(JS::Realm&) override; | 
					
						
							| 
									
										
										
										
											2023-01-10 06:56:59 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     JS::NonnullGCPtr<ParentNode> root() { return *m_root; } | 
					
						
							| 
									
										
										
										
											2023-12-21 22:36:37 +13:00
										 |  |  |     JS::NonnullGCPtr<ParentNode const> root() const { return *m_root; } | 
					
						
							| 
									
										
										
										
											2022-03-21 20:01:47 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-04-22 21:11:20 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2022-09-01 20:50:16 +02:00
										 |  |  |     virtual void visit_edges(Cell::Visitor&) override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-04-01 15:08:22 +02:00
										 |  |  |     void update_cache_if_needed() const; | 
					
						
							| 
									
										
										
										
											2024-07-25 07:49:41 +02:00
										 |  |  |     void update_name_to_element_mappings_if_needed() const; | 
					
						
							| 
									
										
										
										
											2024-04-01 15:08:22 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-03-19 15:41:02 +01:00
										 |  |  |     mutable u64 m_cached_dom_tree_version { 0 }; | 
					
						
							|  |  |  |     mutable Vector<JS::NonnullGCPtr<Element>> m_cached_elements; | 
					
						
							| 
									
										
										
										
											2024-07-25 07:49:41 +02:00
										 |  |  |     mutable OwnPtr<HashMap<FlyString, JS::NonnullGCPtr<Element>>> m_cached_name_to_element_mappings; | 
					
						
							| 
									
										
										
										
											2024-03-19 15:41:02 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-01 20:50:16 +02:00
										 |  |  |     JS::NonnullGCPtr<ParentNode> m_root; | 
					
						
							| 
									
										
										
										
											2021-04-22 21:11:20 +02:00
										 |  |  |     Function<bool(Element const&)> m_filter; | 
					
						
							| 
									
										
										
										
											2023-05-23 11:25:07 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Scope m_scope { Scope::Descendants }; | 
					
						
							| 
									
										
										
										
											2021-04-22 21:11:20 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |