mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 23:27:35 +00:00
LibHTML: Add inserted_into() and removed_from() TreeNode callbacks
These will be called when a Node or LayoutNode is inserted or removed from a tree. They get the parent node as an argument.
This commit is contained in:
parent
402c6de5c9
commit
7912592f89
4 changed files with 26 additions and 5 deletions
|
@ -55,6 +55,9 @@ public:
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual void inserted_into(Node&) {}
|
||||||
|
virtual void removed_from(Node&) {}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Node(Document&, NodeType);
|
Node(Document&, NodeType);
|
||||||
|
|
||||||
|
|
|
@ -64,6 +64,9 @@ public:
|
||||||
|
|
||||||
const StyleProperties& style_properties() const { return m_style_properties; }
|
const StyleProperties& style_properties() const { return m_style_properties; }
|
||||||
|
|
||||||
|
void inserted_into(LayoutNode&) {}
|
||||||
|
void removed_from(LayoutNode&) {}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
explicit LayoutNode(const Node*, StyleProperties&&);
|
explicit LayoutNode(const Node*, StyleProperties&&);
|
||||||
|
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
#include <AK/Function.h>
|
||||||
#include <AK/NonnullRefPtrVector.h>
|
#include <AK/NonnullRefPtrVector.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <LibHTML/DOM/Element.h>
|
#include <LibHTML/DOM/Element.h>
|
||||||
|
@ -99,7 +100,7 @@ NonnullRefPtr<Document> parse_html(const String& html)
|
||||||
attribute_value_buffer.clear();
|
attribute_value_buffer.clear();
|
||||||
if (state == State::Free && !text_buffer.string_view().is_empty()) {
|
if (state == State::Free && !text_buffer.string_view().is_empty()) {
|
||||||
auto text_node = adopt(*new Text(document, text_buffer.to_string()));
|
auto text_node = adopt(*new Text(document, text_buffer.to_string()));
|
||||||
node_stack.last().append_child(text_node);
|
node_stack.last().append_child(text_node, false);
|
||||||
}
|
}
|
||||||
state = new_state;
|
state = new_state;
|
||||||
text_buffer.clear();
|
text_buffer.clear();
|
||||||
|
@ -116,7 +117,7 @@ NonnullRefPtr<Document> parse_html(const String& html)
|
||||||
new_element->set_attributes(move(attributes));
|
new_element->set_attributes(move(attributes));
|
||||||
node_stack.append(new_element);
|
node_stack.append(new_element);
|
||||||
if (node_stack.size() != 1)
|
if (node_stack.size() != 1)
|
||||||
node_stack[node_stack.size() - 2].append_child(new_element);
|
node_stack[node_stack.size() - 2].append_child(new_element, false);
|
||||||
|
|
||||||
if (is_self_closing_tag(new_element->tag_name()))
|
if (is_self_closing_tag(new_element->tag_name()))
|
||||||
close_tag();
|
close_tag();
|
||||||
|
@ -278,5 +279,16 @@ NonnullRefPtr<Document> parse_html(const String& html)
|
||||||
ASSERT_NOT_REACHED();
|
ASSERT_NOT_REACHED();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Function<void(Node&)> fire_insertion_callbacks = [&](Node& node) {
|
||||||
|
for (auto* child = node.first_child(); child; child = child->next_sibling()) {
|
||||||
|
fire_insertion_callbacks(*child);
|
||||||
|
}
|
||||||
|
if (node.parent())
|
||||||
|
node.inserted_into(*node.parent());
|
||||||
|
};
|
||||||
|
|
||||||
|
fire_insertion_callbacks(*document);
|
||||||
|
|
||||||
return document;
|
return document;
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ public:
|
||||||
const T* first_child() const { return m_first_child; }
|
const T* first_child() const { return m_first_child; }
|
||||||
const T* last_child() const { return m_last_child; }
|
const T* last_child() const { return m_last_child; }
|
||||||
|
|
||||||
void append_child(NonnullRefPtr<T> node);
|
void append_child(NonnullRefPtr<T> node, bool call_inserted_into = true);
|
||||||
void donate_all_children_to(T& node);
|
void donate_all_children_to(T& node);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
@ -49,16 +49,19 @@ private:
|
||||||
};
|
};
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
inline void TreeNode<T>::append_child(NonnullRefPtr<T> node)
|
inline void TreeNode<T>::append_child(NonnullRefPtr<T> node, bool call_inserted_into)
|
||||||
{
|
{
|
||||||
ASSERT(!node->m_parent);
|
ASSERT(!node->m_parent);
|
||||||
if (m_last_child)
|
if (m_last_child)
|
||||||
m_last_child->m_next_sibling = node.ptr();
|
m_last_child->m_next_sibling = node.ptr();
|
||||||
node->m_previous_sibling = m_last_child;
|
node->m_previous_sibling = m_last_child;
|
||||||
node->m_parent = static_cast<T*>(this);
|
node->m_parent = static_cast<T*>(this);
|
||||||
m_last_child = &node.leak_ref();
|
m_last_child = node.ptr();
|
||||||
if (!m_first_child)
|
if (!m_first_child)
|
||||||
m_first_child = m_last_child;
|
m_first_child = m_last_child;
|
||||||
|
if (call_inserted_into)
|
||||||
|
node->inserted_into(static_cast<T&>(*this));
|
||||||
|
(void)node.leak_ref();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue