LayoutTreeModel.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /*
  2. * Copyright (c) 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 "LayoutTreeModel.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 <LibWeb/Layout/LayoutText.h>
  32. #include <ctype.h>
  33. #include <stdio.h>
  34. namespace Web {
  35. LayoutTreeModel::LayoutTreeModel(Document& document)
  36. : m_document(document)
  37. {
  38. m_document_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png"));
  39. m_element_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png"));
  40. m_text_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-unknown.png"));
  41. }
  42. LayoutTreeModel::~LayoutTreeModel()
  43. {
  44. }
  45. GUI::ModelIndex LayoutTreeModel::index(int row, int column, const GUI::ModelIndex& parent) const
  46. {
  47. if (!parent.is_valid())
  48. return create_index(row, column, m_document->layout_node());
  49. auto& parent_node = *static_cast<LayoutNode*>(parent.internal_data());
  50. return create_index(row, column, parent_node.child_at_index(row));
  51. }
  52. GUI::ModelIndex LayoutTreeModel::parent_index(const GUI::ModelIndex& index) const
  53. {
  54. if (!index.is_valid())
  55. return {};
  56. auto& node = *static_cast<LayoutNode*>(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->layout_node());
  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 LayoutTreeModel::row_count(const GUI::ModelIndex& index) const
  75. {
  76. if (!index.is_valid())
  77. return 1;
  78. auto& node = *static_cast<LayoutNode*>(index.internal_data());
  79. return node.child_count();
  80. }
  81. int LayoutTreeModel::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 LayoutTreeModel::data(const GUI::ModelIndex& index, Role role) const
  106. {
  107. auto& node = *static_cast<LayoutNode*>(index.internal_data());
  108. if (role == Role::Icon) {
  109. if (node.is_root())
  110. return m_document_icon;
  111. if (node.is_text())
  112. return m_text_icon;
  113. return m_element_icon;
  114. }
  115. if (role == Role::Display) {
  116. if (node.is_text())
  117. return String::format("LayoutText: %s", with_whitespace_collapsed(downcast<LayoutText>(node).text_for_rendering()).characters());
  118. StringBuilder builder;
  119. builder.append(node.class_name());
  120. builder.append(' ');
  121. if (node.is_anonymous()) {
  122. builder.append("[anonymous]");
  123. } else if (!node.node()->is_element()) {
  124. builder.append(node.node()->node_name());
  125. } else {
  126. auto& element = downcast<Element>(*node.node());
  127. builder.append('<');
  128. builder.append(element.local_name());
  129. element.for_each_attribute([&](auto& name, auto& value) {
  130. builder.append(' ');
  131. builder.append(name);
  132. builder.append('=');
  133. builder.append('"');
  134. builder.append(value);
  135. builder.append('"');
  136. });
  137. builder.append('>');
  138. }
  139. return builder.to_string();
  140. }
  141. return {};
  142. }
  143. void LayoutTreeModel::update()
  144. {
  145. did_update();
  146. }
  147. }