Dump.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #include <AK/Utf8View.h>
  2. #include <LibHTML/CSS/StyleSheet.h>
  3. #include <LibHTML/DOM/Document.h>
  4. #include <LibHTML/DOM/DocumentType.h>
  5. #include <LibHTML/DOM/Element.h>
  6. #include <LibHTML/DOM/Text.h>
  7. #include <LibHTML/Dump.h>
  8. #include <LibHTML/Layout/LayoutBlock.h>
  9. #include <LibHTML/Layout/LayoutNode.h>
  10. #include <LibHTML/Layout/LayoutText.h>
  11. #include <stdio.h>
  12. void dump_tree(const Node& node)
  13. {
  14. static int indent = 0;
  15. for (int i = 0; i < indent; ++i)
  16. dbgprintf(" ");
  17. if (is<Document>(node)) {
  18. dbgprintf("*Document*\n");
  19. } else if (is<Element>(node)) {
  20. dbgprintf("<%s", to<Element>(node).tag_name().characters());
  21. to<Element>(node).for_each_attribute([](auto& name, auto& value) {
  22. dbgprintf(" %s=%s", name.characters(), value.characters());
  23. });
  24. dbgprintf(">\n");
  25. } else if (is<Text>(node)) {
  26. dbgprintf("\"%s\"\n", static_cast<const Text&>(node).data().characters());
  27. } else if (is<DocumentType>(node)) {
  28. dbgprintf("<!DOCTYPE>\n");
  29. }
  30. ++indent;
  31. if (is<ParentNode>(node)) {
  32. static_cast<const ParentNode&>(node).for_each_child([](auto& child) {
  33. dump_tree(child);
  34. });
  35. }
  36. --indent;
  37. }
  38. void dump_tree(const LayoutNode& layout_node)
  39. {
  40. static int indent = 0;
  41. for (int i = 0; i < indent; ++i)
  42. dbgprintf(" ");
  43. String tag_name;
  44. if (layout_node.is_anonymous())
  45. tag_name = "(anonymous)";
  46. else if (is<Text>(layout_node.node()))
  47. tag_name = "#text";
  48. else if (is<Document>(layout_node.node()))
  49. tag_name = "#document";
  50. else if (is<Element>(layout_node.node()))
  51. tag_name = to<Element>(*layout_node.node()).tag_name();
  52. else
  53. tag_name = "???";
  54. dbgprintf("%s {%s} at (%d,%d) size %dx%d",
  55. layout_node.class_name(),
  56. tag_name.characters(),
  57. layout_node.rect().x(),
  58. layout_node.rect().y(),
  59. layout_node.rect().width(),
  60. layout_node.rect().height());
  61. // Dump the horizontal box properties
  62. dbgprintf(" [%d+%d+%d %d %d+%d+%d]",
  63. layout_node.box_model().margin().left.to_px(),
  64. layout_node.box_model().border().left.to_px(),
  65. layout_node.box_model().padding().left.to_px(),
  66. layout_node.rect().width(),
  67. layout_node.box_model().padding().right.to_px(),
  68. layout_node.box_model().border().right.to_px(),
  69. layout_node.box_model().margin().right.to_px());
  70. // And the vertical box properties
  71. dbgprintf(" [%d+%d+%d %d %d+%d+%d]",
  72. layout_node.box_model().margin().top.to_px(),
  73. layout_node.box_model().border().top.to_px(),
  74. layout_node.box_model().padding().top.to_px(),
  75. layout_node.rect().height(),
  76. layout_node.box_model().padding().bottom.to_px(),
  77. layout_node.box_model().border().bottom.to_px(),
  78. layout_node.box_model().margin().bottom.to_px());
  79. dbgprintf("\n");
  80. if (layout_node.is_block() && static_cast<const LayoutBlock&>(layout_node).children_are_inline()) {
  81. auto& block = static_cast<const LayoutBlock&>(layout_node);
  82. for (int i = 0; i < indent; ++i)
  83. dbgprintf(" ");
  84. dbgprintf(" Line boxes (%d):\n", block.line_boxes().size());
  85. for (int line_box_index = 0; line_box_index < block.line_boxes().size(); ++line_box_index) {
  86. auto& line_box = block.line_boxes()[line_box_index];
  87. for (int i = 0; i < indent; ++i)
  88. dbgprintf(" ");
  89. dbgprintf(" [%d] width: %d\n", line_box_index, line_box.width());
  90. for (int fragment_index = 0; fragment_index < line_box.fragments().size(); ++fragment_index) {
  91. auto& fragment = line_box.fragments()[fragment_index];
  92. for (int i = 0; i < indent; ++i)
  93. dbgprintf(" ");
  94. dbgprintf(" [%d] layout_node: %s{%p}, start: %d, length: %d, rect: %s\n",
  95. fragment_index,
  96. fragment.layout_node().class_name(),
  97. &fragment.layout_node(),
  98. fragment.start(),
  99. fragment.length(),
  100. fragment.rect().to_string().characters());
  101. if (fragment.layout_node().is_text()) {
  102. for (int i = 0; i < indent; ++i)
  103. dbgprintf(" ");
  104. auto& layout_text = static_cast<const LayoutText&>(fragment.layout_node());
  105. dbgprintf(" text: \"%s\"\n",
  106. String(Utf8View(layout_text.node().data()).substring_view(fragment.start(), fragment.length()).as_string()).characters());
  107. }
  108. }
  109. }
  110. }
  111. layout_node.style().for_each_property([&](auto property_id, auto& value) {
  112. for (int i = 0; i < indent; ++i)
  113. dbgprintf(" ");
  114. dbgprintf(" (CSS::PropertyID(%u): %s)\n", (unsigned)property_id, value.to_string().characters());
  115. });
  116. ++indent;
  117. layout_node.for_each_child([](auto& child) {
  118. dump_tree(child);
  119. });
  120. --indent;
  121. }
  122. void dump_rule(const StyleRule& rule)
  123. {
  124. dbgprintf("Rule:\n");
  125. for (auto& selector : rule.selectors()) {
  126. dbgprintf(" Selector:\n");
  127. for (auto& component : selector.components()) {
  128. const char* type_description = "Unknown";
  129. switch (component.type) {
  130. case Selector::Component::Type::Invalid:
  131. type_description = "Invalid";
  132. break;
  133. case Selector::Component::Type::Id:
  134. type_description = "Id";
  135. break;
  136. case Selector::Component::Type::Class:
  137. type_description = "Class";
  138. break;
  139. case Selector::Component::Type::TagName:
  140. type_description = "TagName";
  141. break;
  142. }
  143. const char* relation_description = "";
  144. switch (component.relation) {
  145. case Selector::Component::Relation::None:
  146. break;
  147. case Selector::Component::Relation::ImmediateChild:
  148. relation_description = "{ImmediateChild}";
  149. break;
  150. case Selector::Component::Relation::Descendant:
  151. relation_description = "{Descendant}";
  152. break;
  153. case Selector::Component::Relation::AdjacentSibling:
  154. relation_description = "{AdjacentSibling}";
  155. break;
  156. case Selector::Component::Relation::GeneralSibling:
  157. relation_description = "{GeneralSibling}";
  158. break;
  159. }
  160. dbgprintf(" %s:%s %s\n", type_description, component.value.characters(), relation_description);
  161. }
  162. }
  163. dbgprintf(" Declarations:\n");
  164. for (auto& property : rule.declaration().properties()) {
  165. dbgprintf(" CSS::PropertyID(%u): '%s'\n", (unsigned)property.property_id, property.value->to_string().characters());
  166. }
  167. }
  168. void dump_sheet(const StyleSheet& sheet)
  169. {
  170. dbgprintf("StyleSheet{%p}: %d rule(s)\n", &sheet, sheet.rules().size());
  171. for (auto& rule : sheet.rules()) {
  172. dump_rule(rule);
  173. }
  174. }