diff --git a/LibHTML/Frame.cpp b/LibHTML/Frame.cpp
new file mode 100644
index 00000000000..73e35b06653
--- /dev/null
+++ b/LibHTML/Frame.cpp
@@ -0,0 +1,31 @@
+#include
+#include
+
+Frame::Frame()
+ : m_size(800, 600)
+{
+}
+
+Frame::~Frame()
+{
+}
+
+void Frame::set_document(Document* document)
+{
+ m_document = document;
+}
+
+void Frame::layout()
+{
+ if (!m_document)
+ return;
+
+ if (!m_document->layout_node())
+ m_document->create_layout_node();
+
+ ASSERT(m_document->layout_node());
+
+ m_document->layout_node()->style().size().set_width(m_size.width());
+
+ m_document->layout_node()->layout();
+}
diff --git a/LibHTML/Frame.h b/LibHTML/Frame.h
new file mode 100644
index 00000000000..3007a02fbfb
--- /dev/null
+++ b/LibHTML/Frame.h
@@ -0,0 +1,21 @@
+#pragma once
+
+#include
+#include
+
+class Frame {
+public:
+ Frame();
+ ~Frame();
+
+ Document* document() { return m_document.ptr(); }
+ const Document* document() const { return m_document.ptr(); }
+
+ void set_document(Document*);
+
+ void layout();
+
+private:
+ RetainPtr m_document;
+ Size m_size;
+};
diff --git a/LibHTML/Layout/LayoutDocument.cpp b/LibHTML/Layout/LayoutDocument.cpp
index e3ca9a2241c..3698dc73a4a 100644
--- a/LibHTML/Layout/LayoutDocument.cpp
+++ b/LibHTML/Layout/LayoutDocument.cpp
@@ -8,3 +8,9 @@ LayoutDocument::LayoutDocument(const Document& document)
LayoutDocument::~LayoutDocument()
{
}
+
+void LayoutDocument::layout()
+{
+ rect().set_width(style().size().width());
+ LayoutNode::layout();
+}
diff --git a/LibHTML/Layout/LayoutDocument.h b/LibHTML/Layout/LayoutDocument.h
index 370834eb62e..a06e9f470b3 100644
--- a/LibHTML/Layout/LayoutDocument.h
+++ b/LibHTML/Layout/LayoutDocument.h
@@ -3,14 +3,13 @@
#include
#include
-class LayoutDocument : public LayoutNode {
+class LayoutDocument final : public LayoutNode {
public:
explicit LayoutDocument(const Document&);
virtual ~LayoutDocument() override;
const Document& node() const { return static_cast(*LayoutNode::node()); }
-
virtual const char* class_name() const override { return "LayoutDocument"; }
-
+ virtual void layout() override;
private:
};
diff --git a/LibHTML/Layout/LayoutNode.cpp b/LibHTML/Layout/LayoutNode.cpp
index 587d8138686..2e441bdfc12 100644
--- a/LibHTML/Layout/LayoutNode.cpp
+++ b/LibHTML/Layout/LayoutNode.cpp
@@ -31,3 +31,10 @@ void LayoutNode::append_child(Retained node)
if (!m_first_child)
m_first_child = m_last_child;
}
+
+void LayoutNode::layout()
+{
+ for_each_child([](auto& child) {
+ child.layout();
+ });
+}
diff --git a/LibHTML/Layout/LayoutNode.h b/LibHTML/Layout/LayoutNode.h
index 76d56043e41..ce1e040a8b5 100644
--- a/LibHTML/Layout/LayoutNode.h
+++ b/LibHTML/Layout/LayoutNode.h
@@ -16,6 +16,7 @@ public:
int retain_count() const { return m_retain_count; }
const Rect& rect() const { return m_rect; }
+ Rect& rect() { return m_rect; }
void set_rect(const Rect& rect) { m_rect = rect; }
LayoutStyle& style() { return m_style; }
@@ -45,9 +46,18 @@ public:
callback(*node);
}
+ template
+ inline void for_each_child(Callback callback)
+ {
+ for (auto* node = first_child(); node; node = node->next_sibling())
+ callback(*node);
+ }
+
virtual const char* class_name() const { return "LayoutNode"; }
virtual bool is_text() const { return false; }
+ virtual void layout();
+
protected:
explicit LayoutNode(const Node*);
diff --git a/LibHTML/Layout/LayoutStyle.h b/LibHTML/Layout/LayoutStyle.h
index fd92e75d3e4..99bb54fec4d 100644
--- a/LibHTML/Layout/LayoutStyle.h
+++ b/LibHTML/Layout/LayoutStyle.h
@@ -1,6 +1,7 @@
#pragma once
#include
+#include
struct Box {
int top { 0 };
@@ -32,6 +33,9 @@ public:
FontStyle font_style() const { return m_font_style; }
+ const Size& size() const { return m_size; }
+ Size& size() { return m_size; }
+
private:
Color m_text_color;
Color m_background_color;
@@ -40,5 +44,7 @@ private:
Box m_margin;
Box m_padding;
+ Size m_size;
+
FontStyle m_font_style { FontStyle::Normal };
};
diff --git a/LibHTML/Makefile b/LibHTML/Makefile
index 05996e29cc5..1b068adc31e 100644
--- a/LibHTML/Makefile
+++ b/LibHTML/Makefile
@@ -13,6 +13,7 @@ LIBHTML_OBJS = \
Layout/LayoutInline.o \
Layout/LayoutDocument.o \
Layout/LayoutStyle.o \
+ Frame.o \
Dump.o
TEST_OBJS = test.o
diff --git a/LibHTML/test.cpp b/LibHTML/test.cpp
index 56b3305fc55..51a86bdd268 100644
--- a/LibHTML/test.cpp
+++ b/LibHTML/test.cpp
@@ -1,5 +1,6 @@
#include
#include
+#include
#include
#include
@@ -16,6 +17,12 @@ int main(int argc, char** argv)
doc->build_layout_tree();
ASSERT(doc->layout_node());
+ dump_tree(*doc->layout_node());
+
+ auto frame = make();
+ frame->set_document(doc);
+ frame->layout();
+
dump_tree(*doc->layout_node());
return 0;
}