From 2305dee455874ee08c6e77df83132a3bd314c858 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Fri, 18 Oct 2019 10:16:33 +0200 Subject: [PATCH] LibHTML: Add LayoutNode::first_ancestor_of_type() --- Libraries/LibHTML/Layout/LayoutNode.h | 32 +++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/Libraries/LibHTML/Layout/LayoutNode.h b/Libraries/LibHTML/Layout/LayoutNode.h index e515de53d2..0fc8f38ca7 100644 --- a/Libraries/LibHTML/Layout/LayoutNode.h +++ b/Libraries/LibHTML/Layout/LayoutNode.h @@ -67,6 +67,7 @@ public: const StyleProperties& style() const; + LayoutNodeWithStyle* parent(); const LayoutNodeWithStyle* parent() const; void inserted_into(LayoutNode&) {} @@ -97,6 +98,12 @@ public: template T* first_child_of_type(); + template + const T* first_ancestor_of_type() const; + + template + T* first_ancestor_of_type(); + protected: explicit LayoutNode(const Node*); @@ -157,6 +164,11 @@ inline const LayoutNodeWithStyle* LayoutNode::parent() const return static_cast(TreeNode::parent()); } +inline LayoutNodeWithStyle* LayoutNode::parent() +{ + return static_cast(TreeNode::parent()); +} + template inline bool is(const LayoutNode&) { @@ -248,3 +260,23 @@ inline T* LayoutNode::first_child_of_type() } return nullptr; } + +template +inline const T* LayoutNode::first_ancestor_of_type() const +{ + for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) { + if (is(*ancestor)) + return &to(*ancestor); + } + return nullptr; +} + +template +inline T* LayoutNode::first_ancestor_of_type() +{ + for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) { + if (is(*ancestor)) + return &to(*ancestor); + } + return nullptr; +}