DOMTreeJSONModel.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (c) 2018-2020, 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 "DOMTreeJSONModel.h"
  8. #include <AK/JsonObject.h>
  9. #include <AK/StringBuilder.h>
  10. #include <ctype.h>
  11. namespace Web {
  12. DOMTreeJSONModel::DOMTreeJSONModel(JsonObject dom_tree)
  13. : m_dom_tree(dom_tree)
  14. {
  15. m_document_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png"));
  16. m_element_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png"));
  17. m_text_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-unknown.png"));
  18. }
  19. DOMTreeJSONModel::~DOMTreeJSONModel()
  20. {
  21. }
  22. GUI::ModelIndex DOMTreeJSONModel::index(int row, int column, const GUI::ModelIndex& parent) const
  23. {
  24. if (!parent.is_valid()) {
  25. return create_index(row, column, (void*)get_internal_id(m_dom_tree));
  26. }
  27. auto parent_node = find_node(parent);
  28. auto children = get_children(parent_node);
  29. auto child_node = children[row].as_object();
  30. auto child_internal_id = (void*)get_internal_id(child_node);
  31. return create_index(row, column, child_internal_id);
  32. }
  33. GUI::ModelIndex DOMTreeJSONModel::parent_index(const GUI::ModelIndex& index) const
  34. {
  35. // FIXME: Handle the template element (child elements are not stored in it, all of its children are in its document fragment "content")
  36. // Probably in the JSON generation in Node.cpp?
  37. if (!index.is_valid())
  38. return {};
  39. auto node = find_node(index);
  40. auto node_internal_id = get_internal_id(node);
  41. auto parent_node = find_parent_of_child_with_internal_id(node_internal_id);
  42. if (!parent_node.has_value())
  43. return {};
  44. auto parent_node_internal_id = get_internal_id(parent_node.value());
  45. // If the parent is the root document, we know it has index 0, 0
  46. if (parent_node_internal_id == get_internal_id(m_dom_tree)) {
  47. return create_index(0, 0, (void*)parent_node_internal_id);
  48. }
  49. // Otherwise, we need to find the grandparent, to find the index of parent within that
  50. auto grandparent_node = find_parent_of_child_with_internal_id(parent_node_internal_id);
  51. VERIFY(grandparent_node.has_value());
  52. auto grandparent_children = get_children(*grandparent_node);
  53. if (grandparent_children.is_empty())
  54. return {};
  55. for (int grandparent_child_index = 0; grandparent_child_index < grandparent_children.size(); ++grandparent_child_index) {
  56. auto child = grandparent_children[grandparent_child_index].as_object();
  57. if (get_internal_id(child) == parent_node_internal_id)
  58. return create_index(grandparent_child_index, 0, (void*)(parent_node_internal_id));
  59. }
  60. return {};
  61. }
  62. int DOMTreeJSONModel::row_count(const GUI::ModelIndex& index) const
  63. {
  64. if (!index.is_valid())
  65. return 1;
  66. auto child = find_node(index);
  67. return get_children(child).size();
  68. }
  69. int DOMTreeJSONModel::column_count(const GUI::ModelIndex&) const
  70. {
  71. return 1;
  72. }
  73. static String with_whitespace_collapsed(const StringView& string)
  74. {
  75. StringBuilder builder;
  76. for (size_t i = 0; i < string.length(); ++i) {
  77. if (isspace(string[i])) {
  78. builder.append(' ');
  79. while (i < string.length()) {
  80. if (isspace(string[i])) {
  81. ++i;
  82. continue;
  83. }
  84. builder.append(string[i]);
  85. break;
  86. }
  87. continue;
  88. }
  89. builder.append(string[i]);
  90. }
  91. return builder.to_string();
  92. }
  93. GUI::Variant DOMTreeJSONModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  94. {
  95. auto node = find_node(index);
  96. auto node_name = node.get("name").as_string();
  97. auto type = node.get("type").as_string_or("unknown");
  98. if (role == GUI::ModelRole::Icon) {
  99. if (type == "document")
  100. return m_document_icon;
  101. if (type == "element")
  102. return m_element_icon;
  103. // FIXME: More node type icons?
  104. return m_text_icon;
  105. }
  106. if (role == GUI::ModelRole::Display) {
  107. if (type == "text")
  108. return with_whitespace_collapsed(node.get("text").as_string());
  109. if (type != "element")
  110. return node_name;
  111. StringBuilder builder;
  112. builder.append('<');
  113. builder.append(node_name.to_lowercase());
  114. if (node.has("attributes")) {
  115. auto attributes = node.get("attributes").as_object();
  116. attributes.for_each_member([&builder](auto& name, JsonValue& value) {
  117. builder.append(' ');
  118. builder.append(name);
  119. builder.append('=');
  120. builder.append('"');
  121. builder.append(value.to_string());
  122. builder.append('"');
  123. });
  124. }
  125. builder.append('>');
  126. return builder.to_string();
  127. }
  128. return {};
  129. }
  130. void DOMTreeJSONModel::update()
  131. {
  132. did_update();
  133. }
  134. Optional<JsonObject> DOMTreeJSONModel::find_parent_of_child_with_internal_id(size_t internal_id) const
  135. {
  136. return find_parent_of_child_with_internal_id(m_dom_tree, internal_id);
  137. }
  138. Optional<JsonObject> DOMTreeJSONModel::find_parent_of_child_with_internal_id(JsonObject node, size_t internal_id) const
  139. {
  140. auto children = get_children(node);
  141. for (int i = 0; i < children.size(); ++i) {
  142. auto child = children[i].as_object();
  143. auto child_internal_id = get_internal_id(child);
  144. if (child_internal_id == internal_id)
  145. return node;
  146. auto maybe_node = find_parent_of_child_with_internal_id(child, internal_id);
  147. if (maybe_node.has_value())
  148. return maybe_node;
  149. }
  150. return {};
  151. }
  152. Optional<JsonObject> DOMTreeJSONModel::find_child_with_internal_id(size_t internal_id) const
  153. {
  154. return find_child_with_internal_id(m_dom_tree, internal_id);
  155. }
  156. Optional<JsonObject> DOMTreeJSONModel::find_child_with_internal_id(JsonObject node, size_t internal_id) const
  157. {
  158. auto node_internal_id = get_internal_id(node);
  159. if (node_internal_id == internal_id) {
  160. return node;
  161. }
  162. auto children = get_children(node);
  163. for (int i = 0; i < children.size(); ++i) {
  164. auto child = children[i].as_object();
  165. auto maybe_node = find_child_with_internal_id(child, internal_id);
  166. if (maybe_node.has_value())
  167. return maybe_node;
  168. }
  169. return {};
  170. }
  171. size_t DOMTreeJSONModel::get_internal_id(JsonObject const& o)
  172. {
  173. return o.get("internal_id").as_u32();
  174. }
  175. JsonArray DOMTreeJSONModel::get_children(JsonObject const& o)
  176. {
  177. auto maybe_children = o.get("children");
  178. if (maybe_children.is_null())
  179. return {};
  180. return maybe_children.as_array();
  181. }
  182. JsonObject DOMTreeJSONModel::find_node(GUI::ModelIndex index) const
  183. {
  184. auto internal_id = (size_t)(index.internal_data());
  185. auto maybe_node = find_child_with_internal_id(internal_id);
  186. if (!maybe_node.has_value()) {
  187. dbgln("Failed to find node with internal_id={}", internal_id);
  188. VERIFY_NOT_REACHED();
  189. }
  190. return maybe_node.value();
  191. }
  192. }