From 5a6c36dc917decc58ec5fbce7d094b82a4af4fc1 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sun, 6 Oct 2019 19:54:50 +0200 Subject: [PATCH] LibHTML: Add Node::{next,previous}_element_sibling() These helpers return the next/previous sibling Node that's actually an element. This will be useful in the CSS engine since CSS doesn't care about text nodes. --- Libraries/LibHTML/DOM/Node.cpp | 18 ++++++++++++++++++ Libraries/LibHTML/DOM/Node.h | 4 ++++ 2 files changed, 22 insertions(+) diff --git a/Libraries/LibHTML/DOM/Node.cpp b/Libraries/LibHTML/DOM/Node.cpp index 700a26bcd7..ca9f874780 100644 --- a/Libraries/LibHTML/DOM/Node.cpp +++ b/Libraries/LibHTML/DOM/Node.cpp @@ -83,3 +83,21 @@ String Node::text_content() const builder.trim(1); return builder.to_string(); } + +const Element* Node::next_element_sibling() const +{ + for (auto* node = next_sibling(); node; node = node->next_sibling()) { + if (node->is_element()) + return static_cast(node); + } + return nullptr; +} + +const Element* Node::previous_element_sibling() const +{ + for (auto* node = previous_sibling(); node; node = node->previous_sibling()) { + if (node->is_element()) + return static_cast(node); + } + return nullptr; +} diff --git a/Libraries/LibHTML/DOM/Node.h b/Libraries/LibHTML/DOM/Node.h index e43aa3b2f0..e9771eeb99 100644 --- a/Libraries/LibHTML/DOM/Node.h +++ b/Libraries/LibHTML/DOM/Node.h @@ -14,6 +14,7 @@ enum class NodeType : unsigned { }; class Document; +class Element; class HTMLElement; class HTMLAnchorElement; class ParentNode; @@ -63,6 +64,9 @@ public: void set_layout_node(Badge, LayoutNode* layout_node) const { m_layout_node = layout_node; } + const Element* previous_element_sibling() const; + const Element* next_element_sibling() const; + protected: Node(Document&, NodeType);