Dump.cpp 7.4 KB

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