DOMTreeModel.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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() = default;
  24. GUI::ModelIndex DOMTreeModel::index(int row, int column, const GUI::ModelIndex& parent) const
  25. {
  26. if (!parent.is_valid()) {
  27. return create_index(row, column, &m_dom_tree);
  28. }
  29. auto const& parent_node = *static_cast<JsonObject const*>(parent.internal_data());
  30. auto const* children = get_children(parent_node);
  31. if (!children)
  32. return create_index(row, column, &m_dom_tree);
  33. auto const& child_node = children->at(row).as_object();
  34. return create_index(row, column, &child_node);
  35. }
  36. GUI::ModelIndex DOMTreeModel::parent_index(const GUI::ModelIndex& index) const
  37. {
  38. // FIXME: Handle the template element (child elements are not stored in it, all of its children are in its document fragment "content")
  39. // Probably in the JSON generation in Node.cpp?
  40. if (!index.is_valid())
  41. return {};
  42. auto const& node = *static_cast<JsonObject const*>(index.internal_data());
  43. auto const* parent_node = get_parent(node);
  44. if (!parent_node)
  45. return {};
  46. // If the parent is the root document, we know it has index 0, 0
  47. if (parent_node == &m_dom_tree) {
  48. return create_index(0, 0, parent_node);
  49. }
  50. // Otherwise, we need to find the grandparent, to find the index of parent within that
  51. auto const* grandparent_node = get_parent(*parent_node);
  52. VERIFY(grandparent_node);
  53. auto const* grandparent_children = get_children(*grandparent_node);
  54. if (!grandparent_children)
  55. return {};
  56. for (size_t grandparent_child_index = 0; grandparent_child_index < grandparent_children->size(); ++grandparent_child_index) {
  57. auto const& child = grandparent_children->at(grandparent_child_index).as_object();
  58. if (&child == parent_node)
  59. return create_index(grandparent_child_index, 0, parent_node);
  60. }
  61. return {};
  62. }
  63. int DOMTreeModel::row_count(const GUI::ModelIndex& index) const
  64. {
  65. if (!index.is_valid())
  66. return 1;
  67. auto const& node = *static_cast<JsonObject const*>(index.internal_data());
  68. auto const* children = get_children(node);
  69. return children ? children->size() : 0;
  70. }
  71. int DOMTreeModel::column_count(const GUI::ModelIndex&) const
  72. {
  73. return 1;
  74. }
  75. static String with_whitespace_collapsed(StringView string)
  76. {
  77. StringBuilder builder;
  78. for (size_t i = 0; i < string.length(); ++i) {
  79. if (isspace(string[i])) {
  80. builder.append(' ');
  81. while (i < string.length()) {
  82. if (isspace(string[i])) {
  83. ++i;
  84. continue;
  85. }
  86. builder.append(string[i]);
  87. break;
  88. }
  89. continue;
  90. }
  91. builder.append(string[i]);
  92. }
  93. return builder.to_string();
  94. }
  95. GUI::Variant DOMTreeModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  96. {
  97. auto const& node = *static_cast<JsonObject const*>(index.internal_data());
  98. auto node_name = node.get("name").as_string();
  99. auto type = node.get("type").as_string_or("unknown");
  100. if (role == GUI::ModelRole::ForegroundColor) {
  101. // FIXME: Allow models to return a foreground color *role*.
  102. // Then we won't need to have a GUI::TreeView& member anymore.
  103. if (type == "comment"sv)
  104. return m_tree_view.palette().syntax_comment();
  105. if (type == "pseudo-element"sv)
  106. return m_tree_view.palette().syntax_type();
  107. if (!node.get("visible").to_bool(true))
  108. return m_tree_view.palette().syntax_comment();
  109. return {};
  110. }
  111. if (role == GUI::ModelRole::Icon) {
  112. if (type == "document")
  113. return m_document_icon;
  114. if (type == "element")
  115. return m_element_icon;
  116. // FIXME: More node type icons?
  117. return m_text_icon;
  118. }
  119. if (role == GUI::ModelRole::Display) {
  120. if (type == "text")
  121. return with_whitespace_collapsed(node.get("text").as_string());
  122. if (type == "comment"sv)
  123. return String::formatted("<!--{}-->", node.get("data"sv).as_string());
  124. if (type != "element")
  125. return node_name;
  126. StringBuilder builder;
  127. builder.append('<');
  128. builder.append(node_name.to_lowercase());
  129. if (node.has("attributes")) {
  130. auto attributes = node.get("attributes").as_object();
  131. attributes.for_each_member([&builder](auto& name, JsonValue const& value) {
  132. builder.append(' ');
  133. builder.append(name);
  134. builder.append('=');
  135. builder.append('"');
  136. builder.append(value.to_string());
  137. builder.append('"');
  138. });
  139. }
  140. builder.append('>');
  141. return builder.to_string();
  142. }
  143. return {};
  144. }
  145. void DOMTreeModel::map_dom_nodes_to_parent(JsonObject const* parent, JsonObject const* node)
  146. {
  147. m_dom_node_to_parent_map.set(node, parent);
  148. m_node_id_to_dom_node_map.set(node->get("id").to_i32(), node);
  149. auto const* children = get_children(*node);
  150. if (!children)
  151. return;
  152. children->for_each([&](auto const& child) {
  153. auto const& child_node = child.as_object();
  154. map_dom_nodes_to_parent(node, &child_node);
  155. });
  156. }
  157. GUI::ModelIndex DOMTreeModel::index_for_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> pseudo_element) const
  158. {
  159. auto node = m_node_id_to_dom_node_map.get(node_id).value_or(nullptr);
  160. if (node) {
  161. if (pseudo_element.has_value()) {
  162. // Find pseudo-element child of the node.
  163. auto node_children = get_children(*node);
  164. for (size_t i = 0; i < node_children->size(); i++) {
  165. auto& child = node_children->at(i).as_object();
  166. if (!child.has("pseudo-element"))
  167. continue;
  168. auto child_pseudo_element = child.get("pseudo-element");
  169. if (!child_pseudo_element.is_i32())
  170. continue;
  171. if (child_pseudo_element.as_i32() == to_underlying(pseudo_element.value()))
  172. return create_index(i, 0, &child);
  173. }
  174. } else {
  175. auto* parent = get_parent(*node);
  176. if (!parent)
  177. return {};
  178. auto parent_children = get_children(*parent);
  179. for (size_t i = 0; i < parent_children->size(); i++) {
  180. if (&parent_children->at(i).as_object() == node) {
  181. return create_index(i, 0, node);
  182. }
  183. }
  184. }
  185. }
  186. dbgln("Didn't find index for node {}, pseudo-element {}!", node_id, pseudo_element.has_value() ? CSS::pseudo_element_name(pseudo_element.value()) : "NONE");
  187. return {};
  188. }
  189. }