LibHTML: Get rid of the style tree

We now create a layout tree directly from the DOM tree.
This way we don't actually lose text nodes ^)
This commit is contained in:
Sergey Bugaev 2019-09-21 15:32:17 +03:00 committed by Andreas Kling
parent a9ebd676e5
commit fd0aa5dd43
22 changed files with 126 additions and 261 deletions

View file

@ -0,0 +1,22 @@
#include <LibHTML/CSS/StyleProperties.h>
void StyleProperties::set_property(const String& name, NonnullRefPtr<StyleValue> value)
{
m_property_values.set(name, move(value));
}
Optional<NonnullRefPtr<StyleValue>> StyleProperties::property(const String& name) const
{
auto it = m_property_values.find(name);
if (it == m_property_values.end())
return {};
return it->value;
}
Length StyleProperties::length_or_fallback(const StringView& property_name, const Length& fallback) const
{
auto value = property(property_name);
if (!value.has_value())
return fallback;
return value.value()->to_length();
}

View file

@ -0,0 +1,23 @@
#pragma once
#include <AK/HashMap.h>
#include <AK/NonnullRefPtr.h>
#include <LibHTML/CSS/StyleValue.h>
class StyleProperties {
public:
template<typename Callback>
inline void for_each_property(Callback callback) const
{
for (auto& it : m_property_values)
callback(it.key, *it.value);
}
void set_property(const String& name, NonnullRefPtr<StyleValue> value);
Optional<NonnullRefPtr<StyleValue>> property(const String& name) const;
Length length_or_fallback(const StringView& property_name, const Length& fallback) const;
private:
HashMap<String, NonnullRefPtr<StyleValue>> m_property_values;
};

View file

@ -1,6 +1,5 @@
#include <LibHTML/CSS/StyleResolver.h>
#include <LibHTML/CSS/StyleSheet.h>
#include <LibHTML/CSS/StyledNode.h>
#include <LibHTML/DOM/Document.h>
#include <LibHTML/DOM/Element.h>
#include <LibHTML/Dump.h>
@ -53,19 +52,14 @@ NonnullRefPtrVector<StyleRule> StyleResolver::collect_matching_rules(const Eleme
return matching_rules;
}
NonnullRefPtr<StyledNode> StyleResolver::create_styled_node(const Document& document)
StyleProperties StyleResolver::resolve_style(const Element& element)
{
return StyledNode::create(document);
}
NonnullRefPtr<StyledNode> StyleResolver::create_styled_node(const Element& element)
{
auto style = StyledNode::create(element);
StyleProperties style_properties;
auto matching_rules = collect_matching_rules(element);
for (auto& rule : matching_rules) {
for (auto& declaration : rule.declarations()) {
style->set_property(declaration.property_name(), declaration.value());
style_properties.set_property(declaration.property_name(), declaration.value());
}
}
return style;
return style_properties;
}

View file

@ -1,14 +1,14 @@
#pragma once
#include <AK/OwnPtr.h>
#include <AK/NonnullRefPtrVector.h>
#include <AK/OwnPtr.h>
#include <LibHTML/CSS/StyleProperties.h>
class Document;
class Element;
class ParentNode;
class StyleRule;
class StyleSheet;
class StyledNode;
class StyleResolver {
public:
@ -18,12 +18,10 @@ public:
Document& document() { return m_document; }
const Document& document() const { return m_document; }
NonnullRefPtr<StyledNode> create_styled_node(const Element&);
NonnullRefPtr<StyledNode> create_styled_node(const Document&);
StyleProperties resolve_style(const Element&);
NonnullRefPtrVector<StyleRule> collect_matching_rules(const Element&) const;
private:
Document& m_document;
};

View file

@ -1,25 +0,0 @@
#include <LibHTML/CSS/StyledNode.h>
StyledNode::StyledNode(const Node* node)
: m_node(node)
{
}
StyledNode::~StyledNode()
{
}
Display StyledNode::display() const
{
auto it = m_property_values.find("display");
if (it == m_property_values.end())
return Display::Inline;
auto value = it->value->to_string();
if (value == "none")
return Display::None;
if (value == "block")
return Display::Block;
if (value == "inline")
return Display::Inline;
ASSERT_NOT_REACHED();
}

View file

