Dump.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #include <LibHTML/DOM/Document.h>
  2. #include <LibHTML/DOM/Element.h>
  3. #include <LibHTML/DOM/Text.h>
  4. #include <LibHTML/Dump.h>
  5. #include <LibHTML/Layout/LayoutNode.h>
  6. #include <LibHTML/Layout/LayoutText.h>
  7. #include <stdio.h>
  8. void dump_tree(const Node& node)
  9. {
  10. static int indent = 0;
  11. for (int i = 0; i < indent; ++i)
  12. printf(" ");
  13. if (node.is_document()) {
  14. printf("*Document*\n");
  15. } else if (node.is_element()) {
  16. printf("<%s", static_cast<const Element&>(node).tag_name().characters());
  17. static_cast<const Element&>(node).for_each_attribute([](auto& name, auto& value) {
  18. printf(" %s=%s", name.characters(), value.characters());
  19. });
  20. printf(">\n");
  21. } else if (node.is_text()) {
  22. printf("\"%s\"\n", static_cast<const Text&>(node).data().characters());
  23. }
  24. ++indent;
  25. if (node.is_parent_node()) {
  26. static_cast<const ParentNode&>(node).for_each_child([](auto& child) {
  27. dump_tree(child);
  28. });
  29. }
  30. --indent;
  31. }
  32. void dump_tree(const LayoutNode& layout_node)
  33. {
  34. static int indent = 0;
  35. for (int i = 0; i < indent; ++i)
  36. printf(" ");
  37. String tag_name;
  38. if (layout_node.is_anonymous())
  39. tag_name = "(anonymous)";
  40. else if (layout_node.node()->is_text())
  41. tag_name = "#text";
  42. else if (layout_node.node()->is_document())
  43. tag_name = "#document";
  44. else if (layout_node.node()->is_element())
  45. tag_name = static_cast<const Element&>(*layout_node.node()).tag_name();
  46. else
  47. tag_name = "???";
  48. printf("%s {%s} at (%d,%d) size %dx%d",
  49. layout_node.class_name(),
  50. tag_name.characters(),
  51. layout_node.rect().x(),
  52. layout_node.rect().y(),
  53. layout_node.rect().width(),
  54. layout_node.rect().height());
  55. if (layout_node.is_text())
  56. printf(" \"%s\"", static_cast<const LayoutText&>(layout_node).text().characters());
  57. printf("\n");
  58. ++indent;
  59. layout_node.for_each_child([](auto& child) {
  60. dump_tree(child);
  61. });
  62. --indent;
  63. }