mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-07 10:06:03 +00:00
LibWeb: Make ParentNode.children vend the same HTMLCollection every time
Instead of creating a new HTMLCollection every time you access .children, we now follow the spec and vend the same object. This was annoyingly difficult before, and trivial now that the DOM is garbage-collected. :^)
This commit is contained in:
parent
6b3293a74b
commit
dc6e625680
Notes:
sideshowbarker
2024-07-17 09:39:38 +09:00
Author: https://github.com/awesomekling
Commit: dc6e625680
2 changed files with 17 additions and 5 deletions
|
@ -84,16 +84,23 @@ u32 ParentNode::child_element_count() const
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void ParentNode::visit_edges(Cell::Visitor& visitor)
|
||||||
|
{
|
||||||
|
Base::visit_edges(visitor);
|
||||||
|
visitor.visit(m_children);
|
||||||
|
}
|
||||||
|
|
||||||
// https://dom.spec.whatwg.org/#dom-parentnode-children
|
// https://dom.spec.whatwg.org/#dom-parentnode-children
|
||||||
JS::NonnullGCPtr<HTMLCollection> ParentNode::children()
|
JS::NonnullGCPtr<HTMLCollection> ParentNode::children()
|
||||||
{
|
{
|
||||||
// The children getter steps are to return an HTMLCollection collection rooted at this matching only element children.
|
// The children getter steps are to return an HTMLCollection collection rooted at this matching only element children.
|
||||||
// FIXME: This should return the same HTMLCollection object every time,
|
if (!m_children) {
|
||||||
// but that would cause a reference cycle since HTMLCollection refs the root.
|
m_children = HTMLCollection::create(*this, [this](Element const& element) {
|
||||||
return HTMLCollection::create(*this, [this](Element const& element) {
|
|
||||||
return is_parent_of(element);
|
return is_parent_of(element);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
return *m_children;
|
||||||
|
}
|
||||||
|
|
||||||
// https://dom.spec.whatwg.org/#concept-getelementsbytagname
|
// https://dom.spec.whatwg.org/#concept-getelementsbytagname
|
||||||
// NOTE: This method is only exposed on Document and Element, but is in ParentNode to prevent code duplication.
|
// NOTE: This method is only exposed on Document and Element, but is in ParentNode to prevent code duplication.
|
||||||
|
|
|
@ -45,6 +45,11 @@ protected:
|
||||||
: Node(document, type)
|
: Node(document, type)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
virtual void visit_edges(Cell::Visitor&) override;
|
||||||
|
|
||||||
|
private:
|
||||||
|
JS::GCPtr<HTMLCollection> m_children;
|
||||||
};
|
};
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue