DOMTreeJSONModel.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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::try_load_from_file("/res/icons/16x16/filetype-html.png"));
  16. m_element_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/inspector-object.png"));
  17. m_text_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-unknown.png"));
  18. map_dom_nodes_to_parent(nullptr, &m_dom_tree);
  19. }
  20. DOMTreeJSONModel::~DOMTreeJSONModel()
  21. {
  22. }
  23. GUI::ModelIndex DOMTreeJSONModel::index(int row, int column, const GUI::ModelIndex& parent) const
  24. {
  25. if (!parent.is_valid()) {
  26. return create_index(row, column, &m_dom_tree);
  27. }
  28. auto const& parent_node = *static_cast<JsonObject const*>(parent.internal_data());
  29. auto const* children = get_children(parent_node);
  30. if (!children)
  31. return create_index(row, column, &m_dom_tree);
  32. auto const& child_node = children->at(row).as_object();
  33. return create_index(row, column, &child_node);
  34. }
  35. GUI::ModelIndex DOMTreeJSONModel::parent_index(const GUI::ModelIndex& index) const
  36. {
  37. // FIXME: Handle the template element (child elements are not stored in it, all of its children are in its document fragment "content")
  38. // Probably in the JSON generation in Node.cpp?
  39. if (!index.is_valid())
  40. return {};
  41. auto const& node = *static_cast<JsonObject const*>(index.internal_data());
  42. auto const* parent_node = get_parent(node);
  43. if (!parent_node)
  44. return {};
  45. // If the parent is the root document, we know it has index 0, 0
  46. if (parent_node == &m_dom_tree) {
  47. return create_index(0, 0, parent_node);
  48. }
  49. // Otherwise, we need to find the grandparent, to find the index of parent within that
  50. auto const* grandparent_node = get_parent(*parent_node);
  51. VERIFY(grandparent_node);
  52. auto const* grandparent_children = get_children(*grandparent_node);
  53. if (!grandparent_children)
  54. return {};
  55. for (size_t grandparent_child_index = 0; grandparent_child_index < grandparent_children->size(); ++grandparent_child_index) {
  56. auto const& child = grandparent_children->at(grandparent_child_index).as_object();
  57. if (&child == parent_node)
  58. return create_index(grandparent_child_index, 0, parent_node);
  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 const& node = *static_cast<JsonObject const*>(index.internal_data());
  67. auto const* children = get_children(node);
  68. return children ? children->size() : 0;
  69. }
  70. int DOMTreeJSONModel::column_count(const GUI::ModelIndex&) const
  71. {
  72. return 1;
  73. }
  74. static String with_whitespace_collapsed(const StringView& string)
  75. {
  76. StringBuilder builder;
  77. for (size_t i = 0; i < string.length(); ++i) {
  78. if (isspace(string[i])) {
  79. builder.append(' ');
  80. while (i < string.length()) {
  81. if (isspace(string[i])) {
  82. ++i;
  83. continue;
  84. }
  85. builder.append(string[i]);
  86. break;
  87. }
  88. continue;
  89. }
  90. builder.append(string[i]);
  91. }
  92. return builder.to_string();
  93. }
  94. GUI::Variant DOMTreeJSONModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  95. {
  96. auto const& node = *static_cast<JsonObject const*>(index.internal_data());
  97. auto node_name = node.get("name").as_string();
  98. auto type = node.get("type").as_string_or("unknown");
  99. if (role == GUI::ModelRole::Icon) {
  100. if (type == "document")
  101. return m_document_icon;
  102. if (type == "element")
  103. return m_element_icon;
  104. // FIXME: More node type icons?
  105. return m_text_icon;
  106. }
  107. if (role == GUI::ModelRole::Display) {
  108. if (type == "text")
  109. return with_whitespace_collapsed(node.get("text").as_string());
  110. if (type != "element")
  111. return node_name;
  112. StringBuilder builder;
  113. builder.append('<');
  114. builder.append(node_name.to_lowercase());
  115. if (node.has("attributes")) {
  116. auto attributes = node.get("attributes").as_object();
  117. attributes.for_each_member([&builder](auto& name, JsonValue const& value) {
  118. builder.append(' ');
  119. builder.append(name);
  120. builder.append('=');
  121. builder.append('"');
  122. builder.append(value.to_string());
  123. builder.append('"');
  124. });
  125. }
  126. builder.append('>');
  127. return builder.to_string();
  128. }
  129. return {};
  130. }
  131. void DOMTreeJSONModel::map_dom_nodes_to_parent(JsonObject const* parent, JsonObject const* node)
  132. {
  133. m_dom_node_to_parent_map.set(node, parent);
  134. auto const* children = get_children(*node);
  135. if (!children)
  136. return;
  137. children->for_each([&](auto const& child) {
  138. auto const& child_node = child.as_object();
  139. map_dom_nodes_to_parent(node, &child_node);
  140. });
  141. }
  142. }