@ -1,78 +0,0 @@
#pragma once
#include <AK/String.h>
#include <AK/HashMap.h>
#include <AK/NonnullRefPtr.h>
#include <AK/Optional.h>
#include <LibHTML/CSS/StyleValue.h>
#include <LibHTML/TreeNode.h>
class Node;
enum class Display {
None,
Block,
Inline,
};
class StyledNode : public TreeNode<StyledNode> {
public:
static NonnullRefPtr<StyledNode> create(const Node& node)
{
return adopt(*new StyledNode(&node));
}
~StyledNode();
const Node* node() const { return m_node; }
template<typename Callback>
inline void for_each_child(Callback callback) const
{
for (auto* node = first_child(); node; node = node->next_sibling())
callback(*node);
}
template<typename Callback>
inline void for_each_child(Callback callback)
{
for (auto* node = first_child(); node; node = node->next_sibling())
callback(*node);
}
template<typename Callback>
inline void for_each_property(Callback callback) const
{
for (auto& it : m_property_values)
callback(it.key, *it.value);
}
void set_property(const String& name, NonnullRefPtr<StyleValue> value)
{
m_property_values.set(name, move(value));
}
Optional<NonnullRefPtr<StyleValue>> property(const String& name) const
{
auto it = m_property_values.find(name);
if (it == m_property_values.end())
return {};
return it->value;
}
Display display() const;
Length length_or_fallback(const StringView& property_name, const Length& fallback)
{
auto value = property(property_name);
if (!value.has_value())
return fallback;
return value.value()->to_length();
}
protected:
explicit StyledNode(const Node*);
private:
const Node* m_node { nullptr };
HashMap<String, NonnullRefPtr<StyleValue>> m_property_values;
};

View file

@ -1,5 +1,4 @@
#include <LibHTML/CSS/StyleSheet.h>
#include <LibHTML/CSS/StyledNode.h>
#include <LibHTML/DOM/Document.h>
#include <LibHTML/DOM/Element.h>
#include <LibHTML/DOM/Text.h>
@ -37,7 +36,7 @@ void dump_tree(const LayoutNode& layout_node)
{
static int indent = 0;
for (int i = 0; i < indent; ++i)
printf(" ");
printf(" ");
String tag_name;
if (layout_node.is_anonymous())
@ -83,40 +82,15 @@ void dump_tree(const LayoutNode& layout_node)
printf(" \"%s\"", static_cast<const LayoutText&>(layout_node).text().characters());
printf("\n");
++indent;
layout_node.for_each_child([](auto& child) {
dump_tree(child);
});
--indent;
}
void dump_tree(const StyledNode& styled_node)
{
static int indent = 0;
for (int i = 0; i < indent; ++i)
printf(" ");
String tag_name;
auto& node = *styled_node.node();
if (node.is_text())
tag_name = "#text";
else if (node.is_document())
tag_name = "#document";
else if (node.is_element())
tag_name = static_cast<const Element&>(node).tag_name();
else
tag_name = "???";
printf("%s", tag_name.characters());
printf("\n");
styled_node.for_each_property([&](auto& key, auto& value) {
layout_node.style_properties().for_each_property([&](auto& key, auto& value) {
for (int i = 0; i < indent; ++i)
printf(" ");
printf(" (%s: %s)\n", key.characters(), value.to_string().characters());
});
++indent;
styled_node.for_each_child([](auto& child) {
layout_node.for_each_child([](auto& child) {
dump_tree(child);
});
--indent;

View file

@ -4,10 +4,8 @@ class Node;
class LayoutNode;
class StyleRule;
class StyleSheet;
class StyledNode;
void dump_tree(const Node&);
void dump_tree(const StyledNode&);
void dump_tree(const LayoutNode&);
void dump_sheet(const StyleSheet&);
void dump_rule(const StyleRule&);

View file

@ -1,6 +1,5 @@
#include <AK/Function.h>
#include <LibHTML/CSS/StyleResolver.h>
#include <LibHTML/CSS/StyledNode.h>
#include <LibHTML/DOM/Element.h>
#include <LibHTML/Dump.h>
#include <LibHTML/Frame.h>
@ -23,71 +22,46 @@ void Frame::set_document(Document* document)
m_document = document;
}
RefPtr<StyledNode> Frame::generate_style_tree()
RefPtr<LayoutNode> Frame::generate_layout_tree()
{
if (!m_document)
return nullptr;
auto resolver = m_document->style_resolver();
auto create_layout_node = [&](const Node& node) -> RefPtr<LayoutNode> {
if (node.is_document())
return adopt(*new LayoutDocument(static_cast<const Document&>(node), {}));
auto& resolver = m_document->style_resolver();
Function<RefPtr<StyledNode>(const Node&, StyledNode*)> resolve_style = [&](const Node& node, StyledNode* parent_styled_node) -> RefPtr<StyledNode> {
RefPtr<StyledNode> styled_node;
if (node.is_element())
styled_node = resolver.create_styled_node(static_cast<const Element&>(node));
else if (node.is_document())
styled_node = resolver.create_styled_node(static_cast<const Document&>(node));
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(*m_document, nullptr);
dump_tree(*styled_root);
return styled_root;
}
auto style_properties = resolver.resolve_style(static_cast<const Element&>(node));
auto display_property = style_properties.property("display");
String display = display_property.has_value() ? display_property.release_value()->to_string() : "inline";
RefPtr<LayoutNode> Frame::generate_layout_tree(const StyledNode& styled_root)
{
auto create_layout_node = [](const StyledNode& styled_node) -> RefPtr<LayoutNode> {
if (styled_node.node() && styled_node.node()->is_document())
return adopt(*new LayoutDocument(static_cast<const Document&>(*styled_node.node()), styled_node));
switch (styled_node.display()) {
case Display::None:
if (display == "none")
return nullptr;
case Display::Block:
return adopt(*new LayoutBlock(styled_node.node(), &styled_node));
case Display::Inline:
return adopt(*new LayoutInline(*styled_node.node(), styled_node));
default:
ASSERT_NOT_REACHED();
}
if (display == "block")
return adopt(*new LayoutBlock(&node, move(style_properties)));
if (display == "inline")
return adopt(*new LayoutInline(node, move(style_properties)));
ASSERT_NOT_REACHED();
};
Function<RefPtr<LayoutNode>(const StyledNode&)> build_layout_tree;
build_layout_tree = [&](const StyledNode& styled_node) -> RefPtr<LayoutNode> {
auto layout_node = create_layout_node(styled_node);
Function<RefPtr<LayoutNode>(const Node&)> build_layout_tree;
build_layout_tree = [&](const Node& node) -> RefPtr<LayoutNode> {
auto layout_node = create_layout_node(node);
if (!layout_node)
return nullptr;
if (!styled_node.has_children())
if (!node.has_children())
return layout_node;
for (auto* styled_child = styled_node.first_child(); styled_child; styled_child = styled_child->next_sibling()) {
auto layout_child = build_layout_tree(*styled_child);
static_cast<const ParentNode&>(node).for_each_child([&](const Node& child) {
auto layout_child = build_layout_tree(child);
if (!layout_child)
continue;
return;
if (layout_child->is_inline())
layout_node->inline_wrapper().append_child(*layout_child);
else
layout_node->append_child(*layout_child);
}
});
return layout_node;
};
return build_layout_tree(styled_root);
return build_layout_tree(*m_document);
}
void Frame::layout()
@ -95,8 +69,7 @@ void Frame::layout()
if (!m_document)
return;
auto styled_root = generate_style_tree();
auto layout_root = generate_layout_tree(*styled_root);
auto layout_root = generate_layout_tree();
layout_root->style().size().set_width(m_size.width());

View file

@ -1,7 +1,7 @@
#pragma once
#include <LibHTML/DOM/Document.h>
#include <LibDraw/Size.h>
#include <LibHTML/DOM/Document.h>
class Frame {
public:
@ -16,8 +16,7 @@ public:
void layout();
private:
RefPtr<StyledNode> generate_style_tree();
RefPtr<LayoutNode> generate_layout_tree(const StyledNode&);
RefPtr<LayoutNode> generate_layout_tree();
RefPtr<Document> m_document;
Size m_size;

View file

@ -1,9 +1,8 @@
#include <LibHTML/CSS/StyledNode.h>
#include <LibHTML/DOM/Element.h>
#include <LibHTML/Layout/LayoutBlock.h>
LayoutBlock::LayoutBlock(const Node* node, const StyledNode* styled_node)
: LayoutNode(node, styled_node)
LayoutBlock::LayoutBlock(const Node* node, StyleProperties&& style_properties)
: LayoutNode(node, move(style_properties))
{
}
@ -14,7 +13,7 @@ LayoutBlock::~LayoutBlock()
LayoutNode& LayoutBlock::inline_wrapper()
{
if (!last_child() || !last_child()->is_block()) {
append_child(adopt(*new LayoutBlock(nullptr, nullptr)));
append_child(adopt(*new LayoutBlock(nullptr, {})));
}
return *last_child();
}
@ -42,21 +41,17 @@ void LayoutBlock::layout()
void LayoutBlock::compute_width()
{
if (!styled_node()) {
// I guess the size is "auto" in this case.
return;
}
auto& style_properties = this->style_properties();
auto& styled_node = *this->styled_node();
auto auto_value = Length();
auto zero_value = Length(0, Length::Type::Absolute);
auto width = styled_node.length_or_fallback("width", auto_value);
auto margin_left = styled_node.length_or_fallback("margin-left", zero_value);
auto margin_right = styled_node.length_or_fallback("margin-right", zero_value);
auto border_left = styled_node.length_or_fallback("border-left", zero_value);
auto border_right = styled_node.length_or_fallback("border-right", zero_value);
auto padding_left = styled_node.length_or_fallback("padding-left", zero_value);
auto padding_right = styled_node.length_or_fallback("padding-right", zero_value);
auto width = style_properties.length_or_fallback("width", auto_value);
auto margin_left = style_properties.length_or_fallback("margin-left", zero_value);
auto margin_right = style_properties.length_or_fallback("margin-right", zero_value);
auto border_left = style_properties.length_or_fallback("border-left", zero_value);
auto border_right = style_properties.length_or_fallback("border-right", zero_value);
auto padding_left = style_properties.length_or_fallback("padding-left", zero_value);
auto padding_right = style_properties.length_or_fallback("padding-right", zero_value);
dbg() << " Left: " << margin_left << "+" << border_left << "+" << padding_left;
dbg() << "Right: " << margin_right << "+" << border_right << "+" << padding_right;
@ -116,32 +111,27 @@ void LayoutBlock::compute_width()
void LayoutBlock::compute_position()
{
if (!styled_node()) {
// I guess the size is "auto" in this case.
return;
}
auto& style_properties = this->style_properties();
auto& styled_node = *this->styled_node();
auto auto_value = Length();
auto zero_value = Length(0, Length::Type::Absolute);
auto width = styled_node.length_or_fallback("width", auto_value);
style().margin().top = styled_node.length_or_fallback("margin-top", zero_value);
style().margin().bottom = styled_node.length_or_fallback("margin-bottom", zero_value);
style().border().top = styled_node.length_or_fallback("border-top", zero_value);
style().border().bottom = styled_node.length_or_fallback("border-bottom", zero_value);
style().padding().top = styled_node.length_or_fallback("padding-top", zero_value);
style().padding().bottom = styled_node.length_or_fallback("padding-bottom", zero_value);
auto width = style_properties.length_or_fallback("width", auto_value);
style().margin().top = style_properties.length_or_fallback("margin-top", zero_value);
style().margin().bottom = style_properties.length_or_fallback("margin-bottom", zero_value);
style().border().top = style_properties.length_or_fallback("border-top", zero_value);
style().border().bottom = style_properties.length_or_fallback("border-bottom", zero_value);
style().padding().top = style_properties.length_or_fallback("padding-top", zero_value);
style().padding().bottom = style_properties.length_or_fallback("padding-bottom", zero_value);
rect().set_x(containing_block()->rect().x() + style().margin().left.to_px() + style().border().left.to_px() + style().padding().left.to_px());
rect().set_y(containing_block()->rect().y() + style().margin().top.to_px() + style().border().top.to_px() + style().padding().top.to_px());
}
void LayoutBlock::compute_height()
{
if (!styled_node())
return;
auto& styled_node = *this->styled_node();
auto height_property = styled_node.property("height");
auto& style_properties = this->style_properties();
auto height_property = style_properties.property("height");
if (!height_property.has_value())
return;
auto height_length = height_property.value()->to_length();

View file

@ -6,7 +6,7 @@ class Element;
class LayoutBlock : public LayoutNode {
public:
LayoutBlock(const Node*, const StyledNode*);
LayoutBlock(const Node*, StyleProperties&&);
virtual ~LayoutBlock() override;
virtual const char* class_name() const override { return "LayoutBlock"; }

View file

@ -1,7 +1,7 @@
#include <LibHTML/Layout/LayoutDocument.h>
LayoutDocument::LayoutDocument(const Document& document, const StyledNode& styled_node)
: LayoutBlock(&document, &styled_node)
LayoutDocument::LayoutDocument(const Document& document, StyleProperties&& style_properties)
: LayoutBlock(&document, move(style_properties))
{
}

View file

@ -1,15 +1,16 @@
#pragma once
#include <LibHTML/Layout/LayoutBlock.h>
#include <LibHTML/DOM/Document.h>
#include <LibHTML/Layout/LayoutBlock.h>
class LayoutDocument final : public LayoutBlock {
public:
LayoutDocument(const Document&, const StyledNode&);
LayoutDocument(const Document&, StyleProperties&&);
virtual ~LayoutDocument() override;
const Document& node() const { return static_cast<const Document&>(*LayoutNode::node()); }
virtual const char* class_name() const override { return "LayoutDocument"; }
virtual void layout() override;
private:
};

View file

@ -1,8 +1,8 @@
#include <LibHTML/DOM/Element.h>
#include <LibHTML/Layout/LayoutInline.h>
LayoutInline::LayoutInline(const Node& node, const StyledNode& styled_node)
: LayoutNode(&node, &styled_node)
LayoutInline::LayoutInline(const Node& node, StyleProperties&& style_properties)
: LayoutNode(&node, move(style_properties))
{
}

View file

@ -6,7 +6,7 @@ class Element;
class LayoutInline : public LayoutNode {
public:
LayoutInline(const Node&, const StyledNode&);
LayoutInline(const Node&, StyleProperties&&);
virtual ~LayoutInline() override;
virtual const char* class_name() const override { return "LayoutInline"; }

View file

@ -1,10 +1,9 @@
#include <LibHTML/Layout/LayoutBlock.h>
#include <LibHTML/Layout/LayoutNode.h>
#include <LibHTML/CSS/StyledNode.h>
LayoutNode::LayoutNode(const Node* node, const StyledNode* styled_node)
LayoutNode::LayoutNode(const Node* node, StyleProperties&& style_properties)
: m_node(node)
, m_styled_node(styled_node)
, m_style_properties(style_properties)
{
}

View file

@ -2,13 +2,13 @@
#include <AK/NonnullRefPtr.h>
#include <AK/Vector.h>
#include <LibDraw/Rect.h>
#include <LibHTML/CSS/StyleProperties.h>
#include <LibHTML/Layout/ComputedStyle.h>
#include <LibHTML/TreeNode.h>
#include <LibDraw/Rect.h>
class Node;
class LayoutBlock;
class StyledNode;
class LayoutNode : public TreeNode<LayoutNode> {
public:
@ -49,16 +49,15 @@ public:
virtual LayoutNode& inline_wrapper() { return *this; }
StyledNode* styled_node() { return m_styled_node; }
const StyledNode* styled_node() const { return m_styled_node; }
const StyleProperties& style_properties() const { return m_style_properties; }
protected:
explicit LayoutNode(const Node*, const StyledNode*);
explicit LayoutNode(const Node*, StyleProperties&&);
private:
const Node* m_node { nullptr };
RefPtr<StyledNode> m_styled_node;
StyleProperties m_style_properties;
ComputedStyle m_style;
Rect m_rect;
};

View file

@ -1,8 +1,8 @@
#include <LibHTML/Layout/LayoutText.h>
#include <ctype.h>
LayoutText::LayoutText(const Text& text, const StyledNode& styled_node)
: LayoutNode(&text, &styled_node)
LayoutText::LayoutText(const Text& text, StyleProperties&& style_properties)
: LayoutNode(&text, move(style_properties))
{
}
@ -29,7 +29,6 @@ const String& LayoutText::text() const
void LayoutText::compute_runs()
{
}
void LayoutText::layout()

View file

@ -1,11 +1,11 @@
#pragma once
#include <LibHTML/Layout/LayoutNode.h>
#include <LibHTML/DOM/Text.h>
#include <LibHTML/Layout/LayoutNode.h>
class LayoutText : public LayoutNode {
public:
LayoutText(const Text&, const StyledNode&);
LayoutText(const Text&, StyleProperties&&);
virtual ~LayoutText() override;
const Text& node() const { return static_cast<const Text&>(*LayoutNode::node()); }

View file

@ -9,7 +9,7 @@ LIBHTML_OBJS = \
CSS/StyleRule.o \
CSS/StyleDeclaration.o \
CSS/StyleValue.o \
CSS/StyledNode.o \
CSS/StyleProperties.o \
CSS/StyleResolver.o \
CSS/DefaultStyleSheetSource.o \
Parser/HTMLParser.o \

View file

@ -1,6 +1,5 @@
#include <LibCore/CFile.h>
#include <LibHTML/CSS/StyleResolver.h>
#include <LibHTML/CSS/StyledNode.h>
#include <LibHTML/DOM/Element.h>
#include <LibHTML/Dump.h>
#include <LibHTML/Frame.h>