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

LibHTML: Add Comment and CharacterData nodes and improve HTML parsing

This patch adds the CharacterData subclass of Node, which is now the
parent class of Text and a new Comment class.

A Comment node is one of these in HTML: <!--hello friends-->
Since these occur somewhat frequently on the web, we need to be able
to parse them.

This patch also adds a child rejection mechanism to the DOM tree.
Nodes can now override is_child_allowed(Node) and return false if they
don't want a particular Node to become a child of theirs. This is used
to prevent Document from taking on unwanted children.
This commit is contained in:
Andreas Kling 2019-10-12 23:26:47 +02:00
parent 6d150df58a
commit b083a233d8
15 changed files with 158 additions and 25 deletions

View file

@ -50,8 +50,10 @@ public:
void append_child(NonnullRefPtr<T> node, bool call_inserted_into = true);
void donate_all_children_to(T& node);
bool is_child_allowed(const T&) const { return true; }
protected:
TreeNode() { }
TreeNode() {}
private:
int m_ref_count { 1 };
@ -66,6 +68,10 @@ template<typename T>
inline void TreeNode<T>::append_child(NonnullRefPtr<T> node, bool call_inserted_into)
{
ASSERT(!node->m_parent);
if (!static_cast<T*>(this)->is_child_allowed(*node))
return;
if (m_last_child)
m_last_child->m_next_sibling = node.ptr();
node->m_previous_sibling = m_last_child;
@ -82,6 +88,10 @@ template<typename T>
inline void TreeNode<T>::prepend_child(NonnullRefPtr<T> node, bool call_inserted_into)
{
ASSERT(!node->m_parent);
if (!static_cast<T*>(this)->is_child_allowed(*node))
return;
if (m_first_child)
m_first_child->m_previous_sibling = node.ptr();
node->m_next_sibling = m_first_child;
@ -112,7 +122,6 @@ inline void TreeNode<T>::donate_all_children_to(T& node)
m_last_child = nullptr;
}
template<typename T>
inline bool TreeNode<T>::is_ancestor_of(const TreeNode<T>& other) const
{