LayoutTreeModel.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "LayoutTreeModel.h"
  7. #include <AK/StringBuilder.h>
  8. #include <LibWeb/DOM/Document.h>
  9. #include <LibWeb/DOM/Element.h>
  10. #include <LibWeb/DOM/Text.h>
  11. #include <LibWeb/Layout/InitialContainingBlockBox.h>
  12. #include <LibWeb/Layout/TextNode.h>
  13. #include <ctype.h>
  14. #include <stdio.h>
  15. namespace Web {
  16. LayoutTreeModel::LayoutTreeModel(DOM::Document& document)
  17. : m_document(document)
  18. {
  19. m_document_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-html.png"));
  20. m_element_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/inspector-object.png"));
  21. m_text_icon.set_bitmap_for_size(16, Gfx::Bitmap::load_from_file("/res/icons/16x16/filetype-unknown.png"));
  22. }
  23. LayoutTreeModel::~LayoutTreeModel()
  24. {
  25. }
  26. GUI::ModelIndex LayoutTreeModel::index(int row, int column, const GUI::ModelIndex& parent) const
  27. {
  28. if (!parent.is_valid())
  29. return create_index(row, column, m_document->layout_node());
  30. auto& parent_node = *static_cast<Layout::Node*>(parent.internal_data());
  31. return create_index(row, column, parent_node.child_at_index(row));
  32. }
  33. GUI::ModelIndex LayoutTreeModel::parent_index(const GUI::ModelIndex& index) const
  34. {
  35. if (!index.is_valid())
  36. return {};
  37. auto& node = *static_cast<Layout::Node*>(index.internal_data());
  38. if (!node.parent())
  39. return {};
  40. // No grandparent? Parent is the document!
  41. if (!node.parent()->parent()) {
  42. return create_index(0, 0, m_document->layout_node());
  43. }
  44. // Walk the grandparent's children to find the index of node's parent in its parent.
  45. // (This is needed to produce the row number of the GUI::ModelIndex corresponding to node's parent.)
  46. int grandparent_child_index = 0;
  47. for (auto* grandparent_child = node.parent()->parent()->first_child(); grandparent_child; grandparent_child = grandparent_child->next_sibling()) {
  48. if (grandparent_child == node.parent())
  49. return create_index(grandparent_child_index, 0, node.parent());
  50. ++grandparent_child_index;
  51. }
  52. VERIFY_NOT_REACHED();
  53. return {};
  54. }
  55. int LayoutTreeModel::row_count(const GUI::ModelIndex& index) const
  56. {
  57. if (!index.is_valid())
  58. return 1;
  59. auto& node = *static_cast<Layout::Node*>(index.internal_data());
  60. return node.child_count();
  61. }
  62. int LayoutTreeModel::column_count(const GUI::ModelIndex&) const
  63. {
  64. return 1;
  65. }
  66. static String with_whitespace_collapsed(const StringView& string)
  67. {
  68. StringBuilder builder;
  69. for (size_t i = 0; i < string.length(); ++i) {
  70. if (isspace(string[i])) {
  71. builder.append(' ');
  72. while (i < string.length()) {
  73. if (isspace(string[i])) {
  74. ++i;
  75. continue;
  76. }
  77. builder.append(string[i]);
  78. break;
  79. }
  80. continue;
  81. }
  82. builder.append(string[i]);
  83. }
  84. return builder.to_string();
  85. }
  86. GUI::Variant LayoutTreeModel::data(const GUI::ModelIndex& index, GUI::ModelRole role) const
  87. {
  88. auto& node = *static_cast<Layout::Node*>(index.internal_data());
  89. if (role == GUI::ModelRole::Icon) {
  90. if (is<Layout::InitialContainingBlockBox>(node))
  91. return m_document_icon;
  92. if (is<Layout::TextNode>(node))
  93. return m_text_icon;
  94. return m_element_icon;
  95. }
  96. if (role == GUI::ModelRole::Display) {
  97. if (is<Layout::TextNode>(node))
  98. return String::formatted("TextNode: {}", with_whitespace_collapsed(verify_cast<Layout::TextNode>(node).text_for_rendering()));
  99. StringBuilder builder;
  100. builder.append(node.class_name());
  101. builder.append(' ');
  102. if (node.is_anonymous()) {
  103. builder.append("[anonymous]");
  104. } else if (!node.dom_node()->is_element()) {
  105. builder.append(node.dom_node()->node_name());
  106. } else {
  107. auto& element = verify_cast<DOM::Element>(*node.dom_node());
  108. builder.append('<');
  109. builder.append(element.local_name());
  110. element.for_each_attribute([&](auto& name, auto& value) {
  111. builder.append(' ');
  112. builder.append(name);
  113. builder.append('=');
  114. builder.append('"');
  115. builder.append(value);
  116. builder.append('"');
  117. });
  118. builder.append('>');
  119. }
  120. return builder.to_string();
  121. }
  122. return {};
  123. }
  124. void LayoutTreeModel::update()
  125. {
  126. did_update();
  127. }
  128. }