summaryrefslogtreecommitdiff
path: root/Libraries/LibHTML/DOM
diff options
context:
space:
mode:
authorAndreas Kling <awesomekling@gmail.com>2019-10-06 19:54:50 +0200
committerAndreas Kling <awesomekling@gmail.com>2019-10-06 19:54:50 +0200
commit5a6c36dc917decc58ec5fbce7d094b82a4af4fc1 (patch)
treeacdec29f295415d26143d17dfa5e8232f0869616 /Libraries/LibHTML/DOM
parentcad326f3230620512e7b8a5e7c65af4827421092 (diff)
downloadserenity-5a6c36dc917decc58ec5fbce7d094b82a4af4fc1.zip
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.
Diffstat (limited to 'Libraries/LibHTML/DOM')
-rw-r--r--Libraries/LibHTML/DOM/Node.cpp18
-rw-r--r--Libraries/LibHTML/DOM/Node.h4
2 files changed, 22 insertions, 0 deletions
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<const Element*>(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<const Element*>(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>, 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);