mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-01-07 19:57:45 +03: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
Notes:
sideshowbarker
2024-07-19 11:53:18 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/7912592f894
@ -55,6 +55,9 @@ public:
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
virtual void inserted_into(Node&) {}
|
||||
virtual void removed_from(Node&) {}
|
||||
|
||||
protected:
|
||||
Node(Document&, NodeType);
|
||||
|
||||
|
@ -64,6 +64,9 @@ public:
|
||||
|
||||
const StyleProperties& style_properties() const { return m_style_properties; }
|
||||
|
||||
void inserted_into(LayoutNode&) {}
|
||||
void removed_from(LayoutNode&) {}
|
||||
|
||||
protected:
|
||||
explicit LayoutNode(const Node*, StyleProperties&&);
|
||||
|
||||
|
@ -1,3 +1,4 @@
|
||||
#include <AK/Function.h>
|
||||
#include <AK/NonnullRefPtrVector.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibHTML/DOM/Element.h>
|
||||
@ -99,7 +100,7 @@ NonnullRefPtr<Document> parse_html(const String& html)
|
||||
attribute_value_buffer.clear();
|
||||
if (state == State::Free && !text_buffer.string_view().is_empty()) {
|
||||
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;
|
||||
text_buffer.clear();
|
||||
@ -116,7 +117,7 @@ NonnullRefPtr<Document> parse_html(const String& html)
|
||||
new_element->set_attributes(move(attributes));
|
||||
node_stack.append(new_element);
|
||||
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()))
|
||||
close_tag();
|
||||
@ -278,5 +279,16 @@ NonnullRefPtr<Document> parse_html(const String& html)
|
||||
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;
|
||||
}
|
||||
|
@ -33,7 +33,7 @@ public:
|
||||
const T* first_child() const { return m_first_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);
|
||||
|
||||
protected:
|
||||
@ -49,16 +49,19 @@ private:
|
||||
};
|
||||
|
||||
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);
|
||||
if (m_last_child)
|
||||
m_last_child->m_next_sibling = node.ptr();
|
||||
node->m_previous_sibling = m_last_child;
|
||||
node->m_parent = static_cast<T*>(this);
|
||||
m_last_child = &node.leak_ref();
|
||||
m_last_child = node.ptr();
|
||||
if (!m_first_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>
|
||||
|
Loading…
Reference in New Issue
Block a user