DOMTreeModel.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include "DOMTreeModel.h"
  27. #include <AK/StringBuilder.h>
  28. #include <LibHTML/DOM/Document.h>
  29. #include <LibHTML/DOM/Element.h>
  30. #include <LibHTML/DOM/Text.h>
  31. #include <ctype.h>
  32. #include <stdio.h>
  33. DOMTreeModel::DOMTreeModel(Document& document)
  34. : m_document(document)
  35. {
  36. m_document_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png"));
  37. m_element_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png"));
  38. m_text_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-unknown.png"));
  39. }
  40. DOMTreeModel::~DOMTreeModel()
  41. {
  42. }
  43. GUI::ModelIndex DOMTreeModel::index(int row, int column, const GUI::ModelIndex& parent) const
  44. {
  45. if (!parent.is_valid()) {
  46. return create_index(row, column, m_document.ptr());
  47. }
  48. auto& parent_node = *static_cast<Node*>(parent.internal_data());
  49. return create_index(row, column, parent_node.child_at_index(row));
  50. }
  51. GUI::ModelIndex DOMTreeModel::parent_index(const GUI::ModelIndex& index) const
  52. {
  53. if (!index.is_valid())
  54. return {};
  55. auto& node = *static_cast<Node*>(index.internal_data());
  56. if (!node.parent())
  57. return {};
  58. // No grandparent? Parent is the document!
  59. if (!node.parent()->parent()) {
  60. return create_index(0, 0, m_document.ptr());
  61. }
  62. // Walk the grandparent's children to find the index of node's parent in its parent.
  63. // (This is needed to produce the row number of the GUI::ModelIndex corresponding to node's parent.)
  64. int grandparent_child_index = 0;
  65. for (auto* grandparent_child = node.parent()->parent()->first_child(); grandparent_child; grandparent_child = grandparent_child->next_sibling()) {
  66. if (grandparent_child == node.parent())
  67. return create_index(grandparent_child_index, 0, node.parent());
  68. ++grandparent_child_index;
  69. }
  70. ASSERT_NOT_REACHED();
  71. return {};
  72. }
  73. int DOMTreeModel::row_count(const GUI::ModelIndex& index) const
  74. {
  75. if (!index.is_valid())
  76. return 1;
  77. auto& node = *static_cast<Node*>(index.internal_data());
  78. return node.child_count();
  79. }
  80. int DOMTreeModel::column_count(const GUI::ModelIndex&) const
  81. {
  82. return 1;
  83. }
  84. static String with_whitespace_collapsed(const StringView& string)
  85. {
  86. StringBuilder builder;
  87. for (size_t i = 0; i < string.length(); ++i) {
  88. if (isspace(string[i])) {
  89. builder.append(' ');
  90. while (i < string.length()) {
  91. if (isspace(string[i])) {
  92. ++i;
  93. continue;
  94. }
  95. builder.append(string[i]);
  96. break;
  97. }
  98. continue;
  99. }
  100. builder.append(string[i]);
  101. }
  102. return builder.to_string();
  103. }
  104. GUI::Variant DOMTreeModel::data(const GUI::ModelIndex& index, Role role) const
  105. {
  106. auto& node = *static_cast<Node*>(index.internal_data());
  107. if (role == Role::Icon) {
  108. if (node.is_document())
  109. return m_document_icon;
  110. if (node.is_element())
  111. return m_element_icon;
  112. // FIXME: More node type icons?
  113. return m_text_icon;
  114. }
  115. if (role == Role::Display) {
  116. if (node.is_text())
  117. return String::format("%s", with_whitespace_collapsed(to<Text>(node).data()).characters());
  118. if (!node.is_element())
  119. return node.tag_name();
  120. auto& element = to<Element>(node);
  121. StringBuilder builder;
  122. builder.append('<');
  123. builder.append(element.tag_name());
  124. element.for_each_attribute([&](auto& name, auto& value) {
  125. builder.append(' ');
  126. builder.append(name);
  127. builder.append('=');
  128. builder.append('"');
  129. builder.append(value);
  130. builder.append('"');
  131. });
  132. builder.append('>');
  133. return builder.to_string();
  134. }
  135. return {};
  136. }
  137. void DOMTreeModel::update()
  138. {
  139. did_update();
  140. }