| 
									
										
										
										
											2020-05-24 19:24:36 +02:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2022-10-17 10:46:11 +02:00
										 |  |  |  * Copyright (c) 2020-2022, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2020-05-24 19:24:36 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-05-24 19:24:36 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <AK/NonnullRefPtrVector.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/Element.h>
 | 
					
						
							|  |  |  | #include <LibWeb/Forward.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-28 18:20:36 +02:00
										 |  |  | namespace Web::HTML { | 
					
						
							| 
									
										
										
										
											2020-05-24 19:24:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-14 22:30:50 +01:00
										 |  |  | // https://html.spec.whatwg.org/multipage/parsing.html#stack-of-open-elements
 | 
					
						
							| 
									
										
										
										
											2020-05-24 19:24:36 +02:00
										 |  |  | class StackOfOpenElements { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2022-02-14 22:30:50 +01:00
										 |  |  |     // Initially, the stack of open elements is empty.
 | 
					
						
							|  |  |  |     // The stack grows downwards; the topmost node on the stack is the first one added to the stack,
 | 
					
						
							|  |  |  |     // and the bottommost node of the stack is the most recently added node in the stack
 | 
					
						
							|  |  |  |     // (notwithstanding when the stack is manipulated in a random access fashion as part of the handling for misnested tags).
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-14 13:21:51 -06:00
										 |  |  |     StackOfOpenElements() = default; | 
					
						
							| 
									
										
										
										
											2020-05-24 19:24:36 +02:00
										 |  |  |     ~StackOfOpenElements(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     DOM::Element& first() { return *m_elements.first(); } | 
					
						
							|  |  |  |     DOM::Element& last() { return *m_elements.last(); } | 
					
						
							| 
									
										
										
										
											2020-05-28 00:27:46 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-24 19:24:36 +02:00
										 |  |  |     bool is_empty() const { return m_elements.is_empty(); } | 
					
						
							| 
									
										
										
										
											2022-10-17 10:46:11 +02:00
										 |  |  |     void push(JS::NonnullGCPtr<DOM::Element> element) { m_elements.append(element); } | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     JS::NonnullGCPtr<DOM::Element> pop() { return *m_elements.take_last(); } | 
					
						
							| 
									
										
										
										
											2022-03-20 02:11:42 +01:00
										 |  |  |     void remove(DOM::Element const& element); | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     void replace(DOM::Element const& to_remove, JS::NonnullGCPtr<DOM::Element> to_add); | 
					
						
							|  |  |  |     void insert_immediately_below(JS::NonnullGCPtr<DOM::Element> element_to_add, DOM::Element const& target); | 
					
						
							| 
									
										
										
										
											2020-05-24 19:24:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     const DOM::Element& current_node() const { return *m_elements.last(); } | 
					
						
							|  |  |  |     DOM::Element& current_node() { return *m_elements.last(); } | 
					
						
							| 
									
										
										
										
											2020-05-24 19:24:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     bool has_in_scope(FlyString const& tag_name) const; | 
					
						
							|  |  |  |     bool has_in_button_scope(FlyString const& tag_name) const; | 
					
						
							|  |  |  |     bool has_in_table_scope(FlyString const& tag_name) const; | 
					
						
							|  |  |  |     bool has_in_list_item_scope(FlyString const& tag_name) const; | 
					
						
							|  |  |  |     bool has_in_select_scope(FlyString const& tag_name) const; | 
					
						
							| 
									
										
										
										
											2020-05-24 19:24:36 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 19:37:56 +02:00
										 |  |  |     bool has_in_scope(const DOM::Element&) const; | 
					
						
							| 
									
										
										
										
											2020-05-27 23:22:42 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 19:37:56 +02:00
										 |  |  |     bool contains(const DOM::Element&) const; | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     bool contains(FlyString const& tag_name) const; | 
					
						
							| 
									
										
										
										
											2020-05-24 22:39:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-17 10:46:11 +02:00
										 |  |  |     auto const& elements() const { return m_elements; } | 
					
						
							|  |  |  |     auto& elements() { return m_elements; } | 
					
						
							| 
									
										
										
										
											2020-05-24 22:39:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     void pop_until_an_element_with_tag_name_has_been_popped(FlyString const&); | 
					
						
							| 
									
										
										
										
											2020-05-28 18:09:31 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     JS::GCPtr<DOM::Element> topmost_special_node_below(DOM::Element const&); | 
					
						
							| 
									
										
										
										
											2020-05-29 22:06:05 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-19 22:30:33 +01:00
										 |  |  |     struct LastElementResult { | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |         JS::GCPtr<DOM::Element> element; | 
					
						
							| 
									
										
										
										
											2020-08-19 22:30:33 +01:00
										 |  |  |         ssize_t index; | 
					
						
							|  |  |  |     }; | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     LastElementResult last_element_with_tag_name(FlyString const&); | 
					
						
							| 
									
										
										
										
											2022-08-28 13:42:07 +02:00
										 |  |  |     JS::GCPtr<DOM::Element> element_immediately_above(DOM::Element const&); | 
					
						
							| 
									
										
										
										
											2020-06-21 17:00:55 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-17 10:46:11 +02:00
										 |  |  |     void visit_edges(JS::Cell::Visitor&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-24 19:24:36 +02:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2022-04-01 20:58:27 +03:00
										 |  |  |     bool has_in_scope_impl(FlyString const& tag_name, Vector<FlyString> const&) const; | 
					
						
							|  |  |  |     bool has_in_scope_impl(const DOM::Element& target_node, Vector<FlyString> const&) const; | 
					
						
							| 
									
										
										
										
											2020-05-24 22:21:25 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-17 10:46:11 +02:00
										 |  |  |     Vector<JS::NonnullGCPtr<DOM::Element>> m_elements; | 
					
						
							| 
									
										
										
										
											2020-05-24 19:24:36 +02:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |