1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-27 18:55:09 +00:00

LibHTML: Refactor to go from DOM -> styled tree -> layout tree.

Frame::layout() drives everything now, it takes the DOM contained in the
frame and puts it through the tree transformations.
This commit is contained in:
Andreas Kling 2019-06-29 21:42:07 +02:00
parent 6e95b11395
commit 7eef69ad4b
20 changed files with 132 additions and 131 deletions

View file

@ -1,10 +1,12 @@
#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/Dump.h>
#include <LibHTML/Frame.h>
#include <LibHTML/Layout/LayoutBlock.h>
#include <LibHTML/Layout/LayoutInline.h>
#include <LibHTML/Parser/CSSParser.h>
#include <LibHTML/Parser/HTMLParser.h>
#include <stdio.h>
@ -23,47 +25,12 @@ int main(int argc, char** argv)
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 document = parse_html(html);
dump_tree(document);
document->add_sheet(*sheet);
auto frame = make<Frame>();
frame->set_document(doc);
frame->set_document(document);
frame->layout();
printf("\033[33;1mLayout tree after layout:\033[0m\n");
dump_tree(*doc->layout_node());
return 0;
}