mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 18:27:35 +00:00
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:
parent
a9ebd676e5
commit
fd0aa5dd43
22 changed files with 126 additions and 261 deletions
22
Libraries/LibHTML/CSS/StyleProperties.cpp
Normal file
22
Libraries/LibHTML/CSS/StyleProperties.cpp
Normal 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();
|
||||||
|
}
|
23
Libraries/LibHTML/CSS/StyleProperties.h
Normal file
23
Libraries/LibHTML/CSS/StyleProperties.h
Normal 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;
|
||||||
|
};
|
|
@ -1,6 +1,5 @@
|
||||||
#include <LibHTML/CSS/StyleResolver.h>
|
#include <LibHTML/CSS/StyleResolver.h>
|
||||||
#include <LibHTML/CSS/StyleSheet.h>
|
#include <LibHTML/CSS/StyleSheet.h>
|
||||||
#include <LibHTML/CSS/StyledNode.h>
|
|
||||||
#include <LibHTML/DOM/Document.h>
|
#include <LibHTML/DOM/Document.h>
|
||||||
#include <LibHTML/DOM/Element.h>
|
#include <LibHTML/DOM/Element.h>
|
||||||
#include <LibHTML/Dump.h>
|
#include <LibHTML/Dump.h>
|
||||||
|
@ -53,19 +52,14 @@ NonnullRefPtrVector<StyleRule> StyleResolver::collect_matching_rules(const Eleme
|
||||||
return matching_rules;
|
return matching_rules;
|
||||||
}
|
}
|
||||||
|
|
||||||
NonnullRefPtr<StyledNode> StyleResolver::create_styled_node(const Document& document)
|
StyleProperties StyleResolver::resolve_style(const Element& element)
|
||||||
{
|
{
|
||||||
return StyledNode::create(document);
|
StyleProperties style_properties;
|
||||||
}
|
|
||||||
|
|
||||||
NonnullRefPtr<StyledNode> StyleResolver::create_styled_node(const Element& element)
|
|
||||||
{
|
|
||||||
auto style = StyledNode::create(element);
|
|
||||||
auto matching_rules = collect_matching_rules(element);
|
auto matching_rules = collect_matching_rules(element);
|
||||||
for (auto& rule : matching_rules) {
|
for (auto& rule : matching_rules) {
|
||||||
for (auto& declaration : rule.declarations()) {
|
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;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <AK/OwnPtr.h>
|
|
||||||
#include <AK/NonnullRefPtrVector.h>
|
#include <AK/NonnullRefPtrVector.h>
|
||||||
|
#include <AK/OwnPtr.h>
|
||||||
|
#include <LibHTML/CSS/StyleProperties.h>
|
||||||
|
|
||||||
class Document;
|
class Document;
|
||||||
class Element;
|
class Element;
|
||||||
class ParentNode;
|
class ParentNode;
|
||||||
class StyleRule;
|
class StyleRule;
|
||||||
class StyleSheet;
|
class StyleSheet;
|
||||||
class StyledNode;
|
|
||||||
|
|
||||||
class StyleResolver {
|
class StyleResolver {
|
||||||
public:
|
public:
|
||||||
|
@ -18,12 +18,10 @@ public:
|
||||||
Document& document() { return m_document; }
|
Document& document() { return m_document; }
|
||||||
const Document& document() const { return m_document; }
|
const Document& document() const { return m_document; }
|
||||||
|
|
||||||
NonnullRefPtr<StyledNode> create_styled_node(const Element&);
|
StyleProperties resolve_style(const Element&);
|
||||||
NonnullRefPtr<StyledNode> create_styled_node(const Document&);
|
|
||||||
|
|
||||||
NonnullRefPtrVector<StyleRule> collect_matching_rules(const Element&) const;
|
NonnullRefPtrVector<StyleRule> collect_matching_rules(const Element&) const;
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Document& m_document;
|
Document& m_document;
|
||||||
};
|
};
|
||||||
|
|
|
@ -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();
|
|
||||||
}
|
|
|
@ -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;
|
|
||||||
};
|
|
|
@ -1,5 +1,4 @@
|
||||||
#include <LibHTML/CSS/StyleSheet.h>
|
#include <LibHTML/CSS/StyleSheet.h>
|
||||||
#include <LibHTML/CSS/StyledNode.h>
|
|
||||||
#include <LibHTML/DOM/Document.h>
|
#include <LibHTML/DOM/Document.h>
|
||||||
#include <LibHTML/DOM/Element.h>
|
#include <LibHTML/DOM/Element.h>
|
||||||
#include <LibHTML/DOM/Text.h>
|
#include <LibHTML/DOM/Text.h>
|
||||||
|
@ -83,40 +82,15 @@ void dump_tree(const LayoutNode& layout_node)
|
||||||
printf(" \"%s\"", static_cast<const LayoutText&>(layout_node).text().characters());
|
printf(" \"%s\"", static_cast<const LayoutText&>(layout_node).text().characters());
|
||||||
|
|
||||||
printf("\n");
|
printf("\n");
|
||||||
++indent;
|
|
||||||
layout_node.for_each_child([](auto& child) {
|
|
||||||
dump_tree(child);
|
|
||||||
});
|
|
||||||
--indent;
|
|
||||||
}
|
|
||||||
|
|
||||||
void dump_tree(const StyledNode& styled_node)
|
layout_node.style_properties().for_each_property([&](auto& key, auto& value) {
|
||||||
{
|
|
||||||
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) {
|
|
||||||
for (int i = 0; i < indent; ++i)
|
for (int i = 0; i < indent; ++i)
|
||||||
printf(" ");
|
printf(" ");
|
||||||
printf(" (%s: %s)\n", key.characters(), value.to_string().characters());
|
printf(" (%s: %s)\n", key.characters(), value.to_string().characters());
|
||||||
});
|
});
|
||||||
|
|
||||||
++indent;
|
++indent;
|
||||||
styled_node.for_each_child([](auto& child) {
|
layout_node.for_each_child([](auto& child) {
|
||||||
dump_tree(child);
|
dump_tree(child);
|
||||||
});
|
});
|
||||||
--indent;
|
--indent;
|
||||||
|
|
|
@ -4,10 +4,8 @@ class Node;
|
||||||
class LayoutNode;
|
class LayoutNode;
|
||||||
class StyleRule;
|
class StyleRule;
|
||||||
class StyleSheet;
|
class StyleSheet;
|
||||||
class StyledNode;
|
|
||||||
|
|
||||||
void dump_tree(const Node&);
|
void dump_tree(const Node&);
|
||||||
void dump_tree(const StyledNode&);
|
|
||||||
void dump_tree(const LayoutNode&);
|
void dump_tree(const LayoutNode&);
|
||||||
void dump_sheet(const StyleSheet&);
|
void dump_sheet(const StyleSheet&);
|
||||||
void dump_rule(const StyleRule&);
|
void dump_rule(const StyleRule&);
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
#include <AK/Function.h>
|
#include <AK/Function.h>
|
||||||
#include <LibHTML/CSS/StyleResolver.h>
|
#include <LibHTML/CSS/StyleResolver.h>
|
||||||
#include <LibHTML/CSS/StyledNode.h>
|
|
||||||
#include <LibHTML/DOM/Element.h>
|
#include <LibHTML/DOM/Element.h>
|
||||||
#include <LibHTML/Dump.h>
|
#include <LibHTML/Dump.h>
|
||||||
#include <LibHTML/Frame.h>
|
#include <LibHTML/Frame.h>
|
||||||
|
@ -23,71 +22,46 @@ void Frame::set_document(Document* document)
|
||||||
m_document = document;
|
m_document = document;
|
||||||
}
|
}
|
||||||
|
|
||||||
RefPtr<StyledNode> Frame::generate_style_tree()
|
RefPtr<LayoutNode> Frame::generate_layout_tree()
|
||||||
{
|
{
|
||||||
if (!m_document)
|
auto resolver = m_document->style_resolver();
|
||||||
return nullptr;
|
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();
|
auto style_properties = resolver.resolve_style(static_cast<const Element&>(node));
|
||||||
Function<RefPtr<StyledNode>(const Node&, StyledNode*)> resolve_style = [&](const Node& node, StyledNode* parent_styled_node) -> RefPtr<StyledNode> {
|
auto display_property = style_properties.property("display");
|
||||||
RefPtr<StyledNode> styled_node;
|
String display = display_property.has_value() ? display_property.release_value()->to_string() : "inline";
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
RefPtr<LayoutNode> Frame::generate_layout_tree(const StyledNode& styled_root)
|
if (display == "none")
|
||||||
{
|
|
||||||
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:
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
case Display::Block:
|
if (display == "block")
|
||||||
return adopt(*new LayoutBlock(styled_node.node(), &styled_node));
|
return adopt(*new LayoutBlock(&node, move(style_properties)));
|
||||||
case Display::Inline:
|
if (display == "inline")
|
||||||
return adopt(*new LayoutInline(*styled_node.node(), styled_node));
|
return adopt(*new LayoutInline(node, move(style_properties)));
|
||||||
default:
|
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
Function<RefPtr<LayoutNode>(const StyledNode&)> build_layout_tree;
|
Function<RefPtr<LayoutNode>(const Node&)> build_layout_tree;
|
||||||
build_layout_tree = [&](const StyledNode& styled_node) -> RefPtr<LayoutNode> {
|
build_layout_tree = [&](const Node& node) -> RefPtr<LayoutNode> {
|
||||||
auto layout_node = create_layout_node(styled_node);
|
auto layout_node = create_layout_node(node);
|
||||||
if (!layout_node)
|
if (!layout_node)
|
||||||
return nullptr;
|
return nullptr;
|
||||||
if (!styled_node.has_children())
|
if (!node.has_children())
|
||||||
return layout_node;
|
return layout_node;
|
||||||
for (auto* styled_child = styled_node.first_child(); styled_child; styled_child = styled_child->next_sibling()) {
|
static_cast<const ParentNode&>(node).for_each_child([&](const Node& child) {
|
||||||
auto layout_child = build_layout_tree(*styled_child);
|
auto layout_child = build_layout_tree(child);
|
||||||
if (!layout_child)
|
if (!layout_child)
|
||||||
continue;
|
return;
|
||||||
if (layout_child->is_inline())
|
if (layout_child->is_inline())
|
||||||
layout_node->inline_wrapper().append_child(*layout_child);
|
layout_node->inline_wrapper().append_child(*layout_child);
|
||||||
else
|
else
|
||||||
layout_node->append_child(*layout_child);
|
layout_node->append_child(*layout_child);
|
||||||
}
|
});
|
||||||
return layout_node;
|
return layout_node;
|
||||||
};
|
};
|
||||||
return build_layout_tree(styled_root);
|
return build_layout_tree(*m_document);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Frame::layout()
|
void Frame::layout()
|
||||||
|
@ -95,8 +69,7 @@ void Frame::layout()
|
||||||
if (!m_document)
|
if (!m_document)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
auto styled_root = generate_style_tree();
|
auto layout_root = generate_layout_tree();
|
||||||
auto layout_root = generate_layout_tree(*styled_root);
|
|
||||||
|
|
||||||
layout_root->style().size().set_width(m_size.width());
|
layout_root->style().size().set_width(m_size.width());
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <LibHTML/DOM/Document.h>
|
|
||||||
#include <LibDraw/Size.h>
|
#include <LibDraw/Size.h>
|
||||||
|
#include <LibHTML/DOM/Document.h>
|
||||||
|
|
||||||
class Frame {
|
class Frame {
|
||||||
public:
|
public:
|
||||||
|
@ -16,8 +16,7 @@ public:
|
||||||
void layout();
|
void layout();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
RefPtr<StyledNode> generate_style_tree();
|
RefPtr<LayoutNode> generate_layout_tree();
|
||||||
RefPtr<LayoutNode> generate_layout_tree(const StyledNode&);
|
|
||||||
|
|
||||||
RefPtr<Document> m_document;
|
RefPtr<Document> m_document;
|
||||||
Size m_size;
|
Size m_size;
|
||||||
|
|
|
@ -1,9 +1,8 @@
|
||||||
#include <LibHTML/CSS/StyledNode.h>
|
|
||||||
#include <LibHTML/DOM/Element.h>
|
#include <LibHTML/DOM/Element.h>
|
||||||
#include <LibHTML/Layout/LayoutBlock.h>
|
#include <LibHTML/Layout/LayoutBlock.h>
|
||||||
|
|
||||||
LayoutBlock::LayoutBlock(const Node* node, const StyledNode* styled_node)
|
LayoutBlock::LayoutBlock(const Node* node, StyleProperties&& style_properties)
|
||||||
: LayoutNode(node, styled_node)
|
: LayoutNode(node, move(style_properties))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -14,7 +13,7 @@ LayoutBlock::~LayoutBlock()
|
||||||
LayoutNode& LayoutBlock::inline_wrapper()
|
LayoutNode& LayoutBlock::inline_wrapper()
|
||||||
{
|
{
|
||||||
if (!last_child() || !last_child()->is_block()) {
|
if (!last_child() || !last_child()->is_block()) {
|
||||||
append_child(adopt(*new LayoutBlock(nullptr, nullptr)));
|
append_child(adopt(*new LayoutBlock(nullptr, {})));
|
||||||
}
|
}
|
||||||
return *last_child();
|
return *last_child();
|
||||||
}
|
}
|
||||||
|
@ -42,21 +41,17 @@ void LayoutBlock::layout()
|
||||||
|
|
||||||
void LayoutBlock::compute_width()
|
void LayoutBlock::compute_width()
|
||||||
{
|
{
|
||||||
if (!styled_node()) {
|
auto& style_properties = this->style_properties();
|
||||||
// I guess the size is "auto" in this case.
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto& styled_node = *this->styled_node();
|
|
||||||
auto auto_value = Length();
|
auto auto_value = Length();
|
||||||
auto zero_value = Length(0, Length::Type::Absolute);
|
auto zero_value = Length(0, Length::Type::Absolute);
|
||||||
auto width = styled_node.length_or_fallback("width", auto_value);
|
auto width = style_properties.length_or_fallback("width", auto_value);
|
||||||
auto margin_left = styled_node.length_or_fallback("margin-left", zero_value);
|
auto margin_left = style_properties.length_or_fallback("margin-left", zero_value);
|
||||||
auto margin_right = styled_node.length_or_fallback("margin-right", zero_value);
|
auto margin_right = style_properties.length_or_fallback("margin-right", zero_value);
|
||||||
auto border_left = styled_node.length_or_fallback("border-left", zero_value);
|
auto border_left = style_properties.length_or_fallback("border-left", zero_value);
|
||||||
auto border_right = styled_node.length_or_fallback("border-right", zero_value);
|
auto border_right = style_properties.length_or_fallback("border-right", zero_value);
|
||||||
auto padding_left = styled_node.length_or_fallback("padding-left", zero_value);
|
auto padding_left = style_properties.length_or_fallback("padding-left", zero_value);
|
||||||
auto padding_right = styled_node.length_or_fallback("padding-right", zero_value);
|
auto padding_right = style_properties.length_or_fallback("padding-right", zero_value);
|
||||||
|
|
||||||
dbg() << " Left: " << margin_left << "+" << border_left << "+" << padding_left;
|
dbg() << " Left: " << margin_left << "+" << border_left << "+" << padding_left;
|
||||||
dbg() << "Right: " << margin_right << "+" << border_right << "+" << padding_right;
|
dbg() << "Right: " << margin_right << "+" << border_right << "+" << padding_right;
|
||||||
|
@ -116,32 +111,27 @@ void LayoutBlock::compute_width()
|
||||||
|
|
||||||
void LayoutBlock::compute_position()
|
void LayoutBlock::compute_position()
|
||||||
{
|
{
|
||||||
if (!styled_node()) {
|
auto& style_properties = this->style_properties();
|
||||||
// I guess the size is "auto" in this case.
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto& styled_node = *this->styled_node();
|
|
||||||
auto auto_value = Length();
|
auto auto_value = Length();
|
||||||
auto zero_value = Length(0, Length::Type::Absolute);
|
auto zero_value = Length(0, Length::Type::Absolute);
|
||||||
|
|
||||||
auto width = styled_node.length_or_fallback("width", auto_value);
|
auto width = style_properties.length_or_fallback("width", auto_value);
|
||||||
style().margin().top = styled_node.length_or_fallback("margin-top", zero_value);
|
style().margin().top = style_properties.length_or_fallback("margin-top", zero_value);
|
||||||
style().margin().bottom = styled_node.length_or_fallback("margin-bottom", zero_value);
|
style().margin().bottom = style_properties.length_or_fallback("margin-bottom", zero_value);
|
||||||
style().border().top = styled_node.length_or_fallback("border-top", zero_value);
|
style().border().top = style_properties.length_or_fallback("border-top", zero_value);
|
||||||
style().border().bottom = styled_node.length_or_fallback("border-bottom", zero_value);
|
style().border().bottom = style_properties.length_or_fallback("border-bottom", zero_value);
|
||||||
style().padding().top = styled_node.length_or_fallback("padding-top", zero_value);
|
style().padding().top = style_properties.length_or_fallback("padding-top", zero_value);
|
||||||
style().padding().bottom = styled_node.length_or_fallback("padding-bottom", 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_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());
|
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()
|
void LayoutBlock::compute_height()
|
||||||
{
|
{
|
||||||
if (!styled_node())
|
auto& style_properties = this->style_properties();
|
||||||
return;
|
|
||||||
auto& styled_node = *this->styled_node();
|
auto height_property = style_properties.property("height");
|
||||||
auto height_property = styled_node.property("height");
|
|
||||||
if (!height_property.has_value())
|
if (!height_property.has_value())
|
||||||
return;
|
return;
|
||||||
auto height_length = height_property.value()->to_length();
|
auto height_length = height_property.value()->to_length();
|
||||||
|
|
|
@ -6,7 +6,7 @@ class Element;
|
||||||
|
|
||||||
class LayoutBlock : public LayoutNode {
|
class LayoutBlock : public LayoutNode {
|
||||||
public:
|
public:
|
||||||
LayoutBlock(const Node*, const StyledNode*);
|
LayoutBlock(const Node*, StyleProperties&&);
|
||||||
virtual ~LayoutBlock() override;
|
virtual ~LayoutBlock() override;
|
||||||
|
|
||||||
virtual const char* class_name() const override { return "LayoutBlock"; }
|
virtual const char* class_name() const override { return "LayoutBlock"; }
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#include <LibHTML/Layout/LayoutDocument.h>
|
#include <LibHTML/Layout/LayoutDocument.h>
|
||||||
|
|
||||||
LayoutDocument::LayoutDocument(const Document& document, const StyledNode& styled_node)
|
LayoutDocument::LayoutDocument(const Document& document, StyleProperties&& style_properties)
|
||||||
: LayoutBlock(&document, &styled_node)
|
: LayoutBlock(&document, move(style_properties))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,15 +1,16 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <LibHTML/Layout/LayoutBlock.h>
|
|
||||||
#include <LibHTML/DOM/Document.h>
|
#include <LibHTML/DOM/Document.h>
|
||||||
|
#include <LibHTML/Layout/LayoutBlock.h>
|
||||||
|
|
||||||
class LayoutDocument final : public LayoutBlock {
|
class LayoutDocument final : public LayoutBlock {
|
||||||
public:
|
public:
|
||||||
LayoutDocument(const Document&, const StyledNode&);
|
LayoutDocument(const Document&, StyleProperties&&);
|
||||||
virtual ~LayoutDocument() override;
|
virtual ~LayoutDocument() override;
|
||||||
|
|
||||||
const Document& node() const { return static_cast<const Document&>(*LayoutNode::node()); }
|
const Document& node() const { return static_cast<const Document&>(*LayoutNode::node()); }
|
||||||
virtual const char* class_name() const override { return "LayoutDocument"; }
|
virtual const char* class_name() const override { return "LayoutDocument"; }
|
||||||
virtual void layout() override;
|
virtual void layout() override;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
#include <LibHTML/DOM/Element.h>
|
#include <LibHTML/DOM/Element.h>
|
||||||
#include <LibHTML/Layout/LayoutInline.h>
|
#include <LibHTML/Layout/LayoutInline.h>
|
||||||
|
|
||||||
LayoutInline::LayoutInline(const Node& node, const StyledNode& styled_node)
|
LayoutInline::LayoutInline(const Node& node, StyleProperties&& style_properties)
|
||||||
: LayoutNode(&node, &styled_node)
|
: LayoutNode(&node, move(style_properties))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -6,7 +6,7 @@ class Element;
|
||||||
|
|
||||||
class LayoutInline : public LayoutNode {
|
class LayoutInline : public LayoutNode {
|
||||||
public:
|
public:
|
||||||
LayoutInline(const Node&, const StyledNode&);
|
LayoutInline(const Node&, StyleProperties&&);
|
||||||
virtual ~LayoutInline() override;
|
virtual ~LayoutInline() override;
|
||||||
|
|
||||||
virtual const char* class_name() const override { return "LayoutInline"; }
|
virtual const char* class_name() const override { return "LayoutInline"; }
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
#include <LibHTML/Layout/LayoutBlock.h>
|
#include <LibHTML/Layout/LayoutBlock.h>
|
||||||
#include <LibHTML/Layout/LayoutNode.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_node(node)
|
||||||
, m_styled_node(styled_node)
|
, m_style_properties(style_properties)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,13 +2,13 @@
|
||||||
|
|
||||||
#include <AK/NonnullRefPtr.h>
|
#include <AK/NonnullRefPtr.h>
|
||||||
#include <AK/Vector.h>
|
#include <AK/Vector.h>
|
||||||
|
#include <LibDraw/Rect.h>
|
||||||
|
#include <LibHTML/CSS/StyleProperties.h>
|
||||||
#include <LibHTML/Layout/ComputedStyle.h>
|
#include <LibHTML/Layout/ComputedStyle.h>
|
||||||
#include <LibHTML/TreeNode.h>
|
#include <LibHTML/TreeNode.h>
|
||||||
#include <LibDraw/Rect.h>
|
|
||||||
|
|
||||||
class Node;
|
class Node;
|
||||||
class LayoutBlock;
|
class LayoutBlock;
|
||||||
class StyledNode;
|
|
||||||
|
|
||||||
class LayoutNode : public TreeNode<LayoutNode> {
|
class LayoutNode : public TreeNode<LayoutNode> {
|
||||||
public:
|
public:
|
||||||
|
@ -49,16 +49,15 @@ public:
|
||||||
|
|
||||||
virtual LayoutNode& inline_wrapper() { return *this; }
|
virtual LayoutNode& inline_wrapper() { return *this; }
|
||||||
|
|
||||||
StyledNode* styled_node() { return m_styled_node; }
|
const StyleProperties& style_properties() const { return m_style_properties; }
|
||||||
const StyledNode* styled_node() const { return m_styled_node; }
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit LayoutNode(const Node*, const StyledNode*);
|
explicit LayoutNode(const Node*, StyleProperties&&);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const Node* m_node { nullptr };
|
const Node* m_node { nullptr };
|
||||||
RefPtr<StyledNode> m_styled_node;
|
|
||||||
|
|
||||||
|
StyleProperties m_style_properties;
|
||||||
ComputedStyle m_style;
|
ComputedStyle m_style;
|
||||||
Rect m_rect;
|
Rect m_rect;
|
||||||
};
|
};
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
#include <LibHTML/Layout/LayoutText.h>
|
#include <LibHTML/Layout/LayoutText.h>
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
LayoutText::LayoutText(const Text& text, const StyledNode& styled_node)
|
LayoutText::LayoutText(const Text& text, StyleProperties&& style_properties)
|
||||||
: LayoutNode(&text, &styled_node)
|
: LayoutNode(&text, move(style_properties))
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -29,7 +29,6 @@ const String& LayoutText::text() const
|
||||||
|
|
||||||
void LayoutText::compute_runs()
|
void LayoutText::compute_runs()
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void LayoutText::layout()
|
void LayoutText::layout()
|
||||||
|
|
|
@ -1,11 +1,11 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <LibHTML/Layout/LayoutNode.h>
|
|
||||||
#include <LibHTML/DOM/Text.h>
|
#include <LibHTML/DOM/Text.h>
|
||||||
|
#include <LibHTML/Layout/LayoutNode.h>
|
||||||
|
|
||||||
class LayoutText : public LayoutNode {
|
class LayoutText : public LayoutNode {
|
||||||
public:
|
public:
|
||||||
LayoutText(const Text&, const StyledNode&);
|
LayoutText(const Text&, StyleProperties&&);
|
||||||
virtual ~LayoutText() override;
|
virtual ~LayoutText() override;
|
||||||
|
|
||||||
const Text& node() const { return static_cast<const Text&>(*LayoutNode::node()); }
|
const Text& node() const { return static_cast<const Text&>(*LayoutNode::node()); }
|
||||||
|
|
|
@ -9,7 +9,7 @@ LIBHTML_OBJS = \
|
||||||
CSS/StyleRule.o \
|
CSS/StyleRule.o \
|
||||||
CSS/StyleDeclaration.o \
|
CSS/StyleDeclaration.o \
|
||||||
CSS/StyleValue.o \
|
CSS/StyleValue.o \
|
||||||
CSS/StyledNode.o \
|
CSS/StyleProperties.o \
|
||||||
CSS/StyleResolver.o \
|
CSS/StyleResolver.o \
|
||||||
CSS/DefaultStyleSheetSource.o \
|
CSS/DefaultStyleSheetSource.o \
|
||||||
Parser/HTMLParser.o \
|
Parser/HTMLParser.o \
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
#include <LibCore/CFile.h>
|
#include <LibCore/CFile.h>
|
||||||
#include <LibHTML/CSS/StyleResolver.h>
|
#include <LibHTML/CSS/StyleResolver.h>
|
||||||
#include <LibHTML/CSS/StyledNode.h>
|
|
||||||
#include <LibHTML/DOM/Element.h>
|
#include <LibHTML/DOM/Element.h>
|
||||||
#include <LibHTML/Dump.h>
|
#include <LibHTML/Dump.h>
|
||||||
#include <LibHTML/Frame.h>
|
#include <LibHTML/Frame.h>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue