diff options
Diffstat (limited to 'Userland/Libraries/LibWeb/DOM')
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Document.cpp | 20 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Document.h | 22 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Element.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Element.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/HTMLCollection.cpp | 33 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/HTMLCollection.h | 37 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/ParentNode.cpp | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/ParentNode.h | 6 |
8 files changed, 74 insertions, 54 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 5b42f2e31b..78add97ac4 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -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. diff --git a/Userland/Libraries/LibWeb/DOM/Document.h b/Userland/Libraries/LibWeb/DOM/Document.h index f4d6e62525..36efd422ca 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.h +++ b/Userland/Libraries/LibWeb/DOM/Document.h @@ -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&); - - 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> get_elements_by_name(String const&); + JS::NonnullGCPtr<HTMLCollection> get_elements_by_class_name(FlyString const&); + + 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; } diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index 665bf4b40e..4826fc8d21 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -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); diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index 45aa9411e1..0c64a6ceff 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -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(); } diff --git a/Userland/Libraries/LibWeb/DOM/HTMLCollection.cpp b/Userland/Libraries/LibWeb/DOM/HTMLCollection.cpp index 9f95e30094..d4977dab8c 100644 --- a/Userland/Libraries/LibWeb/DOM/HTMLCollection.cpp +++ b/Userland/Libraries/LibWeb/DOM/HTMLCollection.cpp @@ -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); +} } diff --git a/Userland/Libraries/LibWeb/DOM/HTMLCollection.h b/Userland/Libraries/LibWeb/DOM/HTMLCollection.h index eae346bf07..b9f5a7eca2 100644 --- a/Userland/Libraries/LibWeb/DOM/HTMLCollection.h +++ b/Userland/Libraries/LibWeb/DOM/HTMLCollection.h @@ -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) diff --git a/Userland/Libraries/LibWeb/DOM/ParentNode.cpp b/Userland/Libraries/LibWeb/DOM/ParentNode.cpp index 42bbc2428a..efbcfbffe9 100644 --- a/Userland/Libraries/LibWeb/DOM/ParentNode.cpp +++ b/Userland/Libraries/LibWeb/DOM/ParentNode.cpp @@ -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; diff --git a/Userland/Libraries/LibWeb/DOM/ParentNode.h b/Userland/Libraries/LibWeb/DOM/ParentNode.h index a2667892dd..0d2482140c 100644 --- a/Userland/Libraries/LibWeb/DOM/ParentNode.h +++ b/Userland/Libraries/LibWeb/DOM/ParentNode.h @@ -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); |