diff options
author | Tobias Christiansen <tobi@tobyase.de> | 2021-04-17 23:05:56 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-04-20 18:29:19 +0200 |
commit | c09ac536c5c7630888eee4376a33492a93bae541 (patch) | |
tree | a5d016b041bc25a6bbedfb7b51a3b372bde65d1b /Userland/Libraries/LibWeb | |
parent | e92bffb2e3f304485bf086b6cc0206c60fa4a374 (diff) | |
download | serenity-c09ac536c5c7630888eee4376a33492a93bae541.zip |
LibWeb: Add capabilities to find the index of a child in its parent.
For Elements depending on the index they are inside their parent. Most
notably the <ol> element.
Also added a typed version to only count children of a certain type.
This patch is work towards #2059
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r-- | Userland/Libraries/LibWeb/TreeNode.h | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/TreeNode.h b/Userland/Libraries/LibWeb/TreeNode.h index 77b4139db4..ec6ea07d70 100644 --- a/Userland/Libraries/LibWeb/TreeNode.h +++ b/Userland/Libraries/LibWeb/TreeNode.h @@ -97,6 +97,39 @@ public: return const_cast<TreeNode*>(this)->child_at_index(index); } + Optional<size_t> index_of_child(const T& search_child) + { + VERIFY(search_child.parent() == this); + size_t index = 0; + auto* child = first_child(); + VERIFY(child); + + do { + if (child == &search_child) + return index; + index++; + } while (child && (child = child->next_sibling())); + return {}; + } + + template<typename ChildType> + Optional<size_t> index_of_child(const T& search_child) + { + VERIFY(search_child.parent() == this); + size_t index = 0; + auto* child = first_child(); + VERIFY(child); + + do { + if (!is<ChildType>(child)) + continue; + if (child == &search_child) + return index; + index++; + } while (child && (child = child->next_sibling())); + return {}; + } + bool is_ancestor_of(const TreeNode&) const; bool is_inclusive_ancestor_of(const TreeNode&) const; bool is_descendant_of(const TreeNode&) const; |