| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							| 
									
										
										
										
											2021-08-24 17:04:30 +01:00
										 |  |  |  * Copyright (c) 2018-2020, Adam Hodgen <ant1441@gmail.com> | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-24 17:04:30 +01:00
										 |  |  | #include <AK/HashMap.h>
 | 
					
						
							|  |  |  | #include <AK/JsonObject.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-06 20:33:02 +01:00
										 |  |  | #include <LibGUI/Model.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-26 19:37:56 +02:00
										 |  |  | #include <LibWeb/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | namespace Web { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | class DOMTreeModel final : public GUI::Model { | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-11-02 19:32:26 +01:00
										 |  |  |     static NonnullRefPtr<DOMTreeModel> create(StringView dom_tree, GUI::TreeView& tree_view) | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2021-08-24 17:04:30 +01:00
										 |  |  |         auto json_or_error = JsonValue::from_string(dom_tree); | 
					
						
							|  |  |  |         if (!json_or_error.has_value()) | 
					
						
							|  |  |  |             VERIFY_NOT_REACHED(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-02 19:32:26 +01:00
										 |  |  |         return adopt_ref(*new DOMTreeModel(json_or_error.value().as_object(), tree_view)); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     virtual ~DOMTreeModel() override; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     virtual int row_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override; | 
					
						
							|  |  |  |     virtual int column_count(const GUI::ModelIndex& = GUI::ModelIndex()) const override; | 
					
						
							| 
									
										
										
										
											2020-08-16 16:00:07 +02:00
										 |  |  |     virtual GUI::Variant data(const GUI::ModelIndex&, GUI::ModelRole) const override; | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     virtual GUI::ModelIndex index(int row, int column, const GUI::ModelIndex& parent = GUI::ModelIndex()) const override; | 
					
						
							|  |  |  |     virtual GUI::ModelIndex parent_index(const GUI::ModelIndex&) const override; | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-27 17:36:10 +01:00
										 |  |  |     GUI::ModelIndex index_for_node(i32 node_id) const; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2021-11-02 19:32:26 +01:00
										 |  |  |     DOMTreeModel(JsonObject, GUI::TreeView&); | 
					
						
							| 
									
										
										
										
											2021-08-24 17:04:30 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     ALWAYS_INLINE JsonObject const* get_parent(const JsonObject& o) const | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         auto parent_node = m_dom_node_to_parent_map.get(&o); | 
					
						
							|  |  |  |         VERIFY(parent_node.has_value()); | 
					
						
							|  |  |  |         return *parent_node; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ALWAYS_INLINE static JsonArray const* get_children(const JsonObject& o) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (auto const* maybe_children = o.get_ptr("children"); maybe_children) | 
					
						
							|  |  |  |             return &maybe_children->as_array(); | 
					
						
							|  |  |  |         return nullptr; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-24 17:04:30 +01:00
										 |  |  |     void map_dom_nodes_to_parent(JsonObject const* parent, JsonObject const* child); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-02 19:32:26 +01:00
										 |  |  |     GUI::TreeView& m_tree_view; | 
					
						
							| 
									
										
										
										
											2020-03-07 12:02:21 +13:00
										 |  |  |     GUI::Icon m_document_icon; | 
					
						
							|  |  |  |     GUI::Icon m_element_icon; | 
					
						
							|  |  |  |     GUI::Icon m_text_icon; | 
					
						
							| 
									
										
										
										
											2021-08-24 17:04:30 +01:00
										 |  |  |     JsonObject m_dom_tree; | 
					
						
							|  |  |  |     HashMap<JsonObject const*, JsonObject const*> m_dom_node_to_parent_map; | 
					
						
							| 
									
										
										
										
											2021-08-27 17:36:10 +01:00
										 |  |  |     HashMap<i32, JsonObject const*> m_node_id_to_dom_node_map; | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |