DOMTreeModel.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <LibWeb/DOM/Document.h>
  29. #include <LibWeb/DOM/Element.h>
  30. #include <LibWeb/DOM/Text.h>
  31. #include <ctype.h>
  32. #include <stdio.h>
  33. namespace Web {
  34. DOMTreeModel::DOMTreeModel(Document& document)
  35. : m_document(document)
  36. {
  37. m_document_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png"));
  38. m_element_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png"));
  39. m_text_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-unknown.png"));
  40. }
  41. DOMTreeModel::~DOMTreeModel()
  42. {
  43. }
  44. GUI::ModelIndex DOMTreeModel::index(int row, int column, const GUI::ModelIndex& parent) const
  45. {
  46. if (!parent.is_valid()) {
  47. return create_index(row, column, m_document.ptr());
  48. }
  49. auto& parent_node = *static_cast<Node*>(parent.internal_data());
  50. return create_index(row, column, parent_node.child_at_index(row));
  51. }
  52. GUI::ModelIndex DOMTreeModel::parent_index(const GUI::ModelIndex& index) const
  53. {
  54. if (!index.is_valid())
  55. return {};
  56. auto& node = *static_cast<Node*>(index.internal_data());
  57. if (!node.parent())
  58. return {};
  59. // No grandparent? Parent is the document!
  60. if (!node.parent()->parent()) {
  61. return create_index(0, 0, m_document.ptr());
  62. }
  63. // Walk the grandparent's children to find the index of node's parent in its parent.
  64. // (This is needed to produce the row number of the GUI::ModelIndex corresponding to node's parent.)
  65. int grandparent_child_index = 0;
  66. for (auto* grandparent_child = node.parent()->parent()->first_child(); grandparent_child; grandparent_child = grandparent_child->next_sibling()) {
  67. if (grandparent_child == node.parent())
  68. return create_index(grandparent_child_index, 0, node.parent());
  69. ++grandparent_child_index;
  70. }
  71. ASSERT_NOT_REACHED();
  72. return {};
  73. }
  74. int DOMTreeModel::row_count(const GUI::ModelIndex& index) const
  75. {
  76. if (!index.is_valid())
  77. return 1;
  78. auto& node = *static_cast<Node*>(index.internal_data());
  79. return node.child_count();
  80. }
  81. int DOMTreeModel::column_count(const GUI::ModelIndex&) const
  82. {
  83. return 1;
  84. }
  85. static String with_whitespace_collapsed(const StringView& string)
  86. {
  87. StringBuilder builder;
  88. for (size_t i = 0; i < string.length(); ++i) {
  89. if (isspace(string[i])) {
  90. builder.append(' ');
  91. while (i < string.length()) {
  92. if (isspace(string[i])) {
  93. ++i;
  94. continue;
  95. }
  96. builder.append(string[i]);
  97. break;
  98. }
  99. continue;
  100. }
  101. builder.append(string[i]);
  102. }
  103. return builder.to_string();
  104. }
  105. GUI::Variant DOMTreeModel::data(const GUI::ModelIndex& index, Role role) const
  106. {
  107. auto& node = *static_cast<Node*>(index.internal_data());
  108. if (role == Role::Icon) {
  109. if (node.is_document())
  110. return m_document_icon;
  111. if (node.is_element())
  112. return m_element_icon;
  113. // FIXME: More node type icons?
  114. return m_text_icon;
  115. }
  116. if (role == Role::Display) {
  117. if (node.is_text())
  118. return String::format("%s", with_whitespace_collapsed(to<Text>(node).data()).characters());
  119. if (!node.is_element())
  120. return node.tag_name();
  121. auto& element = to<Element>(node);
  122. StringBuilder builder;
  123. builder.append('<');
  124. builder.append(element.tag_name());
  125. element.for_each_attribute([&](auto& name, auto& value) {
  126. builder.append(' ');
  127. builder.append(name);
  128. builder.append('=');
  129. builder.append('"');
  130. builder.append(value);
  131. builder.append('"');
  132. });
  133. builder.append('>');
  134. return builder.to_string();
  135. }
  136. return {};
  137. }
  138. void DOMTreeModel::update()
  139. {
  140. did_update();
  141. }
  142. }