DOMTreeModel.cpp 3.8 KB

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