| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> | 
					
						
							|  |  |  |  * All rights reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Redistribution and use in source and binary forms, with or without | 
					
						
							|  |  |  |  * modification, are permitted provided that the following conditions are met: | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 1. Redistributions of source code must retain the above copyright notice, this | 
					
						
							|  |  |  |  *    list of conditions and the following disclaimer. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * 2. Redistributions in binary form must reproduce the above copyright notice, | 
					
						
							|  |  |  |  *    this list of conditions and the following disclaimer in the documentation | 
					
						
							|  |  |  |  *    and/or other materials provided with the distribution. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
					
						
							|  |  |  |  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
					
						
							|  |  |  |  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
					
						
							|  |  |  |  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
					
						
							|  |  |  |  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
					
						
							|  |  |  |  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
					
						
							|  |  |  |  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
					
						
							|  |  |  |  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
					
						
							|  |  |  |  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
					
						
							|  |  |  |  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											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) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-02-06 11:56:38 +01:00
										 |  |  |     m_document_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png")); | 
					
						
							|  |  |  |     m_element_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png")); | 
					
						
							|  |  |  |     m_text_icon.set_bitmap_for_size(16, Gfx::Bitmap::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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ASSERT_NOT_REACHED(); | 
					
						
							|  |  |  |     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-01-03 14:05:46 +01:00
										 |  |  |             return with_whitespace_collapsed(downcast<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(); | 
					
						
							| 
									
										
										
										
											2020-07-26 19:37:56 +02:00
										 |  |  |         auto& element = downcast<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 {}; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void DOMTreeModel::update() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     did_update(); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2020-03-07 10:27:02 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | } |