Dump.cpp 7.4 KB

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