Dump.cpp 5.0 KB

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