TreeModel.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2018-2020, Adam Hodgen <ant1441@gmail.com>
  4. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include <AK/JsonObject.h>
  9. #include <AK/StringBuilder.h>
  10. #include <LibWeb/Infra/Strings.h>
  11. #include <LibWebView/TreeModel.h>
  12. namespace WebView {
  13. TreeModel::TreeModel(Type type, JsonValue tree)
  14. : m_type(type)
  15. , m_tree(move(tree))
  16. {
  17. prepare_node_maps(&m_tree.as_object(), nullptr);
  18. }
  19. TreeModel::~TreeModel() = default;
  20. void TreeModel::prepare_node_maps(JsonObject const* node, JsonObject const* parent_node)
  21. {
  22. m_node_to_parent_map.set(node, parent_node);
  23. if (auto id = node->get_integer<i32>("id"sv); id.has_value()) {
  24. m_node_id_to_node_map.set(*id, node);
  25. }
  26. if (auto children = node->get_array("children"sv); children.has_value()) {
  27. children->for_each([&](auto const& child) {
  28. prepare_node_maps(&child.as_object(), node);
  29. });
  30. }
  31. }
  32. int TreeModel::row_count(ModelIndex const& parent) const
  33. {
  34. if (!parent.is_valid())
  35. return 1;
  36. auto const& node = *static_cast<JsonObject const*>(parent.internal_data);
  37. auto children = node.get_array("children"sv);
  38. return children.has_value() ? static_cast<int>(children->size()) : 0;
  39. }
  40. int TreeModel::column_count(ModelIndex const&) const
  41. {
  42. return 1;
  43. }
  44. ModelIndex TreeModel::index(int row, int column, ModelIndex const& parent) const
  45. {
  46. if (!parent.is_valid())
  47. return { row, column, &m_tree.as_object() };
  48. auto const& parent_node = *static_cast<JsonObject const*>(parent.internal_data);
  49. auto children = parent_node.get_array("children"sv);
  50. if (!children.has_value())
  51. return { row, column, &m_tree.as_object() };
  52. auto const& child_node = children->at(row).as_object();
  53. return { row, column, &child_node };
  54. }
  55. ModelIndex TreeModel::parent(ModelIndex const& index) const
  56. {
  57. // FIXME: Handle the template element (child elements are not stored in it, all of its children are in its document fragment "content")
  58. // Probably in the JSON generation in Node.cpp?
  59. if (!index.is_valid())
  60. return {};
  61. auto const& node = *static_cast<JsonObject const*>(index.internal_data);
  62. auto const* parent_node = get_parent(node);
  63. if (!parent_node)
  64. return {};
  65. // If the parent is the root document, we know it has index 0, 0
  66. if (parent_node == &m_tree.as_object())
  67. return { 0, 0, parent_node };
  68. // Otherwise, we need to find the grandparent, to find the index of parent within that
  69. auto const* grandparent_node = get_parent(*parent_node);
  70. VERIFY(grandparent_node);
  71. auto grandparent_children = grandparent_node->get_array("children"sv);
  72. if (!grandparent_children.has_value())
  73. return {};
  74. for (size_t grandparent_child_index = 0; grandparent_child_index < grandparent_children->size(); ++grandparent_child_index) {
  75. auto const& child = grandparent_children->at(grandparent_child_index).as_object();
  76. if (&child == parent_node)
  77. return { static_cast<int>(grandparent_child_index), 0, parent_node };
  78. }
  79. return {};
  80. }
  81. static String accessibility_tree_text_for_display(JsonObject const& node, StringView type)
  82. {
  83. auto role = node.get_deprecated_string("role"sv).value_or({});
  84. if (type == "text")
  85. return MUST(Web::Infra::strip_and_collapse_whitespace(node.get_deprecated_string("text"sv).value()));
  86. if (type != "element")
  87. return MUST(String::from_deprecated_string(role));
  88. auto name = node.get_deprecated_string("name"sv).value_or({});
  89. auto description = node.get_deprecated_string("description"sv).value_or({});
  90. StringBuilder builder;
  91. builder.append(role.to_lowercase());
  92. builder.appendff(" name: \"{}\", description: \"{}\"", name, description);
  93. return MUST(builder.to_string());
  94. }
  95. static String dom_tree_text_for_display(JsonObject const& node, StringView type)
  96. {
  97. auto name = node.get_deprecated_string("name"sv).value_or({});
  98. if (type == "text"sv)
  99. return MUST(Web::Infra::strip_and_collapse_whitespace(node.get_deprecated_string("text"sv).value()));
  100. if (type == "comment"sv)
  101. return MUST(String::formatted("<!--{}-->", node.get_deprecated_string("data"sv).value()));
  102. if (type == "shadow-root"sv)
  103. return MUST(String::formatted("{} ({})", name, node.get_deprecated_string("mode"sv).value()));
  104. if (type != "element"sv)
  105. return MUST(String::from_deprecated_string(name));
  106. StringBuilder builder;
  107. builder.append('<');
  108. builder.append(name.to_lowercase());
  109. if (node.has("attributes"sv)) {
  110. auto attributes = node.get_object("attributes"sv).value();
  111. attributes.for_each_member([&builder](auto const& name, JsonValue const& value) {
  112. builder.append(' ');
  113. builder.append(name);
  114. builder.append('=');
  115. builder.append('"');
  116. builder.append(value.to_deprecated_string());
  117. builder.append('"');
  118. });
  119. }
  120. builder.append('>');
  121. return MUST(builder.to_string());
  122. }
  123. String TreeModel::text_for_display(ModelIndex const& index) const
  124. {
  125. auto const& node = *static_cast<JsonObject const*>(index.internal_data);
  126. auto type = node.get_deprecated_string("type"sv).value_or("unknown"sv);
  127. switch (m_type) {
  128. case Type::AccessibilityTree:
  129. return accessibility_tree_text_for_display(node, type);
  130. case Type::DOMTree:
  131. return dom_tree_text_for_display(node, type);
  132. }
  133. VERIFY_NOT_REACHED();
  134. }
  135. ModelIndex TreeModel::index_for_node(i32 node_id, Optional<Web::CSS::Selector::PseudoElement> const& pseudo_element) const
  136. {
  137. if (auto const* node = m_node_id_to_node_map.get(node_id).value_or(nullptr)) {
  138. if (pseudo_element.has_value()) {
  139. // Find pseudo-element child of the node.
  140. auto node_children = node->get_array("children"sv);
  141. for (size_t i = 0; i < node_children->size(); i++) {
  142. auto const& child = node_children->at(i).as_object();
  143. if (!child.has("pseudo-element"sv))
  144. continue;
  145. auto child_pseudo_element = child.get_i32("pseudo-element"sv);
  146. if (child_pseudo_element == to_underlying(pseudo_element.value()))
  147. return { static_cast<int>(i), 0, &child };
  148. }
  149. } else {
  150. auto const* parent = get_parent(*node);
  151. if (!parent)
  152. return {};
  153. auto parent_children = parent->get_array("children"sv);
  154. for (size_t i = 0; i < parent_children->size(); i++) {
  155. if (&parent_children->at(i).as_object() == node)
  156. return { static_cast<int>(i), 0, node };
  157. }
  158. }
  159. }
  160. 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);
  161. return {};
  162. }
  163. }