Dump.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #include <LibHTML/CSS/StyleSheet.h>
  2. #include <LibHTML/DOM/Document.h>
  3. #include <LibHTML/DOM/Element.h>
  4. #include <LibHTML/DOM/Text.h>
  5. #include <LibHTML/Dump.h>
  6. #include <LibHTML/Layout/LayoutNode.h>
  7. #include <LibHTML/Layout/LayoutText.h>
  8. #include <stdio.h>
  9. void dump_tree(const Node& node)
  10. {
  11. static int indent = 0;
  12. for (int i = 0; i < indent; ++i)
  13. printf(" ");
  14. if (node.is_document()) {
  15. printf("*Document*\n");
  16. } else if (node.is_element()) {
  17. printf("<%s", static_cast<const Element&>(node).tag_name().characters());
  18. static_cast<const Element&>(node).for_each_attribute([](auto& name, auto& value) {
  19. printf(" %s=%s", name.characters(), value.characters());
  20. });
  21. printf(">\n");
  22. } else if (node.is_text()) {
  23. printf("\"%s\"\n", static_cast<const Text&>(node).data().characters());
  24. }
  25. ++indent;
  26. if (node.is_parent_node()) {
  27. static_cast<const ParentNode&>(node).for_each_child([](auto& child) {
  28. dump_tree(child);
  29. });
  30. }
  31. --indent;
  32. }
  33. void dump_tree(const LayoutNode& layout_node)
  34. {
  35. static int indent = 0;
  36. for (int i = 0; i < indent; ++i)
  37. printf(" ");
  38. String tag_name;
  39. if (layout_node.is_anonymous())
  40. tag_name = "(anonymous)";
  41. else if (layout_node.node()->is_text())
  42. tag_name = "#text";
  43. else if (layout_node.node()->is_document())
  44. tag_name = "#document";
  45. else if (layout_node.node()->is_element())
  46. tag_name = static_cast<const Element&>(*layout_node.node()).tag_name();
  47. else
  48. tag_name = "???";
  49. printf("%s {%s} at (%d,%d) size %dx%d",
  50. layout_node.class_name(),
  51. tag_name.characters(),
  52. layout_node.rect().x(),
  53. layout_node.rect().y(),
  54. layout_node.rect().width(),
  55. layout_node.rect().height());
  56. // Dump the horizontal box properties
  57. printf(" [%d+%d+%d %d %d+%d+%d]",
  58. layout_node.style().margin().left.to_px(),
  59. layout_node.style().border().left.to_px(),
  60. layout_node.style().padding().left.to_px(),
  61. layout_node.rect().width(),
  62. layout_node.style().padding().right.to_px(),
  63. layout_node.style().border().right.to_px(),
  64. layout_node.style().margin().right.to_px());
  65. // And the vertical box properties
  66. printf(" [%d+%d+%d %d %d+%d+%d]",
  67. layout_node.style().margin().top.to_px(),
  68. layout_node.style().border().top.to_px(),
  69. layout_node.style().padding().top.to_px(),
  70. layout_node.rect().height(),
  71. layout_node.style().padding().bottom.to_px(),
  72. layout_node.style().border().bottom.to_px(),
  73. layout_node.style().margin().bottom.to_px());
  74. if (layout_node.is_text()) {
  75. const LayoutText& layout_text = static_cast<const LayoutText&>(layout_node);
  76. printf(" \"%s\", %d runs", layout_text.text().characters(), layout_text.runs().size());
  77. }
  78. printf("\n");
  79. layout_node.style_properties().for_each_property([&](auto& key, auto& value) {
  80. for (int i = 0; i < indent; ++i)
  81. printf(" ");
  82. printf(" (%s: %s)\n", key.characters(), value.to_string().characters());
  83. });
  84. ++indent;
  85. layout_node.for_each_child([](auto& child) {
  86. dump_tree(child);
  87. });
  88. --indent;
  89. }
  90. void dump_rule(const StyleRule& rule)
  91. {
  92. printf("Rule:\n");
  93. for (auto& selector : rule.selectors()) {
  94. printf(" Selector:\n");
  95. for (auto& component : selector.components()) {
  96. const char* type_description = "Unknown";
  97. switch (component.type) {
  98. case Selector::Component::Type::Invalid:
  99. type_description = "Invalid";
  100. break;
  101. case Selector::Component::Type::Id:
  102. type_description = "Id";
  103. break;
  104. case Selector::Component::Type::Class:
  105. type_description = "Class";
  106. break;
  107. case Selector::Component::Type::TagName:
  108. type_description = "TagName";
  109. break;
  110. }
  111. printf(" %s:%s\n", type_description, component.value.characters());
  112. }
  113. }
  114. printf(" Declarations:\n");
  115. for (auto& declaration : rule.declarations()) {
  116. printf(" '%s': '%s'\n", declaration.property_name().characters(), declaration.value().to_string().characters());
  117. }
  118. }
  119. void dump_sheet(const StyleSheet& sheet)
  120. {
  121. printf("StyleSheet{%p}: %d rule(s)\n", &sheet, sheet.rules().size());
  122. for (auto& rule : sheet.rules()) {
  123. dump_rule(rule);
  124. }
  125. }