1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 04:58:13 +00:00

LibHTML: Templatize Node::first_child_of_type<T>()

This is a lot nicer than first_child_with_tag_name(...).

The is<T>(Node) functions are obviously unoptimized at the moment,
and this is about establishing pleasant patterns right now. :^)
This commit is contained in:
Andreas Kling 2019-10-06 20:47:57 +02:00
parent f52f2736e1
commit 3bee9d3d3c
5 changed files with 48 additions and 12 deletions

View file

@ -47,14 +47,8 @@ public:
virtual bool is_html_element() const { return false; }
const Node* first_child_with_tag_name(const StringView& tag_name) const
{
for (auto* child = first_child(); child; child = child->next_sibling()) {
if (child->tag_name() == tag_name)
return child;
}
return nullptr;
}
template<typename T>
const T* first_child_of_type() const;
virtual void inserted_into(Node&) {}
virtual void removed_from(Node&) {}
@ -106,9 +100,33 @@ inline const T& to(const Node& node)
return static_cast<const T&>(node);
}
template<typename T>
inline T* to(Node* node)
{
ASSERT(is<T>(node));
return static_cast<T*>(node);
}
template<typename T>
inline const T* to(const Node* node)
{
ASSERT(is<T>(node));
return static_cast<const T*>(node);
}
template<typename T>
inline T& to(Node& node)
{
ASSERT(is<T>(node));
return static_cast<T&>(node);
}
template<typename T>
inline const T* Node::first_child_of_type() const
{
for (auto* child = first_child(); child; child = child->next_sibling()) {
if (is<T>(*child))
return to<T>(child);
}
return nullptr;
}