1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:57:44 +00:00

LibHTML: LayoutText should always use parent's style properties

This patch makes StyleProperties heap-allocated and ref-counted so that
a LayoutNode can be without one. The ref-counting also allows anonymous
blocks to share style with their parent block.

LayoutText never needs a StyleProperties, since text always inherits
style from its parent element. This is handled by style_properties().
This commit is contained in:
Andreas Kling 2019-10-04 12:12:39 +02:00
parent 79d8b9ae75
commit 4e35bbffdd
14 changed files with 45 additions and 39 deletions

View file

@ -3,7 +3,7 @@
#include <LibHTML/Layout/LayoutBlock.h>
#include <LibHTML/Layout/LayoutInline.h>
LayoutBlock::LayoutBlock(const Node* node, StyleProperties&& style_properties)
LayoutBlock::LayoutBlock(const Node* node, NonnullRefPtr<StyleProperties> style_properties)
: LayoutNode(node, move(style_properties))
{
}
@ -15,7 +15,7 @@ LayoutBlock::~LayoutBlock()
LayoutNode& LayoutBlock::inline_wrapper()
{
if (!last_child() || !last_child()->is_block() || last_child()->node() != nullptr) {
append_child(adopt(*new LayoutBlock(nullptr, {})));
append_child(adopt(*new LayoutBlock(nullptr, style_properties())));
}
return *last_child();
}

View file

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

View file

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

View file

@ -5,7 +5,7 @@
class LayoutDocument final : public LayoutBlock {
public:
LayoutDocument(const Document&, StyleProperties&&);
explicit LayoutDocument(const Document&, NonnullRefPtr<StyleProperties>);
virtual ~LayoutDocument() override;
const Document& node() const { return static_cast<const Document&>(*LayoutNode::node()); }

View file

@ -1,7 +1,7 @@
#include <LibHTML/Layout/LayoutBlock.h>
#include <LibHTML/Layout/LayoutInline.h>
LayoutInline::LayoutInline(const Node& node, StyleProperties&& style_properties)
LayoutInline::LayoutInline(const Node& node, RefPtr<StyleProperties> style_properties)
: LayoutNode(&node, move(style_properties))
{
}

View file

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

View file

@ -7,9 +7,9 @@
//#define DRAW_BOXES_AROUND_LAYOUT_NODES
//#define DRAW_BOXES_AROUND_HOVERED_NODES
LayoutNode::LayoutNode(const Node* node, StyleProperties&& style_properties)
LayoutNode::LayoutNode(const Node* node, RefPtr<StyleProperties> style_properties)
: m_node(node)
, m_style_properties(style_properties)
, m_style_properties(move(style_properties))
{
}

View file

@ -62,18 +62,23 @@ public:
virtual LayoutNode& inline_wrapper() { return *this; }
const StyleProperties& style_properties() const { return m_style_properties; }
const StyleProperties& style_properties() const
{
if (m_style_properties)
return *m_style_properties;
return parent()->style_properties();
}
void inserted_into(LayoutNode&) {}
void removed_from(LayoutNode&) {}
protected:
explicit LayoutNode(const Node*, StyleProperties&&);
explicit LayoutNode(const Node*, RefPtr<StyleProperties>);
private:
const Node* m_node { nullptr };
StyleProperties m_style_properties;
RefPtr<StyleProperties> m_style_properties;
ComputedStyle m_style;
Rect m_rect;
};

View file

@ -7,8 +7,8 @@
#include <LibHTML/Layout/LayoutText.h>
#include <ctype.h>
LayoutText::LayoutText(const Text& text, StyleProperties&& style_properties)
: LayoutInline(text, move(style_properties))
LayoutText::LayoutText(const Text& text)
: LayoutInline(text, {})
{
}
@ -78,12 +78,13 @@ static bool is_all_whitespace(const String& string)
return true;
}
const String& LayoutText::text() const
const String& LayoutText::text_for_style(const StyleProperties& style_properties) const
{
static String one_space = " ";
if (is_all_whitespace(node().data()))
if (style_properties().string_or_fallback("white-space", "normal") == "normal")
if (is_all_whitespace(node().data())) {
if (style_properties.string_or_fallback("white-space", "normal") == "normal")
return one_space;
}
return node().data();
}

View file

@ -8,12 +8,12 @@ class LineBoxFragment;
class LayoutText : public LayoutInline {
public:
LayoutText(const Text&, StyleProperties&&);
explicit LayoutText(const Text&);
virtual ~LayoutText() override;
const Text& node() const { return static_cast<const Text&>(*LayoutNode::node()); }
const String& text() const;
const String& text_for_style(const StyleProperties&) const;
virtual const char* class_name() const override { return "LayoutText"; }
virtual bool is_text() const final { return true; }
@ -22,6 +22,8 @@ public:
virtual void split_into_lines(LayoutBlock& container) override;
const StyleProperties& style_properties() const { return parent()->style_properties(); }
private:
template<typename Callback>
void for_each_word(Callback) const;
@ -29,7 +31,6 @@ private:
void for_each_source_line(Callback) const;
void load_font();
void compute_runs();
RefPtr<Font> m_font;
};