| 
									
										
										
										
											2022-02-12 16:38:54 +03:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2022, DerpyCrabs <derpycrabs@gmail.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-04 01:59:58 +02:00
										 |  |  | #include <LibJS/Heap/Handle.h>
 | 
					
						
							| 
									
										
										
										
											2022-09-25 18:03:02 -06:00
										 |  |  | #include <LibWeb/Bindings/Intrinsics.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-12 16:38:54 +03:00
										 |  |  | #include <LibWeb/Geometry/DOMRect.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Geometry/DOMRectList.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::Geometry { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 18:03:02 -06:00
										 |  |  | JS::NonnullGCPtr<DOMRectList> DOMRectList::create(JS::Realm& realm, Vector<JS::Handle<DOMRect>> rect_handles) | 
					
						
							| 
									
										
										
										
											2022-02-12 16:38:54 +03:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2022-09-04 01:59:58 +02:00
										 |  |  |     Vector<JS::NonnullGCPtr<DOMRect>> rects; | 
					
						
							|  |  |  |     for (auto& rect : rect_handles) | 
					
						
							|  |  |  |         rects.append(*rect); | 
					
						
							| 
									
										
										
										
											2023-01-28 13:39:44 -05:00
										 |  |  |     return realm.heap().allocate<DOMRectList>(realm, realm, move(rects)).release_allocated_value_but_fixme_should_propagate_errors(); | 
					
						
							| 
									
										
										
										
											2022-02-12 16:38:54 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-25 18:03:02 -06:00
										 |  |  | DOMRectList::DOMRectList(JS::Realm& realm, Vector<JS::NonnullGCPtr<DOMRect>> rects) | 
					
						
							| 
									
										
										
										
											2023-01-10 06:56:59 -05:00
										 |  |  |     : Bindings::LegacyPlatformObject(realm) | 
					
						
							| 
									
										
										
										
											2022-09-04 01:59:58 +02:00
										 |  |  |     , m_rects(move(rects)) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DOMRectList::~DOMRectList() = default; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-28 12:33:35 -05:00
										 |  |  | JS::ThrowCompletionOr<void> DOMRectList::initialize(JS::Realm& realm) | 
					
						
							| 
									
										
										
										
											2023-01-10 06:56:59 -05:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2023-01-28 12:33:35 -05:00
										 |  |  |     MUST_OR_THROW_OOM(Base::initialize(realm)); | 
					
						
							| 
									
										
										
										
											2023-01-10 06:56:59 -05:00
										 |  |  |     set_prototype(&Bindings::ensure_web_prototype<Bindings::DOMRectListPrototype>(realm, "DOMRectList")); | 
					
						
							| 
									
										
										
										
											2023-01-28 12:33:35 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return {}; | 
					
						
							| 
									
										
										
										
											2023-01-10 06:56:59 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-12 16:38:54 +03:00
										 |  |  | // https://drafts.fxtf.org/geometry-1/#dom-domrectlist-length
 | 
					
						
							|  |  |  | u32 DOMRectList::length() const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return m_rects.size(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // https://drafts.fxtf.org/geometry-1/#dom-domrectlist-item
 | 
					
						
							|  |  |  | DOMRect const* DOMRectList::item(u32 index) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     // The item(index) method, when invoked, must return null when
 | 
					
						
							|  |  |  |     // index is greater than or equal to the number of DOMRect objects associated with the DOMRectList.
 | 
					
						
							|  |  |  |     // Otherwise, the DOMRect object at index must be returned. Indices are zero-based.
 | 
					
						
							|  |  |  |     if (index >= m_rects.size()) | 
					
						
							|  |  |  |         return nullptr; | 
					
						
							| 
									
										
										
										
											2022-09-04 01:59:58 +02:00
										 |  |  |     return m_rects[index]; | 
					
						
							| 
									
										
										
										
											2022-02-12 16:38:54 +03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool DOMRectList::is_supported_property_index(u32 index) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return index < m_rects.size(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-04 01:59:58 +02:00
										 |  |  | JS::Value DOMRectList::item_value(size_t index) const | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (index >= m_rects.size()) | 
					
						
							|  |  |  |         return JS::js_undefined(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return m_rects[index].ptr(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-12 16:38:54 +03:00
										 |  |  | } |