1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 03:57:43 +00:00

LibHTML: Add is<ElementType> and to<ElementType> helper functions

These will help us write node-type-aware template functions.
This commit is contained in:
Andreas Kling 2019-10-06 20:37:39 +02:00
parent bedb00603c
commit f52f2736e1
12 changed files with 93 additions and 22 deletions

View file

@ -74,3 +74,41 @@ protected:
mutable LayoutNode* m_layout_node { nullptr };
NodeType m_type { NodeType::INVALID };
};
template<typename T>
inline bool is(const Node&)
{
return false;
}
template<typename T>
inline bool is(const Node* node)
{
return node && is<T>(*node);
}
template<>
inline bool is<Node>(const Node&)
{
return true;
}
template<>
inline bool is<ParentNode>(const Node& node)
{
return node.is_parent_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);
}