Serenity Operating System
at hosted 157 lines 5.4 kB view raw
1/* 2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include "DOMTreeModel.h" 28#include <AK/StringBuilder.h> 29#include <LibWeb/DOM/Document.h> 30#include <LibWeb/DOM/Element.h> 31#include <LibWeb/DOM/Text.h> 32#include <ctype.h> 33#include <stdio.h> 34 35namespace Web { 36 37DOMTreeModel::DOMTreeModel(Document& document) 38 : m_document(document) 39{ 40 m_document_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png")); 41 m_element_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png")); 42 m_text_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-unknown.png")); 43} 44 45DOMTreeModel::~DOMTreeModel() 46{ 47} 48 49GUI::ModelIndex DOMTreeModel::index(int row, int column, const GUI::ModelIndex& parent) const 50{ 51 if (!parent.is_valid()) { 52 return create_index(row, column, m_document.ptr()); 53 } 54 auto& parent_node = *static_cast<Node*>(parent.internal_data()); 55 return create_index(row, column, parent_node.child_at_index(row)); 56} 57 58GUI::ModelIndex DOMTreeModel::parent_index(const GUI::ModelIndex& index) const 59{ 60 if (!index.is_valid()) 61 return {}; 62 auto& node = *static_cast<Node*>(index.internal_data()); 63 if (!node.parent()) 64 return {}; 65 66 // No grandparent? Parent is the document! 67 if (!node.parent()->parent()) { 68 return create_index(0, 0, m_document.ptr()); 69 } 70 71 // Walk the grandparent's children to find the index of node's parent in its parent. 72 // (This is needed to produce the row number of the GUI::ModelIndex corresponding to node's parent.) 73 int grandparent_child_index = 0; 74 for (auto* grandparent_child = node.parent()->parent()->first_child(); grandparent_child; grandparent_child = grandparent_child->next_sibling()) { 75 if (grandparent_child == node.parent()) 76 return create_index(grandparent_child_index, 0, node.parent()); 77 ++grandparent_child_index; 78 } 79 80 ASSERT_NOT_REACHED(); 81 return {}; 82} 83 84int DOMTreeModel::row_count(const GUI::ModelIndex& index) const 85{ 86 if (!index.is_valid()) 87 return 1; 88 auto& node = *static_cast<Node*>(index.internal_data()); 89 return node.child_count(); 90} 91 92int DOMTreeModel::column_count(const GUI::ModelIndex&) const 93{ 94 return 1; 95} 96 97static String with_whitespace_collapsed(const StringView& string) 98{ 99 StringBuilder builder; 100 for (size_t i = 0; i < string.length(); ++i) { 101 if (isspace(string[i])) { 102 builder.append(' '); 103 while (i < string.length()) { 104 if (isspace(string[i])) { 105 ++i; 106 continue; 107 } 108 builder.append(string[i]); 109 break; 110 } 111 continue; 112 } 113 builder.append(string[i]); 114 } 115 return builder.to_string(); 116} 117 118GUI::Variant DOMTreeModel::data(const GUI::ModelIndex& index, Role role) const 119{ 120 auto& node = *static_cast<Node*>(index.internal_data()); 121 if (role == Role::Icon) { 122 if (node.is_document()) 123 return m_document_icon; 124 if (node.is_element()) 125 return m_element_icon; 126 // FIXME: More node type icons? 127 return m_text_icon; 128 } 129 if (role == Role::Display) { 130 if (node.is_text()) 131 return String::format("%s", with_whitespace_collapsed(to<Text>(node).data()).characters()); 132 if (!node.is_element()) 133 return node.tag_name(); 134 auto& element = to<Element>(node); 135 StringBuilder builder; 136 builder.append('<'); 137 builder.append(element.tag_name()); 138 element.for_each_attribute([&](auto& name, auto& value) { 139 builder.append(' '); 140 builder.append(name); 141 builder.append('='); 142 builder.append('"'); 143 builder.append(value); 144 builder.append('"'); 145 }); 146 builder.append('>'); 147 return builder.to_string(); 148 } 149 return {}; 150} 151 152void DOMTreeModel::update() 153{ 154 did_update(); 155} 156 157}