DOMTreeJSONModel.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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(move(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, &m_dom_tree);
  26. }
  27. auto const& parent_node = *static_cast<JsonObject const*>(parent.internal_data());
  28. auto const* children = get_children(parent_node);
  29. if (!children)
  30. return create_index(row, column, &m_dom_tree);
  31. auto const& child_node = children->at(row).as_object();
  32. return create_index(row, column, &child_node);
  33. }
  34. GUI::ModelIndex DOMTreeJSONModel::parent_index(const GUI::ModelIndex& index) const
  35. {
  36. // FIXME: Handle the template element (child elements are not stored in it, all of its children are in its document fragment "content")
  37. // Probably in the JSON generation in Node.cpp?
  38. if (!index.is_valid())
  39. return {};
  40. auto const& node = *static_cast<JsonObject const*>(index.internal_data());
  41. auto node_internal_id = get_internal_id(node);
  42. auto const* parent_node = find_parent_of_child_with_internal_id(node_internal_id);
  43. if (!parent_node)
  44. return {};
  45. // If the parent is the root document, we know it has index 0, 0
  46. auto parent_node_internal_id = get_internal_id(*parent_node);
  47. if (parent_node_internal_id == get_internal_id(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 = find_parent_of_child_with_internal_id(parent_node_internal_id);
  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 (get_internal_id(child) == parent_node_internal_id)
  59. return create_index(grandparent_child_index, 0, parent_node);
  60. }
  61. return {};
  62. }
  63. int DOMTreeJSONModel::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 DOMTreeJSONModel::column_count(const GUI::ModelIndex&) const
  72. {
  73. return 1;
  74. }
  75. static String with_whitespace_collapsed(const 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 DOMTreeJSONModel::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::Icon) {
  101. if (type == "document")
  102. return m_document_icon;
  103. if (type == "element")
  104. return m_element_icon;
  105. // FIXME: More node type icons?
  106. return m_text_icon;
  107. }
  108. if (role == GUI::ModelRole::Display) {
  109. if (type == "text")
  110. return with_whitespace_collapsed(node.get("text").as_string());
  111. if (type != "element")
  112. return node_name;
  113. StringBuilder builder;
  114. builder.append('<');
  115. builder.append(node_name.to_lowercase());
  116. if (node.has("attributes")) {
  117. auto attributes = node.get("attributes").as_object();
  118. attributes.for_each_member([&builder](auto& name, JsonValue const& value) {
  119. builder.append(' ');
  120. builder.append(name);
  121. builder.append('=');
  122. builder.append('"');
  123. builder.append(value.to_string());
  124. builder.append('"');
  125. });
  126. }
  127. builder.append('>');
  128. return builder.to_string();
  129. }
  130. return {};
  131. }
  132. void DOMTreeJSONModel::update()
  133. {
  134. did_update();
  135. }
  136. JsonObject const* DOMTreeJSONModel::find_parent_of_child_with_internal_id(JsonObject const& node, size_t internal_id) const
  137. {
  138. auto const* children = get_children(node);
  139. if (!children)
  140. return nullptr;
  141. for (size_t i = 0; i < children->size(); ++i) {
  142. auto const& child = children->at(i).as_object();
  143. auto child_internal_id = get_internal_id(child);
  144. if (child_internal_id == internal_id)
  145. return &node;
  146. if (auto const* maybe_node = find_parent_of_child_with_internal_id(child, internal_id); maybe_node)
  147. return maybe_node;
  148. }
  149. return nullptr;
  150. }
  151. }