summaryrefslogtreecommitdiff
path: root/LibHTML/Dump.cpp
blob: 8fbaec164d94a4187a6c574ede369a7c2285ecad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <LibHTML/DOM/Document.h>
#include <LibHTML/DOM/Element.h>
#include <LibHTML/DOM/Text.h>
#include <LibHTML/Dump.h>
#include <LibHTML/Layout/LayoutNode.h>
#include <LibHTML/Layout/LayoutText.h>
#include <stdio.h>

void dump_tree(const Node& node)
{
    static int indent = 0;
    for (int i = 0; i < indent; ++i)
        printf("  ");
    if (node.is_document()) {
        printf("*Document*\n");
    } else if (node.is_element()) {
        printf("<%s", static_cast<const Element&>(node).tag_name().characters());
        static_cast<const Element&>(node).for_each_attribute([](auto& name, auto& value) {
            printf(" %s=%s", name.characters(), value.characters());
        });
        printf(">\n");
    } else if (node.is_text()) {
        printf("\"%s\"\n", static_cast<const Text&>(node).data().characters());
    }
    ++indent;
    if (node.is_parent_node()) {
        static_cast<const ParentNode&>(node).for_each_child([](auto& child) {
            dump_tree(child);
        });
    }
    --indent;
}

void dump_tree(const LayoutNode& layout_node)
{
    static int indent = 0;
    for (int i = 0; i < indent; ++i)
        printf("  ");

    String tag_name;
    if (layout_node.is_anonymous())
        tag_name = "(anonymous)";
    else if (layout_node.node()->is_text())
        tag_name = "#text";
    else if (layout_node.node()->is_document())
        tag_name = "#document";
    else if (layout_node.node()->is_element())
        tag_name = static_cast<const Element&>(*layout_node.node()).tag_name();
    else
        tag_name = "???";

    printf("%s {%s} at (%d,%d) size %dx%d",
        layout_node.class_name(),
        tag_name.characters(),
        layout_node.rect().x(),
        layout_node.rect().y(),
        layout_node.rect().width(),
        layout_node.rect().height());
    if (layout_node.is_text())
        printf(" \"%s\"", static_cast<const LayoutText&>(layout_node).text().characters());
    printf("\n");
    ++indent;
    layout_node.for_each_child([](auto& child) {
        dump_tree(child);
    });
    --indent;
}