LayoutTreeModel.cpp 4.5 KB

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