LayoutTreeModel.cpp 5.8 KB

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