DOMTreeModel.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "DOMTreeModel.h"
  7. #include <AK/StringBuilder.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/DOM/Element.h>
  10. #include <LibWeb/DOM/Text.h>
  11. #include <ctype.h>
  12. #include <stdio.h>
  13. namespace Web {
  14. DOMTreeModel::DOMTreeModel(DOM::Document& document)
  15. : m_document(document)
  16. {
  17. m_document_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-html.png"));
  18. m_element_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/inspector-object.png"));
  19. m_text_icon.set_bitmap_for_size(16, Gfx::Bitmap::try_load_from_file("/res/icons/16x16/filetype-unknown.png"));
  20. }
  21. DOMTreeModel::~DOMTreeModel()
  22. {
  23. }
  24. GUI::ModelIndex DOMTreeModel::index(int row, int column, const GUI::ModelIndex& parent) const
  25. {
  26. if (!parent.is_valid()) {
  27. return create_index(row, column, m_document.ptr());
  28. }
  29. auto& parent_node = *static_cast<DOM::Node*>(parent.internal_data());
  30. return create_index(row, column, parent_node.child_at_index(row));
  31. }
  32. GUI::ModelIndex DOMTreeModel::parent_index(const GUI::ModelIndex& index) const
  33. {
  34. if (!index.is_valid())
  35. return {};
  36. auto& node = *static_cast<DOM::Node*>(index.internal_data());
  37. if (!node.parent())
  38. return {};
  39. // FIXME: Handle the template element (child elements are not stored in it, all of its children are in its document fragment "content")
  40. // No grandparent? Parent is the document!
  41. if (!node.parent()->parent()) {
  42. return create_index(0, 0, m_document.ptr());
  43. }
  44. // Walk the grandparent's children to find the index of node's parent in its parent.
  45. // (This is needed to produce the row number of the GUI::ModelIndex corresponding to node's parent.)
  46. int grandparent_child_index = 0;
  47. for (auto* grandparent_child = node.parent()->parent()->first_child(); grandparent_child; grandparent_child = grandparent_child->next_sibling()) {
  48. if (grandparent_child == node.parent())
  49. return create_index(grandparent_child_index, 0, node.parent());
  50. ++grandparent_child_index;
  51. }
  52. VERIFY_NOT_REACHED();
  53. return {};
  54. }
  55. int DOMTreeModel::row_count(const GUI::ModelIndex& index) const
  56. {
  57. if (!index.is_valid())
  58. return 1;
  59. auto& node = *static_cast<DOM::Node*>(index.internal_data());
  60. return node.child_count();
  61. }
  62. int DOMTreeModel::column_count(const GUI::ModelIndex&) const
  63. {
  64. return 1;
  65. }
  66. static String with_whitespace_collapsed(const StringView& string)
  67. {
  68. StringBuilder builder;
  69. for (size_t i = 0; i < string.length(); ++i) {
  70. if (isspace(string[i])) {
  71. builder.append(' ');
  72. while (i < string.length()) {
  73. if (isspace(string[i])) {
  74. ++i;
  75. continue;
  76. }
  77. builder.append(string[i]);
  78. break;
  79. }
  80. continue;
  81. }
  82. builder.append(string[i]);
  83. }
  84. return builder.to_string();
  85. }
  86. GUI::Variant DOMTreeModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  87. {
  88. auto& node = *static_cast<DOM::Node*>(index.internal_data());
  89. if (role == GUI::ModelRole::Icon) {
  90. if (node.is_document())
  91. return m_document_icon;
  92. if (node.is_element())
  93. return m_element_icon;
  94. // FIXME: More node type icons?
  95. return m_text_icon;
  96. }
  97. if (role == GUI::ModelRole::Display) {
  98. if (node.is_text())
  99. return with_whitespace_collapsed(verify_cast<DOM::Text>(node).data());
  100. if (!node.is_element())
  101. return node.node_name();
  102. auto& element = verify_cast<DOM::Element>(node);
  103. StringBuilder builder;
  104. builder.append('<');
  105. builder.append(element.local_name());
  106. element.for_each_attribute([&](auto& name, auto& value) {
  107. builder.append(' ');
  108. builder.append(name);
  109. builder.append('=');
  110. builder.append('"');
  111. builder.append(value);
  112. builder.append('"');
  113. });
  114. builder.append('>');
  115. return builder.to_string();
  116. }
  117. return {};
  118. }
  119. }