Dump.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 (%g,%g) size %gx%g",
  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(" [%g+%g+%g %g %g+%g+%g]",
  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(" [%g+%g+%g %g %g+%g+%g]",
  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: %g\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. auto fragment_text = layout_text.text_for_rendering().substring(fragment.start(), fragment.length());
  115. dbgprintf(" text: \"%s\"\n", fragment_text.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& complex_selector : selector.complex_selectors()) {
  137. dbgprintf(" ");
  138. const char* relation_description = "";
  139. switch (complex_selector.relation) {
  140. case Selector::ComplexSelector::Relation::None:
  141. break;
  142. case Selector::ComplexSelector::Relation::ImmediateChild:
  143. relation_description = "ImmediateChild";
  144. break;
  145. case Selector::ComplexSelector::Relation::Descendant:
  146. relation_description = "Descendant";
  147. break;
  148. case Selector::ComplexSelector::Relation::AdjacentSibling:
  149. relation_description = "AdjacentSibling";
  150. break;
  151. case Selector::ComplexSelector::Relation::GeneralSibling:
  152. relation_description = "GeneralSibling";
  153. break;
  154. }
  155. if (*relation_description)
  156. dbgprintf("{%s} ", relation_description);
  157. for (int i = 0; i < complex_selector.compound_selector.size(); ++i) {
  158. auto& simple_selector = complex_selector.compound_selector[i];
  159. const char* type_description = "Unknown";
  160. switch (simple_selector.type) {
  161. case Selector::SimpleSelector::Type::Invalid:
  162. type_description = "Invalid";
  163. break;
  164. case Selector::SimpleSelector::Type::Universal:
  165. type_description = "Universal";
  166. break;
  167. case Selector::SimpleSelector::Type::Id:
  168. type_description = "Id";
  169. break;
  170. case Selector::SimpleSelector::Type::Class:
  171. type_description = "Class";
  172. break;
  173. case Selector::SimpleSelector::Type::TagName:
  174. type_description = "TagName";
  175. break;
  176. }
  177. const char* attribute_match_type_description = "";
  178. switch (simple_selector.attribute_match_type) {
  179. case Selector::SimpleSelector::AttributeMatchType::None:
  180. break;
  181. case Selector::SimpleSelector::AttributeMatchType::HasAttribute:
  182. attribute_match_type_description = "HasAttribute";
  183. break;
  184. case Selector::SimpleSelector::AttributeMatchType::ExactValueMatch:
  185. attribute_match_type_description = "ExactValueMatch";
  186. break;
  187. }
  188. dbgprintf("%s:%s", type_description, simple_selector.value.characters());
  189. if (simple_selector.attribute_match_type != Selector::SimpleSelector::AttributeMatchType::None) {
  190. dbgprintf(" [%s, name='%s', value='%s']", attribute_match_type_description, simple_selector.attribute_name.characters(), simple_selector.attribute_value.characters());
  191. }
  192. if (i != complex_selector.compound_selector.size() - 1)
  193. dbgprintf(", ");
  194. }
  195. dbgprintf("\n");
  196. }
  197. }
  198. dbgprintf(" Declarations:\n");
  199. for (auto& property : rule.declaration().properties()) {
  200. dbgprintf(" %s: '%s'\n", CSS::string_from_property_id(property.property_id), property.value->to_string().characters());
  201. }
  202. }
  203. void dump_sheet(const StyleSheet& sheet)
  204. {
  205. dbgprintf("StyleSheet{%p}: %d rule(s)\n", &sheet, sheet.rules().size());
  206. for (auto& rule : sheet.rules()) {
  207. dump_rule(rule);
  208. }
  209. }