summaryrefslogtreecommitdiff
path: root/LibHTML/test.cpp
blob: e3d23837d842ee724d1a5b30d1b0fc7104d3ffdc (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
68
69
#include <LibCore/CFile.h>
#include <LibHTML/Dump.h>
#include <LibHTML/Frame.h>
#include <LibHTML/Parser/CSSParser.h>
#include <LibHTML/CSS/StyleResolver.h>
#include <LibHTML/CSS/StyledNode.h>
#include <LibHTML/DOM/Element.h>
#include <LibHTML/Parser/HTMLParser.h>
#include <stdio.h>

int main(int argc, char** argv)
{
    CFile f(argc == 1 ? "/home/anon/small.html" : argv[1]);
    if (!f.open(CIODevice::ReadOnly)) {
        fprintf(stderr, "Error: %s\n", f.error_string());
        return 1;
    }

    extern const char default_stylesheet_source[];
    String css = default_stylesheet_source;

    auto sheet = parse_css(css);
    dump_sheet(sheet);

    String html = String::copy(f.read_all());
    auto doc = parse_html(html);
    dump_tree(doc);

    StyleResolver resolver(*doc);
    resolver.add_sheet(*sheet);

    Function<RefPtr<StyledNode>(const Node&, StyledNode*)> resolve_style = [&](const Node& node, StyledNode* parent_styled_node) -> RefPtr<StyledNode> {
        auto styled_node = [&]() -> RefPtr<StyledNode> {
            if (node.is_element())
                return resolver.create_styled_node(static_cast<const Element&>(node));
            if (node.is_document())
                return resolver.create_styled_node(static_cast<const Document&>(node));
            return nullptr;
        }();
        if (!styled_node)
            return nullptr;
        if (parent_styled_node)
            parent_styled_node->append_child(*styled_node);
        static_cast<const ParentNode&>(node).for_each_child([&](const Node& child) {
            if (!child.is_element())
                return;
            auto styled_child_node = resolve_style(static_cast<const Element&>(child), styled_node.ptr());
            printf("Created StyledNode{%p} for Element{%p}\n", styled_child_node.ptr(), &node);
        });
        return styled_node;
    };
    auto styled_root = resolve_style(*doc, nullptr);

    dump_tree(*styled_root);

    doc->build_layout_tree();
    ASSERT(doc->layout_node());

    printf("\033[33;1mLayout tree before layout:\033[0m\n");
    dump_tree(*doc->layout_node());

    auto frame = make<Frame>();
    frame->set_document(doc);
    frame->layout();

    printf("\033[33;1mLayout tree after layout:\033[0m\n");
    dump_tree(*doc->layout_node());
    return 0;
}