diff options
author | Andreas Kling <kling@serenityos.org> | 2022-08-08 15:06:56 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-09-06 00:27:09 +0200 |
commit | a56b3f986228c7b5d1b2cb082f760dbed5e50119 (patch) | |
tree | 2dc226d51dac07c3d623e7831138c510cb5336ee /Userland/Libraries/LibWeb/DOM | |
parent | ae11d70b0c5cacf1b10481f3991fcaeaf11f859b (diff) | |
download | serenity-a56b3f986228c7b5d1b2cb082f760dbed5e50119.zip |
LibWeb: Make NamedNodeMap GC-allocated
Diffstat (limited to 'Userland/Libraries/LibWeb/DOM')
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Element.cpp | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Element.h | 4 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp | 31 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/NamedNodeMap.h | 36 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/NamedNodeMap.idl | 2 |
5 files changed, 48 insertions, 27 deletions
diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index 753a915a27..94526ea7a3 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -45,7 +45,7 @@ namespace Web::DOM { Element::Element(Document& document, DOM::QualifiedName qualified_name) : ParentNode(document, NodeType::ELEMENT_NODE) , m_qualified_name(move(qualified_name)) - , m_attributes(NamedNodeMap::create(*this)) + , m_attributes(JS::make_handle(NamedNodeMap::create(*this))) { make_html_uppercased_qualified_name(); } diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index 6c6b7a1db3..22979d8b72 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -59,7 +59,7 @@ public: void remove_attribute(FlyString const& name); DOM::ExceptionOr<bool> toggle_attribute(FlyString const& name, Optional<bool> force); size_t attribute_list_size() const { return m_attributes->length(); } - NonnullRefPtr<NamedNodeMap> const& attributes() const { return m_attributes; } + NamedNodeMap const* attributes() const { return m_attributes.cell(); } Vector<String> get_attribute_names() const; RefPtr<DOMTokenList> const& class_list(); @@ -150,7 +150,7 @@ private: QualifiedName m_qualified_name; String m_html_uppercased_qualified_name; - NonnullRefPtr<NamedNodeMap> m_attributes; + JS::Handle<NamedNodeMap> m_attributes; JS::Handle<CSS::ElementInlineCSSStyleDeclaration> m_inline_style; diff --git a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp index 30f4d21574..bcc41bc824 100644 --- a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp +++ b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp @@ -1,9 +1,14 @@ /* * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org> + * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ +#include <LibWeb/Bindings/NamedNodeMapPrototype.h> +#include <LibWeb/Bindings/NodeWrapper.h> +#include <LibWeb/Bindings/NodeWrapperFactory.h> +#include <LibWeb/Bindings/WindowObject.h> #include <LibWeb/DOM/Attribute.h> #include <LibWeb/DOM/Document.h> #include <LibWeb/DOM/NamedNodeMap.h> @@ -11,13 +16,15 @@ namespace Web::DOM { -NonnullRefPtr<NamedNodeMap> NamedNodeMap::create(Element& associated_element) +NamedNodeMap* NamedNodeMap::create(Element& element) { - return adopt_ref(*new NamedNodeMap(associated_element)); + auto& realm = element.document().preferred_window_object().realm(); + return realm.heap().allocate<NamedNodeMap>(realm, element); } -NamedNodeMap::NamedNodeMap(Element& associated_element) - : RefCountForwarder(associated_element) +NamedNodeMap::NamedNodeMap(Element& element) + : Bindings::LegacyPlatformObject(element.document().preferred_window_object().ensure_web_prototype<Bindings::NamedNodeMapPrototype>("NamedNodeMap")) + , m_element(element) { } @@ -213,4 +220,20 @@ Attribute const* NamedNodeMap::remove_attribute(StringView qualified_name) return attribute; } +JS::Value NamedNodeMap::item_value(size_t index) const +{ + auto const* node = item(index); + if (!node) + return JS::js_undefined(); + return Bindings::wrap(shape().realm(), const_cast<Attribute&>(*node)); +} + +JS::Value NamedNodeMap::named_item_value(FlyString const& name) const +{ + auto const* node = get_named_item(name); + if (!node) + return JS::js_undefined(); + return Bindings::wrap(shape().realm(), const_cast<Attribute&>(*node)); +} + } diff --git a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h index a2c8144b8d..d03d340290 100644 --- a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h +++ b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h @@ -1,5 +1,6 @@ /* * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org> + * Copyright (c) 2022, Andreas Kling <kling@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -7,30 +8,29 @@ #pragma once #include <AK/NonnullRefPtrVector.h> -#include <AK/RefCountForwarder.h> -#include <AK/RefCounted.h> #include <AK/String.h> #include <AK/StringView.h> -#include <AK/WeakPtr.h> -#include <LibWeb/Bindings/Wrappable.h> +#include <LibWeb/Bindings/LegacyPlatformObject.h> #include <LibWeb/DOM/ExceptionOr.h> #include <LibWeb/Forward.h> namespace Web::DOM { // https://dom.spec.whatwg.org/#interface-namednodemap -class NamedNodeMap final - : public RefCountForwarder<Element> - , public Bindings::Wrappable { +class NamedNodeMap : public Bindings::LegacyPlatformObject { + JS_OBJECT(NamedNodeMap, Bindings::LegacyPlatformObject); public: - using WrapperType = Bindings::NamedNodeMapWrapper; - - static NonnullRefPtr<NamedNodeMap> create(Element& associated_element); + static NamedNodeMap* create(Element&); + explicit NamedNodeMap(Element&); ~NamedNodeMap() = default; - bool is_supported_property_index(u32 index) const; - Vector<String> supported_property_names() const; + NamedNodeMap& impl() { return *this; } + + virtual bool is_supported_property_index(u32 index) const override; + virtual Vector<String> supported_property_names() const override; + virtual JS::Value item_value(size_t index) const override; + virtual JS::Value named_item_value(FlyString const& name) const override; size_t length() const { return m_attributes.size(); } bool is_empty() const { return m_attributes.is_empty(); } @@ -50,20 +50,18 @@ public: Attribute const* remove_attribute(StringView qualified_name); private: - explicit NamedNodeMap(Element& associated_element); - - Element& associated_element() { return ref_count_target(); } - Element const& associated_element() const { return ref_count_target(); } + Element& associated_element() { return m_element; } + Element const& associated_element() const { return m_element; } void remove_attribute_at_index(size_t attribute_index); + DOM::Element& m_element; NonnullRefPtrVector<Attribute> m_attributes; }; } namespace Web::Bindings { - -NamedNodeMapWrapper* wrap(JS::Realm&, DOM::NamedNodeMap&); - +inline JS::Object* wrap(JS::Realm&, Web::DOM::NamedNodeMap& object) { return &object; } +using NamedNodeMapWrapper = Web::DOM::NamedNodeMap; } diff --git a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.idl b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.idl index eee7762de3..5633720ba5 100644 --- a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.idl +++ b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.idl @@ -1,6 +1,6 @@ #import <DOM/Attribute.idl> -[Exposed=Window, LegacyUnenumerableNamedProperties] +[Exposed=Window, LegacyUnenumerableNamedProperties, NoInstanceWrapper] interface NamedNodeMap { readonly attribute unsigned long length; |