1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:57:35 +00:00

LibWeb: Make NamedNodeMap GC-allocated

This commit is contained in:
Andreas Kling 2022-08-08 15:06:56 +02:00
parent ae11d70b0c
commit a56b3f9862
7 changed files with 49 additions and 29 deletions

View file

@ -45,7 +45,7 @@ namespace Web::DOM {
Element::Element(Document& document, DOM::QualifiedName qualified_name) Element::Element(Document& document, DOM::QualifiedName qualified_name)
: ParentNode(document, NodeType::ELEMENT_NODE) : ParentNode(document, NodeType::ELEMENT_NODE)
, m_qualified_name(move(qualified_name)) , m_qualified_name(move(qualified_name))
, m_attributes(NamedNodeMap::create(*this)) , m_attributes(JS::make_handle(NamedNodeMap::create(*this)))
{ {
make_html_uppercased_qualified_name(); make_html_uppercased_qualified_name();
} }

View file

@ -59,7 +59,7 @@ public:
void remove_attribute(FlyString const& name); void remove_attribute(FlyString const& name);
DOM::ExceptionOr<bool> toggle_attribute(FlyString const& name, Optional<bool> force); DOM::ExceptionOr<bool> toggle_attribute(FlyString const& name, Optional<bool> force);
size_t attribute_list_size() const { return m_attributes->length(); } 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; Vector<String> get_attribute_names() const;
RefPtr<DOMTokenList> const& class_list(); RefPtr<DOMTokenList> const& class_list();
@ -150,7 +150,7 @@ private:
QualifiedName m_qualified_name; QualifiedName m_qualified_name;
String m_html_uppercased_qualified_name; String m_html_uppercased_qualified_name;
NonnullRefPtr<NamedNodeMap> m_attributes; JS::Handle<NamedNodeMap> m_attributes;
JS::Handle<CSS::ElementInlineCSSStyleDeclaration> m_inline_style; JS::Handle<CSS::ElementInlineCSSStyleDeclaration> m_inline_style;

View file

@ -1,9 +1,14 @@
/* /*
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org> * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * 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/Attribute.h>
#include <LibWeb/DOM/Document.h> #include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/NamedNodeMap.h> #include <LibWeb/DOM/NamedNodeMap.h>
@ -11,13 +16,15 @@
namespace Web::DOM { 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) NamedNodeMap::NamedNodeMap(Element& element)
: RefCountForwarder(associated_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; 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));
}
} }

View file

@ -1,5 +1,6 @@
/* /*
* Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org> * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
* Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
* *
* SPDX-License-Identifier: BSD-2-Clause * SPDX-License-Identifier: BSD-2-Clause
*/ */
@ -7,30 +8,29 @@
#pragma once #pragma once
#include <AK/NonnullRefPtrVector.h> #include <AK/NonnullRefPtrVector.h>
#include <AK/RefCountForwarder.h>
#include <AK/RefCounted.h>
#include <AK/String.h> #include <AK/String.h>
#include <AK/StringView.h> #include <AK/StringView.h>
#include <AK/WeakPtr.h> #include <LibWeb/Bindings/LegacyPlatformObject.h>
#include <LibWeb/Bindings/Wrappable.h>
#include <LibWeb/DOM/ExceptionOr.h> #include <LibWeb/DOM/ExceptionOr.h>
#include <LibWeb/Forward.h> #include <LibWeb/Forward.h>
namespace Web::DOM { namespace Web::DOM {
// https://dom.spec.whatwg.org/#interface-namednodemap // https://dom.spec.whatwg.org/#interface-namednodemap
class NamedNodeMap final class NamedNodeMap : public Bindings::LegacyPlatformObject {
: public RefCountForwarder<Element> JS_OBJECT(NamedNodeMap, Bindings::LegacyPlatformObject);
, public Bindings::Wrappable {
public: public:
using WrapperType = Bindings::NamedNodeMapWrapper; static NamedNodeMap* create(Element&);
explicit NamedNodeMap(Element&);
static NonnullRefPtr<NamedNodeMap> create(Element& associated_element);
~NamedNodeMap() = default; ~NamedNodeMap() = default;
bool is_supported_property_index(u32 index) const; NamedNodeMap& impl() { return *this; }
Vector<String> supported_property_names() const;
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(); } size_t length() const { return m_attributes.size(); }
bool is_empty() const { return m_attributes.is_empty(); } bool is_empty() const { return m_attributes.is_empty(); }
@ -50,20 +50,18 @@ public:
Attribute const* remove_attribute(StringView qualified_name); Attribute const* remove_attribute(StringView qualified_name);
private: private:
explicit NamedNodeMap(Element& associated_element); Element& associated_element() { return m_element; }
Element const& associated_element() const { return m_element; }
Element& associated_element() { return ref_count_target(); }
Element const& associated_element() const { return ref_count_target(); }
void remove_attribute_at_index(size_t attribute_index); void remove_attribute_at_index(size_t attribute_index);
DOM::Element& m_element;
NonnullRefPtrVector<Attribute> m_attributes; NonnullRefPtrVector<Attribute> m_attributes;
}; };
} }
namespace Web::Bindings { namespace Web::Bindings {
inline JS::Object* wrap(JS::Realm&, Web::DOM::NamedNodeMap& object) { return &object; }
NamedNodeMapWrapper* wrap(JS::Realm&, DOM::NamedNodeMap&); using NamedNodeMapWrapper = Web::DOM::NamedNodeMap;
} }

View file

@ -1,6 +1,6 @@
#import <DOM/Attribute.idl> #import <DOM/Attribute.idl>
[Exposed=Window, LegacyUnenumerableNamedProperties] [Exposed=Window, LegacyUnenumerableNamedProperties, NoInstanceWrapper]
interface NamedNodeMap { interface NamedNodeMap {
readonly attribute unsigned long length; readonly attribute unsigned long length;

View file

@ -569,7 +569,6 @@ class MessagePortWrapper;
class MouseEventWrapper; class MouseEventWrapper;
class MutationObserverWrapper; class MutationObserverWrapper;
class MutationRecordWrapper; class MutationRecordWrapper;
class NamedNodeMapWrapper;
class NodeFilterWrapper; class NodeFilterWrapper;
class NodeIteratorWrapper; class NodeIteratorWrapper;
class NodeListWrapper; class NodeListWrapper;

View file

@ -40,7 +40,7 @@ libweb_js_wrapper(DOM/EventTarget)
libweb_js_wrapper(DOM/HTMLCollection) libweb_js_wrapper(DOM/HTMLCollection)
libweb_js_wrapper(DOM/MutationRecord) libweb_js_wrapper(DOM/MutationRecord)
libweb_js_wrapper(DOM/MutationObserver) 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/Node)
libweb_js_wrapper(DOM/NodeIterator) libweb_js_wrapper(DOM/NodeIterator)
libweb_js_wrapper(DOM/NodeList) libweb_js_wrapper(DOM/NodeList)