| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											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
										 |  |  | #include "DOMTreeModel.h"
 | 
					
						
							|  |  |  | #include <AK/StringBuilder.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:32:51 +01:00
										 |  |  | #include <LibWeb/DOM/Document.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/Element.h>
 | 
					
						
							|  |  |  | #include <LibWeb/DOM/Text.h>
 | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  | #include <ctype.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | namespace Web { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-07-26 19:37:56 +02:00
										 |  |  | DOMTreeModel::DOMTreeModel(DOM::Document& document) | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  |     : m_document(document) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2021-07-21 18:02:15 +02:00
										 |  |  |     m_document_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-html.png")); | 
					
						
							|  |  |  |     m_element_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/inspector-object.png")); | 
					
						
							|  |  |  |     m_text_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-unknown.png")); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | DOMTreeModel::~DOMTreeModel() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | GUI::ModelIndex DOMTreeModel::index(int row, int column, const GUI::ModelIndex& parent) const | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!parent.is_valid()) { | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:20 +01:00
										 |  |  |         return create_index(row, column, m_document.ptr()); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-07-26 19:37:56 +02:00
										 |  |  |     auto& parent_node = *static_cast<DOM::Node*>(parent.internal_data()); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  |     return create_index(row, column, parent_node.child_at_index(row)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | GUI::ModelIndex DOMTreeModel::parent_index(const GUI::ModelIndex& index) const | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!index.is_valid()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							| 
									
										
										
										
											2020-07-26 19:37:56 +02:00
										 |  |  |     auto& node = *static_cast<DOM::Node*>(index.internal_data()); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  |     if (!node.parent()) | 
					
						
							|  |  |  |         return {}; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-19 22:30:33 +01:00
										 |  |  |     // FIXME: Handle the template element (child elements are not stored in it, all of its children are in its document fragment "content")
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  |     // No grandparent? Parent is the document!
 | 
					
						
							|  |  |  |     if (!node.parent()->parent()) { | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:20 +01:00
										 |  |  |         return create_index(0, 0, m_document.ptr()); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     // Walk the grandparent's children to find the index of node's parent in its parent.
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  |     // (This is needed to produce the row number of the GUI::ModelIndex corresponding to node's parent.)
 | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  |     int grandparent_child_index = 0; | 
					
						
							|  |  |  |     for (auto* grandparent_child = node.parent()->parent()->first_child(); grandparent_child; grandparent_child = grandparent_child->next_sibling()) { | 
					
						
							|  |  |  |         if (grandparent_child == node.parent()) | 
					
						
							|  |  |  |             return create_index(grandparent_child_index, 0, node.parent()); | 
					
						
							|  |  |  |         ++grandparent_child_index; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-02-23 20:42:32 +01:00
										 |  |  |     VERIFY_NOT_REACHED(); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | int DOMTreeModel::row_count(const GUI::ModelIndex& index) const | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     if (!index.is_valid()) | 
					
						
							|  |  |  |         return 1; | 
					
						
							| 
									
										
										
										
											2020-07-26 19:37:56 +02:00
										 |  |  |     auto& node = *static_cast<DOM::Node*>(index.internal_data()); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  |     return node.child_count(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 15:07:41 +01:00
										 |  |  | int DOMTreeModel::column_count(const GUI::ModelIndex&) const | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  | { | 
					
						
							|  |  |  |     return 1; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static String with_whitespace_collapsed(const StringView& string) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     StringBuilder builder; | 
					
						
							| 
									
										
										
										
											2019-12-09 17:45:40 +01:00
										 |  |  |     for (size_t i = 0; i < string.length(); ++i) { | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  |         if (isspace(string[i])) { | 
					
						
							|  |  |  |             builder.append(' '); | 
					
						
							|  |  |  |             while (i < string.length()) { | 
					
						
							|  |  |  |                 if (isspace(string[i])) { | 
					
						
							|  |  |  |                     ++i; | 
					
						
							|  |  |  |                     continue; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 builder.append(string[i]); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             continue; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         builder.append(string[i]); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return builder.to_string(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-08-16 16:00:07 +02:00
										 |  |  | GUI::Variant DOMTreeModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-07-26 19:37:56 +02:00
										 |  |  |     auto& node = *static_cast<DOM::Node*>(index.internal_data()); | 
					
						
							| 
									
										
										
										
											2020-08-16 16:00:07 +02:00
										 |  |  |     if (role == GUI::ModelRole::Icon) { | 
					
						
							| 
									
										
										
										
											2020-01-02 14:53:38 +01:00
										 |  |  |         if (node.is_document()) | 
					
						
							| 
									
										
										
										
											2019-11-09 11:58:20 +01:00
										 |  |  |             return m_document_icon; | 
					
						
							| 
									
										
										
										
											2020-01-02 14:53:38 +01:00
										 |  |  |         if (node.is_element()) | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  |             return m_element_icon; | 
					
						
							|  |  |  |         // FIXME: More node type icons?
 | 
					
						
							|  |  |  |         return m_text_icon; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-08-16 16:00:07 +02:00
										 |  |  |     if (role == GUI::ModelRole::Display) { | 
					
						
							| 
									
										
										
										
											2020-01-02 14:53:38 +01:00
										 |  |  |         if (node.is_text()) | 
					
						
							| 
									
										
										
										
											2021-06-24 19:53:42 +02:00
										 |  |  |             return with_whitespace_collapsed(verify_cast<DOM::Text>(node).data()); | 
					
						
							| 
									
										
										
										
											2020-01-02 14:53:38 +01:00
										 |  |  |         if (!node.is_element()) | 
					
						
							| 
									
										
										
										
											2020-06-16 19:09:14 +02:00
										 |  |  |             return node.node_name(); | 
					
						
							| 
									
										
										
										
											2021-06-24 19:53:42 +02:00
										 |  |  |         auto& element = verify_cast<DOM::Element>(node); | 
					
						
							| 
									
										
										
										
											2020-01-02 14:53:38 +01:00
										 |  |  |         StringBuilder builder; | 
					
						
							|  |  |  |         builder.append('<'); | 
					
						
							| 
									
										
										
										
											2020-07-23 18:18:13 +02:00
										 |  |  |         builder.append(element.local_name()); | 
					
						
							| 
									
										
										
										
											2020-01-02 14:53:38 +01:00
										 |  |  |         element.for_each_attribute([&](auto& name, auto& value) { | 
					
						
							|  |  |  |             builder.append(' '); | 
					
						
							|  |  |  |             builder.append(name); | 
					
						
							|  |  |  |             builder.append('='); | 
					
						
							|  |  |  |             builder.append('"'); | 
					
						
							|  |  |  |             builder.append(value); | 
					
						
							|  |  |  |             builder.append('"'); | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  |         builder.append('>'); | 
					
						
							|  |  |  |         return builder.to_string(); | 
					
						
							| 
									
										
										
										
											2019-11-09 11:31:03 +01:00
										 |  |  |     } | 
					
						
							|  |  |  |     return {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | } |