DOMTreeModel.cpp 8.2 KB

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