mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 03:47:34 +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
|
@ -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();
|
||||
|
|
|
@ -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"; }
|
||||
|
|
|
@ -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))
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -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:
|
||||
};
|
||||
|
|
|
@ -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))
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -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"; }
|
||||
|
|
|
@ -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)
|
||||
{
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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()
|
||||
|
|
|
@ -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()); }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue