diff --git a/Libraries/LibHTML/Layout/LayoutBlock.cpp b/Libraries/LibHTML/Layout/LayoutBlock.cpp index ecf117b2c2..a4db9dbdf3 100644 --- a/Libraries/LibHTML/Layout/LayoutBlock.cpp +++ b/Libraries/LibHTML/Layout/LayoutBlock.cpp @@ -5,7 +5,7 @@ #include LayoutBlock::LayoutBlock(const Node* node, NonnullRefPtr style) - : LayoutNode(node, move(style)) + : LayoutNodeWithStyle(node, move(style)) { } diff --git a/Libraries/LibHTML/Layout/LayoutBlock.h b/Libraries/LibHTML/Layout/LayoutBlock.h index 58a6f87055..01198bdb7c 100644 --- a/Libraries/LibHTML/Layout/LayoutBlock.h +++ b/Libraries/LibHTML/Layout/LayoutBlock.h @@ -5,7 +5,7 @@ class Element; -class LayoutBlock : public LayoutNode { +class LayoutBlock : public LayoutNodeWithStyle { public: LayoutBlock(const Node*, NonnullRefPtr); virtual ~LayoutBlock() override; diff --git a/Libraries/LibHTML/Layout/LayoutInline.cpp b/Libraries/LibHTML/Layout/LayoutInline.cpp index 0b0d1db62b..5265d9a357 100644 --- a/Libraries/LibHTML/Layout/LayoutInline.cpp +++ b/Libraries/LibHTML/Layout/LayoutInline.cpp @@ -3,7 +3,7 @@ #include LayoutInline::LayoutInline(const Element& element, NonnullRefPtr style) - : LayoutNode(&element, move(style)) + : LayoutNodeWithStyle(&element, move(style)) { set_inline(true); } diff --git a/Libraries/LibHTML/Layout/LayoutInline.h b/Libraries/LibHTML/Layout/LayoutInline.h index 7103afb72e..8a12d5e3fa 100644 --- a/Libraries/LibHTML/Layout/LayoutInline.h +++ b/Libraries/LibHTML/Layout/LayoutInline.h @@ -4,7 +4,7 @@ class LayoutBlock; -class LayoutInline : public LayoutNode { +class LayoutInline : public LayoutNodeWithStyle { public: LayoutInline(const Element&, NonnullRefPtr); virtual ~LayoutInline() override; diff --git a/Libraries/LibHTML/Layout/LayoutNode.cpp b/Libraries/LibHTML/Layout/LayoutNode.cpp index e8e678457f..3be9e7aca2 100644 --- a/Libraries/LibHTML/Layout/LayoutNode.cpp +++ b/Libraries/LibHTML/Layout/LayoutNode.cpp @@ -7,9 +7,8 @@ //#define DRAW_BOXES_AROUND_LAYOUT_NODES //#define DRAW_BOXES_AROUND_HOVERED_NODES -LayoutNode::LayoutNode(const Node* node, RefPtr style) +LayoutNode::LayoutNode(const Node* node) : m_node(node) - , m_style(move(style)) { if (m_node) m_node->set_layout_node({}, this); diff --git a/Libraries/LibHTML/Layout/LayoutNode.h b/Libraries/LibHTML/Layout/LayoutNode.h index 9c56408b33..c75798b800 100644 --- a/Libraries/LibHTML/Layout/LayoutNode.h +++ b/Libraries/LibHTML/Layout/LayoutNode.h @@ -12,6 +12,7 @@ class Document; class Element; class LayoutBlock; class LayoutNode; +class LayoutNodeWithStyle; class LineBoxFragment; class Node; @@ -66,12 +67,9 @@ public: virtual LayoutNode& inline_wrapper() { return *this; } - const StyleProperties& style() const - { - if (m_style) - return *m_style; - return parent()->style(); - } + const StyleProperties& style() const; + + const LayoutNodeWithStyle* parent() const; void inserted_into(LayoutNode&) {} void removed_from(LayoutNode&) {} @@ -79,13 +77,45 @@ public: virtual void split_into_lines(LayoutBlock& container); protected: - explicit LayoutNode(const Node*, RefPtr); + explicit LayoutNode(const Node*); private: + friend class LayoutNodeWithStyle; + const Node* m_node { nullptr }; - RefPtr m_style; BoxModelMetrics m_box_metrics; Rect m_rect; bool m_inline { false }; + bool m_has_style { false }; }; + +class LayoutNodeWithStyle : public LayoutNode { +public: + virtual ~LayoutNodeWithStyle() override {} + + const StyleProperties& style() const { return m_style; } + +protected: + explicit LayoutNodeWithStyle(const Node* node, NonnullRefPtr style) + : LayoutNode(node) + , m_style(move(style)) + { + m_has_style = true; + } + +private: + NonnullRefPtr m_style; +}; + +inline const StyleProperties& LayoutNode::style() const +{ + if (m_has_style) + return static_cast(this)->style(); + return parent()->style(); +} + +inline const LayoutNodeWithStyle* LayoutNode::parent() const +{ + return static_cast(TreeNode::parent()); +} diff --git a/Libraries/LibHTML/Layout/LayoutReplaced.cpp b/Libraries/LibHTML/Layout/LayoutReplaced.cpp index 6d0f8695db..fabfdd2067 100644 --- a/Libraries/LibHTML/Layout/LayoutReplaced.cpp +++ b/Libraries/LibHTML/Layout/LayoutReplaced.cpp @@ -3,7 +3,7 @@ #include LayoutReplaced::LayoutReplaced(const Element& element, NonnullRefPtr style) - : LayoutNode(&element, move(style)) + : LayoutNodeWithStyle(&element, move(style)) { // FIXME: Allow non-inline replaced elements. set_inline(true); diff --git a/Libraries/LibHTML/Layout/LayoutReplaced.h b/Libraries/LibHTML/Layout/LayoutReplaced.h index c76fc3b930..719da0b465 100644 --- a/Libraries/LibHTML/Layout/LayoutReplaced.h +++ b/Libraries/LibHTML/Layout/LayoutReplaced.h @@ -1,7 +1,7 @@ #include #include -class LayoutReplaced : public LayoutNode { +class LayoutReplaced : public LayoutNodeWithStyle { public: LayoutReplaced(const Element&, NonnullRefPtr); virtual ~LayoutReplaced() override; diff --git a/Libraries/LibHTML/Layout/LayoutText.cpp b/Libraries/LibHTML/Layout/LayoutText.cpp index fd64c5b64c..a75b7f11e2 100644 --- a/Libraries/LibHTML/Layout/LayoutText.cpp +++ b/Libraries/LibHTML/Layout/LayoutText.cpp @@ -8,7 +8,7 @@ #include LayoutText::LayoutText(const Text& text) - : LayoutNode(&text, {}) + : LayoutNode(&text) { set_inline(true); }