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 toggle_attribute(FlyString const& name, Optional force); size_t attribute_list_size() const { return m_attributes->length(); } - NonnullRefPtr const& attributes() const { return m_attributes; } + NamedNodeMap const* attributes() const { return m_attributes.cell(); } Vector get_attribute_names() const; RefPtr const& class_list(); @@ -150,7 +150,7 @@ private: QualifiedName m_qualified_name; String m_html_uppercased_qualified_name; - NonnullRefPtr m_attributes; + JS::Handle m_attributes; JS::Handle 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 + * Copyright (c) 2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ +#include +#include +#include +#include #include #include #include @@ -11,13 +16,15 @@ namespace Web::DOM { -NonnullRefPtr 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(realm, element); } -NamedNodeMap::NamedNodeMap(Element& associated_element) - : RefCountForwarder(associated_element) +NamedNodeMap::NamedNodeMap(Element& element) + : Bindings::LegacyPlatformObject(element.document().preferred_window_object().ensure_web_prototype("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(*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(*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 + * Copyright (c) 2022, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -7,30 +8,29 @@ #pragma once #include -#include -#include #include #include -#include -#include +#include #include #include namespace Web::DOM { // https://dom.spec.whatwg.org/#interface-namednodemap -class NamedNodeMap final - : public RefCountForwarder - , public Bindings::Wrappable { +class NamedNodeMap : public Bindings::LegacyPlatformObject { + JS_OBJECT(NamedNodeMap, Bindings::LegacyPlatformObject); public: - using WrapperType = Bindings::NamedNodeMapWrapper; - - static NonnullRefPtr create(Element& associated_element); + static NamedNodeMap* create(Element&); + explicit NamedNodeMap(Element&); ~NamedNodeMap() = default; - bool is_supported_property_index(u32 index) const; - Vector supported_property_names() const; + NamedNodeMap& impl() { return *this; } + + virtual bool is_supported_property_index(u32 index) const override; + virtual Vector 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 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 -[Exposed=Window, LegacyUnenumerableNamedProperties] +[Exposed=Window, LegacyUnenumerableNamedProperties, NoInstanceWrapper] interface NamedNodeMap { readonly attribute unsigned long length; diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h index dd01406f82..17860ad5e1 100644 --- a/Userland/Libraries/LibWeb/Forward.h +++ b/Userland/Libraries/LibWeb/Forward.h @@ -569,7 +569,6 @@ class MessagePortWrapper; class MouseEventWrapper; class MutationObserverWrapper; class MutationRecordWrapper; -class NamedNodeMapWrapper; class NodeFilterWrapper; class NodeIteratorWrapper; class NodeListWrapper; diff --git a/Userland/Libraries/LibWeb/idl_files.cmake b/Userland/Libraries/LibWeb/idl_files.cmake index 62c83c4fbe..e39051582c 100644 --- a/Userland/Libraries/LibWeb/idl_files.cmake +++ b/Userland/Libraries/LibWeb/idl_files.cmake @@ -40,7 +40,7 @@ libweb_js_wrapper(DOM/EventTarget) libweb_js_wrapper(DOM/HTMLCollection) libweb_js_wrapper(DOM/MutationRecord) libweb_js_wrapper(DOM/MutationObserver) -libweb_js_wrapper(DOM/NamedNodeMap) +libweb_js_wrapper(DOM/NamedNodeMap NO_INSTANCE) libweb_js_wrapper(DOM/Node) libweb_js_wrapper(DOM/NodeIterator) libweb_js_wrapper(DOM/NodeList)