Serenity Operating System
at portability 153 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 <LibHTML/DOM/Document.h> 30#include <LibHTML/DOM/Element.h> 31#include <LibHTML/DOM/Text.h> 32#include <ctype.h> 33#include <stdio.h> 34 35DOMTreeModel::DOMTreeModel(Document& document) 36 : m_document(document) 37{ 38 m_document_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png")); 39 m_element_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png")); 40 m_text_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-unknown.png")); 41} 42 43DOMTreeModel::~DOMTreeModel() 44{ 45} 46 47GUI::ModelIndex DOMTreeModel::index(int row, int column, const GUI::ModelIndex& parent) const 48{ 49 if (!parent.is_valid()) { 50 return create_index(row, column, m_document.ptr()); 51 } 52 auto& parent_node = *static_cast<Node*>(parent.internal_data()); 53 return create_index(row, column, parent_node.child_at_index(row)); 54} 55 56GUI::ModelIndex DOMTreeModel::parent_index(const GUI::ModelIndex& index) const 57{ 58 if (!index.is_valid()) 59 return {}; 60 auto& node = *static_cast<Node*>(index.internal_data()); 61 if (!node.parent()) 62 return {}; 63 64 // No grandparent? Parent is the document! 65 if (!node.parent()->parent()) { 66 return create_index(0, 0, m_document.ptr()); 67 } 68 69 // Walk the grandparent's children to find the index of node's parent in its parent. 70 // (This is needed to produce the row number of the GUI::ModelIndex corresponding to node's parent.) 71 int grandparent_child_index = 0; 72 for (auto* grandparent_child = node.parent()->parent()->first_child(); grandparent_child; grandparent_child = grandparent_child->next_sibling()) { 73 if (grandparent_child == node.parent()) 74 return create_index(grandparent_child_index, 0, node.parent()); 75 ++grandparent_child_index; 76 } 77 78 ASSERT_NOT_REACHED(); 79 return {}; 80} 81 82int DOMTreeModel::row_count(const GUI::ModelIndex& index) const 83{ 84 if (!index.is_valid()) 85 return 1; 86 auto& node = *static_cast<Node*>(index.internal_data()); 87 return node.child_count(); 88} 89 90int DOMTreeModel::column_count(const GUI::ModelIndex&) const 91{ 92 return 1; 93} 94 95static String with_whitespace_collapsed(const StringView& string) 96{ 97 StringBuilder builder; 98 for (size_t i = 0; i < string.length(); ++i) { 99 if (isspace(string[i])) { 100 builder.append(' '); 101 while (i < string.length()) { 102 if (isspace(string[i])) { 103 ++i; 104 continue; 105 } 106 builder.append(string[i]); 107 break; 108 } 109 continue; 110 } 111 builder.append(string[i]); 112 } 113 return builder.to_string(); 114} 115 116GUI::Variant DOMTreeModel::data(const GUI::ModelIndex& index, Role role) const 117{ 118 auto& node = *static_cast<Node*>(index.internal_data()); 119 if (role == Role::Icon) { 120 if (node.is_document()) 121 return m_document_icon; 122 if (node.is_element()) 123 return m_element_icon; 124 // FIXME: More node type icons? 125 return m_text_icon; 126 } 127 if (role == Role::Display) { 128 if (node.is_text()) 129 return String::format("%s", with_whitespace_collapsed(to<Text>(node).data()).characters()); 130 if (!node.is_element()) 131 return node.tag_name(); 132 auto& element = to<Element>(node); 133 StringBuilder builder; 134 builder.append('<'); 135 builder.append(element.tag_name()); 136 element.for_each_attribute([&](auto& name, auto& value) { 137 builder.append(' '); 138 builder.append(name); 139 builder.append('='); 140 builder.append('"'); 141 builder.append(value); 142 builder.append('"'); 143 }); 144 builder.append('>'); 145 return builder.to_string(); 146 } 147 return {}; 148} 149 150void DOMTreeModel::update() 151{ 152 did_update(); 153}