mirror of
https://github.com/RGBCube/serenity
synced 2025-07-28 00:07:35 +00:00
LibWeb: Make HTMLCollection and subclasses GC-allocated
This commit is contained in:
parent
4c887bf6c3
commit
2bba97964b
22 changed files with 115 additions and 85 deletions
|
@ -925,14 +925,14 @@ void Document::set_hovered_node(Node* node)
|
|||
invalidate_style();
|
||||
}
|
||||
|
||||
NonnullRefPtr<HTMLCollection> Document::get_elements_by_name(String const& name)
|
||||
JS::NonnullGCPtr<HTMLCollection> Document::get_elements_by_name(String const& name)
|
||||
{
|
||||
return HTMLCollection::create(*this, [name](Element const& element) {
|
||||
return element.name() == name;
|
||||
});
|
||||
}
|
||||
|
||||
NonnullRefPtr<HTMLCollection> Document::get_elements_by_class_name(FlyString const& class_name)
|
||||
JS::NonnullGCPtr<HTMLCollection> Document::get_elements_by_class_name(FlyString const& class_name)
|
||||
{
|
||||
return HTMLCollection::create(*this, [class_name, quirks_mode = document().in_quirks_mode()](Element const& element) {
|
||||
return element.has_class(class_name, quirks_mode ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive);
|
||||
|
@ -940,7 +940,7 @@ NonnullRefPtr<HTMLCollection> Document::get_elements_by_class_name(FlyString con
|
|||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/obsolete.html#dom-document-applets
|
||||
NonnullRefPtr<HTMLCollection> Document::applets()
|
||||
JS::NonnullGCPtr<HTMLCollection> Document::applets()
|
||||
{
|
||||
// FIXME: This should return the same HTMLCollection object every time,
|
||||
// but that would cause a reference cycle since HTMLCollection refs the root.
|
||||
|
@ -948,7 +948,7 @@ NonnullRefPtr<HTMLCollection> Document::applets()
|
|||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/obsolete.html#dom-document-anchors
|
||||
NonnullRefPtr<HTMLCollection> Document::anchors()
|
||||
JS::NonnullGCPtr<HTMLCollection> Document::anchors()
|
||||
{
|
||||
// FIXME: This should return the same HTMLCollection object every time,
|
||||
// but that would cause a reference cycle since HTMLCollection refs the root.
|
||||
|
@ -958,7 +958,7 @@ NonnullRefPtr<HTMLCollection> Document::anchors()
|
|||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/dom.html#dom-document-images
|
||||
NonnullRefPtr<HTMLCollection> Document::images()
|
||||
JS::NonnullGCPtr<HTMLCollection> Document::images()
|
||||
{
|
||||
// FIXME: This should return the same HTMLCollection object every time,
|
||||
// but that would cause a reference cycle since HTMLCollection refs the root.
|
||||
|
@ -968,7 +968,7 @@ NonnullRefPtr<HTMLCollection> Document::images()
|
|||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/dom.html#dom-document-embeds
|
||||
NonnullRefPtr<HTMLCollection> Document::embeds()
|
||||
JS::NonnullGCPtr<HTMLCollection> Document::embeds()
|
||||
{
|
||||
// FIXME: This should return the same HTMLCollection object every time,
|
||||
// but that would cause a reference cycle since HTMLCollection refs the root.
|
||||
|
@ -978,13 +978,13 @@ NonnullRefPtr<HTMLCollection> Document::embeds()
|
|||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/dom.html#dom-document-plugins
|
||||
NonnullRefPtr<HTMLCollection> Document::plugins()
|
||||
JS::NonnullGCPtr<HTMLCollection> Document::plugins()
|
||||
{
|
||||
return embeds();
|
||||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/dom.html#dom-document-links
|
||||
NonnullRefPtr<HTMLCollection> Document::links()
|
||||
JS::NonnullGCPtr<HTMLCollection> Document::links()
|
||||
{
|
||||
// FIXME: This should return the same HTMLCollection object every time,
|
||||
// but that would cause a reference cycle since HTMLCollection refs the root.
|
||||
|
@ -994,7 +994,7 @@ NonnullRefPtr<HTMLCollection> Document::links()
|
|||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/dom.html#dom-document-forms
|
||||
NonnullRefPtr<HTMLCollection> Document::forms()
|
||||
JS::NonnullGCPtr<HTMLCollection> Document::forms()
|
||||
{
|
||||
// FIXME: This should return the same HTMLCollection object every time,
|
||||
// but that would cause a reference cycle since HTMLCollection refs the root.
|
||||
|
@ -1004,7 +1004,7 @@ NonnullRefPtr<HTMLCollection> Document::forms()
|
|||
}
|
||||
|
||||
// https://html.spec.whatwg.org/multipage/dom.html#dom-document-scripts
|
||||
NonnullRefPtr<HTMLCollection> Document::scripts()
|
||||
JS::NonnullGCPtr<HTMLCollection> Document::scripts()
|
||||
{
|
||||
// FIXME: This should return the same HTMLCollection object every time,
|
||||
// but that would cause a reference cycle since HTMLCollection refs the root.
|
||||
|
|
|
@ -172,17 +172,17 @@ public:
|
|||
void schedule_style_update();
|
||||
void schedule_layout_update();
|
||||
|
||||
NonnullRefPtr<HTMLCollection> get_elements_by_name(String const&);
|
||||
NonnullRefPtr<HTMLCollection> get_elements_by_class_name(FlyString const&);
|
||||
JS::NonnullGCPtr<HTMLCollection> get_elements_by_name(String const&);
|
||||
JS::NonnullGCPtr<HTMLCollection> get_elements_by_class_name(FlyString const&);
|
||||
|
||||
NonnullRefPtr<HTMLCollection> applets();
|
||||
NonnullRefPtr<HTMLCollection> anchors();
|
||||
NonnullRefPtr<HTMLCollection> images();
|
||||
NonnullRefPtr<HTMLCollection> embeds();
|
||||
NonnullRefPtr<HTMLCollection> plugins();
|
||||
NonnullRefPtr<HTMLCollection> links();
|
||||
NonnullRefPtr<HTMLCollection> forms();
|
||||
NonnullRefPtr<HTMLCollection> scripts();
|
||||
JS::NonnullGCPtr<HTMLCollection> applets();
|
||||
JS::NonnullGCPtr<HTMLCollection> anchors();
|
||||
JS::NonnullGCPtr<HTMLCollection> images();
|
||||
JS::NonnullGCPtr<HTMLCollection> embeds();
|
||||
JS::NonnullGCPtr<HTMLCollection> plugins();
|
||||
JS::NonnullGCPtr<HTMLCollection> links();
|
||||
JS::NonnullGCPtr<HTMLCollection> forms();
|
||||
JS::NonnullGCPtr<HTMLCollection> scripts();
|
||||
|
||||
String const& source() const { return m_source; }
|
||||
void set_source(String const& source) { m_source = source; }
|
||||
|
|
|
@ -500,7 +500,7 @@ bool Element::is_active() const
|
|||
return document().active_element() == this;
|
||||
}
|
||||
|
||||
NonnullRefPtr<HTMLCollection> Element::get_elements_by_class_name(FlyString const& class_name)
|
||||
JS::NonnullGCPtr<HTMLCollection> Element::get_elements_by_class_name(FlyString const& class_name)
|
||||
{
|
||||
return HTMLCollection::create(*this, [class_name, quirks_mode = document().in_quirks_mode()](Element const& element) {
|
||||
return element.has_class(class_name, quirks_mode ? CaseSensitivity::CaseInsensitive : CaseSensitivity::CaseSensitive);
|
||||
|
|
|
@ -112,7 +112,7 @@ public:
|
|||
bool is_focused() const;
|
||||
bool is_active() const;
|
||||
|
||||
NonnullRefPtr<HTMLCollection> get_elements_by_class_name(FlyString const&);
|
||||
JS::NonnullGCPtr<HTMLCollection> get_elements_by_class_name(FlyString const&);
|
||||
|
||||
ShadowRoot* shadow_root() { return m_shadow_root.ptr(); }
|
||||
ShadowRoot const* shadow_root() const { return m_shadow_root.ptr(); }
|
||||
|
|
|
@ -1,25 +1,39 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2021, Luke Wilde <lukew@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibWeb/Bindings/HTMLCollectionPrototype.h>
|
||||
#include <LibWeb/DOM/Element.h>
|
||||
#include <LibWeb/DOM/HTMLCollection.h>
|
||||
#include <LibWeb/DOM/ParentNode.h>
|
||||
#include <LibWeb/HTML/Window.h>
|
||||
#include <LibWeb/Namespace.h>
|
||||
|
||||
namespace Web::DOM {
|
||||
|
||||
JS::NonnullGCPtr<HTMLCollection> HTMLCollection::create(ParentNode& root, Function<bool(Element const&)> filter)
|
||||
{
|
||||
return *root.heap().allocate<HTMLCollection>(root.realm(), root, move(filter));
|
||||
}
|
||||
|
||||
HTMLCollection::HTMLCollection(ParentNode& root, Function<bool(Element const&)> filter)
|
||||
: m_root(JS::make_handle(root))
|
||||
: LegacyPlatformObject(root.window().ensure_web_prototype<Bindings::HTMLCollectionPrototype>("HTMLCollection"))
|
||||
, m_root(root)
|
||||
, m_filter(move(filter))
|
||||
{
|
||||
}
|
||||
|
||||
HTMLCollection::~HTMLCollection() = default;
|
||||
|
||||
void HTMLCollection::visit_edges(Cell::Visitor& visitor)
|
||||
{
|
||||
Base::visit_edges(visitor);
|
||||
visitor.visit(m_root.ptr());
|
||||
}
|
||||
|
||||
JS::MarkedVector<Element*> HTMLCollection::collect_matching_elements() const
|
||||
{
|
||||
JS::MarkedVector<Element*> elements(m_root->heap());
|
||||
|
@ -109,4 +123,19 @@ bool HTMLCollection::is_supported_property_index(u32 index) const
|
|||
return index < elements.size();
|
||||
}
|
||||
|
||||
JS::Value HTMLCollection::item_value(size_t index) const
|
||||
{
|
||||
auto* element = item(index);
|
||||
if (!element)
|
||||
return JS::js_undefined();
|
||||
return const_cast<Element*>(element);
|
||||
}
|
||||
|
||||
JS::Value HTMLCollection::named_item_value(FlyString const& index) const
|
||||
{
|
||||
auto* element = named_item(index);
|
||||
if (!element)
|
||||
return JS::js_undefined();
|
||||
return const_cast<Element*>(element);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
|
||||
* Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
@ -8,9 +8,8 @@
|
|||
|
||||
#include <AK/FlyString.h>
|
||||
#include <AK/Function.h>
|
||||
#include <AK/Noncopyable.h>
|
||||
#include <LibJS/Heap/GCPtr.h>
|
||||
#include <LibWeb/Bindings/Wrappable.h>
|
||||
#include <LibWeb/Bindings/LegacyPlatformObject.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
|
||||
namespace Web::DOM {
|
||||
|
@ -26,21 +25,13 @@ namespace Web::DOM {
|
|||
// We should teach it how to cache results. The main challenge is invalidating
|
||||
// these caches, since this needs to happen on various kinds of DOM mutation.
|
||||
|
||||
class HTMLCollection
|
||||
: public RefCounted<HTMLCollection>
|
||||
, public Bindings::Wrappable {
|
||||
AK_MAKE_NONCOPYABLE(HTMLCollection);
|
||||
AK_MAKE_NONMOVABLE(HTMLCollection);
|
||||
class HTMLCollection : public Bindings::LegacyPlatformObject {
|
||||
WEB_PLATFORM_OBJECT(HTMLCollection, Bindings::LegacyPlatformObject);
|
||||
|
||||
public:
|
||||
using WrapperType = Bindings::HTMLCollectionWrapper;
|
||||
static JS::NonnullGCPtr<HTMLCollection> create(ParentNode& root, Function<bool(Element const&)> filter);
|
||||
|
||||
static NonnullRefPtr<HTMLCollection> create(ParentNode& root, Function<bool(Element const&)> filter)
|
||||
{
|
||||
return adopt_ref(*new HTMLCollection(root, move(filter)));
|
||||
}
|
||||
|
||||
~HTMLCollection();
|
||||
virtual ~HTMLCollection() override;
|
||||
|
||||
size_t length();
|
||||
Element* item(size_t index) const;
|
||||
|
@ -48,8 +39,10 @@ public:
|
|||
|
||||
JS::MarkedVector<Element*> collect_matching_elements() const;
|
||||
|
||||
Vector<String> supported_property_names() const;
|
||||
bool is_supported_property_index(u32) const;
|
||||
virtual JS::Value item_value(size_t index) const override;
|
||||
virtual JS::Value named_item_value(FlyString const& name) const override;
|
||||
virtual Vector<String> supported_property_names() const override;
|
||||
virtual bool is_supported_property_index(u32) const override;
|
||||
|
||||
protected:
|
||||
HTMLCollection(ParentNode& root, Function<bool(Element const&)> filter);
|
||||
|
@ -57,14 +50,12 @@ protected:
|
|||
JS::NonnullGCPtr<ParentNode> root() { return *m_root; }
|
||||
|
||||
private:
|
||||
JS::Handle<ParentNode> m_root;
|
||||
virtual void visit_edges(Cell::Visitor&) override;
|
||||
|
||||
JS::NonnullGCPtr<ParentNode> m_root;
|
||||
Function<bool(Element const&)> m_filter;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace Web::Bindings {
|
||||
|
||||
HTMLCollectionWrapper* wrap(JS::Realm&, DOM::HTMLCollection&);
|
||||
|
||||
}
|
||||
WRAPPER_HACK(HTMLCollection, Web::DOM)
|
||||
|
|
|
@ -82,7 +82,7 @@ u32 ParentNode::child_element_count() const
|
|||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#dom-parentnode-children
|
||||
NonnullRefPtr<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.
|
||||
// FIXME: This should return the same HTMLCollection object every time,
|
||||
|
@ -94,7 +94,7 @@ NonnullRefPtr<HTMLCollection> ParentNode::children()
|
|||
|
||||
// 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.
|
||||
NonnullRefPtr<HTMLCollection> ParentNode::get_elements_by_tag_name(FlyString const& qualified_name)
|
||||
JS::NonnullGCPtr<HTMLCollection> ParentNode::get_elements_by_tag_name(FlyString const& qualified_name)
|
||||
{
|
||||
// 1. If qualifiedName is "*" (U+002A), return a HTMLCollection rooted at root, whose filter matches only descendant elements.
|
||||
if (qualified_name == "*") {
|
||||
|
@ -122,7 +122,7 @@ NonnullRefPtr<HTMLCollection> ParentNode::get_elements_by_tag_name(FlyString con
|
|||
|
||||
// https://dom.spec.whatwg.org/#concept-getelementsbytagnamens
|
||||
// NOTE: This method is only exposed on Document and Element, but is in ParentNode to prevent code duplication.
|
||||
NonnullRefPtr<HTMLCollection> ParentNode::get_elements_by_tag_name_ns(FlyString const& nullable_namespace, FlyString const& local_name)
|
||||
JS::NonnullGCPtr<HTMLCollection> ParentNode::get_elements_by_tag_name_ns(FlyString const& nullable_namespace, FlyString const& local_name)
|
||||
{
|
||||
// 1. If namespace is the empty string, set it to null.
|
||||
String namespace_ = nullable_namespace;
|
||||
|
|
|
@ -26,10 +26,10 @@ public:
|
|||
ExceptionOr<JS::GCPtr<Element>> query_selector(StringView);
|
||||
ExceptionOr<JS::NonnullGCPtr<NodeList>> query_selector_all(StringView);
|
||||
|
||||
NonnullRefPtr<HTMLCollection> children();
|
||||
JS::NonnullGCPtr<HTMLCollection> children();
|
||||
|
||||
NonnullRefPtr<HTMLCollection> get_elements_by_tag_name(FlyString const&);
|
||||
NonnullRefPtr<HTMLCollection> get_elements_by_tag_name_ns(FlyString const&, FlyString const&);
|
||||
JS::NonnullGCPtr<HTMLCollection> get_elements_by_tag_name(FlyString const&);
|
||||
JS::NonnullGCPtr<HTMLCollection> get_elements_by_tag_name_ns(FlyString const&, FlyString const&);
|
||||
|
||||
ExceptionOr<void> prepend(Vector<Variant<JS::Handle<Node>, String>> const& nodes);
|
||||
ExceptionOr<void> append(Vector<Variant<JS::Handle<Node>, String>> const& nodes);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue