DOMTreeModel.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2018-2020, Adam Hodgen <ant1441@gmail.com>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include "DOMTreeModel.h"
  8. #include <AK/JsonObject.h>
  9. #include <AK/StringBuilder.h>
  10. #include <LibGUI/TreeView.h>
  11. #include <LibGfx/Palette.h>
  12. #include <ctype.h>
  13. namespace Web {
  14. DOMTreeModel::DOMTreeModel(JsonObject dom_tree, GUI::TreeView& tree_view)
  15. : m_tree_view(tree_view)
  16. , m_dom_tree(move(dom_tree))
  17. {
  18. m_document_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-html.png").release_value_but_fixme_should_propagate_errors());
  19. m_element_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/inspector-object.png").release_value_but_fixme_should_propagate_errors());
  20. m_text_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-unknown.png").release_value_but_fixme_should_propagate_errors());
  21. map_dom_nodes_to_parent(nullptr, &m_dom_tree);
  22. }
  23. DOMTreeModel::~DOMTreeModel()
  24. {
  25. }
  26. GUI::ModelIndex DOMTreeModel::index(int row, int column, const GUI::ModelIndex& parent) const
  27. {
  28. if (!parent.is_valid()) {
  29. return create_index(row, column, &m_dom_tree);
  30. }
  31. auto const& parent_node = *static_cast<JsonObject const*>(parent.internal_data());
  32. auto const* children = get_children(parent_node);
  33. if (!children)
  34. return create_index(row, column, &m_dom_tree);
  35. auto const& child_node = children->at(row).as_object();
  36. return create_index(row, column, &child_node);
  37. }
  38. GUI::ModelIndex DOMTreeModel::parent_index(const GUI::ModelIndex& index) const
  39. {
  40. // FIXME: Handle the template element (child elements are not stored in it, all of its children are in its document fragment "content")
  41. // Probably in the JSON generation in Node.cpp?
  42. if (!index.is_valid())
  43. return {};
  44. auto const& node = *static_cast<JsonObject const*>(index.internal_data());
  45. auto const* parent_node = get_parent(node);
  46. if (!parent_node)
  47. return {};
  48. // If the parent is the root document, we know it has index 0, 0
  49. if (parent_node == &m_dom_tree) {
  50. return create_index(0, 0, parent_node);
  51. }
  52. // Otherwise, we need to find the grandparent, to find the index of parent within that
  53. auto const* grandparent_node = get_parent(*parent_node);
  54. VERIFY(grandparent_node);
  55. auto const* grandparent_children = get_children(*grandparent_node);
  56. if (!grandparent_children)
  57. return {};
  58. for (size_t grandparent_child_index = 0; grandparent_child_index < grandparent_children->size(); ++grandparent_child_index) {
  59. auto const& child = grandparent_children->at(grandparent_child_index).as_object();
  60. if (&child == parent_node)
  61. return create_index(grandparent_child_index, 0, parent_node);
  62. }
  63. return {};
  64. }
  65. int DOMTreeModel::row_count(const GUI::ModelIndex& index) const
  66. {
  67. if (!index.is_valid())
  68. return 1;
  69. auto const& node = *static_cast<JsonObject const*>(index.internal_data());
  70. auto const* children = get_children(node);
  71. return children ? children->size() : 0;
  72. }
  73. int DOMTreeModel::column_count(const GUI::ModelIndex&) const
  74. {
  75. return 1;
  76. }
  77. static String with_whitespace_collapsed(const StringView& string)
  78. {
  79. StringBuilder builder;
  80. for (size_t i = 0; i < string.length(); ++i) {
  81. if (isspace(string[i])) {
  82. builder.append(' ');
  83. while (i < string.length()) {
  84. if (isspace(string[i])) {
  85. ++i;
  86. continue;
  87. }
  88. builder.append(string[i]);
  89. break;
  90. }
  91. continue;
  92. }
  93. builder.append(string[i]);
  94. }
  95. return builder.to_string();
  96. }
  97. GUI::Variant DOMTreeModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  98. {
  99. auto const& node = *static_cast<JsonObject const*>(index.internal_data());
  100. auto node_name = node.get("name").as_string();
  101. auto type = node.get("type").as_string_or("unknown");
  102. if (role == GUI::ModelRole::ForegroundColor) {
  103. // FIXME: Allow models to return a foreground color *role*.
  104. // Then we won't need to have a GUI::TreeView& member anymore.
  105. if (type == "comment"sv)
  106. return m_tree_view.palette().syntax_comment();
  107. return {};
  108. }
  109. if (role == GUI::ModelRole::Icon) {
  110. if (type == "document")
  111. return m_document_icon;
  112. if (type == "element")
  113. return m_element_icon;
  114. // FIXME: More node type icons?
  115. return m_text_icon;
  116. }
  117. if (role == GUI::ModelRole::Display) {
  118. if (type == "text")
  119. return with_whitespace_collapsed(node.get("text").as_string());
  120. if (type == "comment"sv)
  121. return String::formatted("<!--{}-->", node.get("data"sv).as_string());
  122. if (type != "element")
  123. return node_name;
  124. StringBuilder builder;
  125. builder.append('<');
  126. builder.append(node_name.to_lowercase());
  127. if (node.has("attributes")) {
  128. auto attributes = node.get("attributes").as_object();
  129. attributes.for_each_member([&builder](auto& name, JsonValue const& value) {
  130. builder.append(' ');
  131. builder.append(name);
  132. builder.append('=');
  133. builder.append('"');
  134. builder.append(value.to_string());
  135. builder.append('"');
  136. });
  137. }
  138. builder.append('>');
  139. return builder.to_string();
  140. }
  141. return {};
  142. }
  143. void DOMTreeModel::map_dom_nodes_to_parent(JsonObject const* parent, JsonObject const* node)
  144. {
  145. m_dom_node_to_parent_map.set(node, parent);
  146. m_node_id_to_dom_node_map.set(node->get("id").to_i32(), node);
  147. auto const* children = get_children(*node);
  148. if (!children)
  149. return;
  150. children->for_each([&](auto const& child) {
  151. auto const& child_node = child.as_object();
  152. map_dom_nodes_to_parent(node, &child_node);
  153. });
  154. }
  155. GUI::ModelIndex DOMTreeModel::index_for_node(i32 node_id) const
  156. {
  157. auto node = m_node_id_to_dom_node_map.get(node_id).value_or(nullptr);
  158. if (node) {
  159. auto* parent = get_parent(*node);
  160. if (!parent)
  161. return {};
  162. auto parent_children = get_children(*parent);
  163. for (size_t i = 0; i < parent_children->size(); i++) {
  164. if (&parent_children->at(i).as_object() == node) {
  165. return create_index(i, 0, node);
  166. }
  167. }
  168. }
  169. dbgln("Didn't find index for node {}!", node_id);
  170. return {};
  171. }
  172. }