mirror of
https://github.com/RGBCube/serenity
synced 2025-07-24 14:47:34 +00:00
LibWeb: Implement (most of) NamedNodeMap to store attributes
This commit is contained in:
parent
454d218716
commit
2a3ac02ef1
6 changed files with 284 additions and 0 deletions
|
@ -928,6 +928,8 @@ static bool is_wrappable_type(IDL::Type const& type)
|
|||
return true;
|
||||
if (type.name == "Attribute")
|
||||
return true;
|
||||
if (type.name == "NamedNodeMap")
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -1655,6 +1657,7 @@ void generate_implementation(IDL::Interface const& interface)
|
|||
#include <LibWeb/Bindings/IDLAbstractOperations.h>
|
||||
#include <LibWeb/Bindings/ImageDataWrapper.h>
|
||||
#include <LibWeb/Bindings/MessagePortWrapper.h>
|
||||
#include <LibWeb/Bindings/NamedNodeMapWrapper.h>
|
||||
#include <LibWeb/Bindings/NodeWrapperFactory.h>
|
||||
#include <LibWeb/Bindings/TextWrapper.h>
|
||||
#include <LibWeb/Bindings/WindowObject.h>
|
||||
|
@ -2867,6 +2870,7 @@ void generate_prototype_implementation(IDL::Interface const& interface)
|
|||
#include <LibWeb/Bindings/ImageDataWrapper.h>
|
||||
#include <LibWeb/Bindings/LocationObject.h>
|
||||
#include <LibWeb/Bindings/MessagePortWrapper.h>
|
||||
#include <LibWeb/Bindings/NamedNodeMapWrapper.h>
|
||||
#include <LibWeb/Bindings/NodeListWrapper.h>
|
||||
#include <LibWeb/Bindings/NodeWrapperFactory.h>
|
||||
#include <LibWeb/Bindings/PerformanceTimingWrapper.h>
|
||||
|
|
|
@ -73,6 +73,7 @@ set(SOURCES
|
|||
DOM/EventTarget.cpp
|
||||
DOM/HTMLCollection.cpp
|
||||
DOM/LiveNodeList.cpp
|
||||
DOM/NamedNodeMap.cpp
|
||||
DOM/Node.cpp
|
||||
DOM/ParentNode.cpp
|
||||
DOM/Position.cpp
|
||||
|
@ -383,6 +384,7 @@ libweb_js_wrapper(DOM/Element)
|
|||
libweb_js_wrapper(DOM/Event)
|
||||
libweb_js_wrapper(DOM/EventTarget)
|
||||
libweb_js_wrapper(DOM/HTMLCollection)
|
||||
libweb_js_wrapper(DOM/NamedNodeMap)
|
||||
libweb_js_wrapper(DOM/Node)
|
||||
libweb_js_wrapper(DOM/NodeList)
|
||||
libweb_js_wrapper(DOM/ProcessingInstruction)
|
||||
|
|
199
Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp
Normal file
199
Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp
Normal file
|
@ -0,0 +1,199 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibWeb/DOM/Attribute.h>
|
||||
#include <LibWeb/DOM/Document.h>
|
||||
#include <LibWeb/DOM/NamedNodeMap.h>
|
||||
#include <LibWeb/Namespace.h>
|
||||
|
||||
namespace Web::DOM {
|
||||
|
||||
NonnullRefPtr<NamedNodeMap> NamedNodeMap::create(Element const& associated_element)
|
||||
{
|
||||
return adopt_ref(*new NamedNodeMap(associated_element));
|
||||
}
|
||||
|
||||
NamedNodeMap::NamedNodeMap(Element const& associated_element)
|
||||
: m_associated_element(associated_element)
|
||||
{
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-indices%E2%91%A3
|
||||
bool NamedNodeMap::is_supported_property_index(u32 index) const
|
||||
{
|
||||
return index < m_attributes.size();
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#ref-for-dfn-supported-property-names%E2%91%A0
|
||||
Vector<String> NamedNodeMap::supported_property_names() const
|
||||
{
|
||||
// 1. Let names be the qualified names of the attributes in this NamedNodeMap object’s attribute list, with duplicates omitted, in order.
|
||||
Vector<String> names;
|
||||
names.ensure_capacity(m_attributes.size());
|
||||
|
||||
for (auto const& attribute : m_attributes) {
|
||||
if (!names.contains_slow(attribute.name()))
|
||||
names.append(attribute.name());
|
||||
}
|
||||
|
||||
// 2. If this NamedNodeMap object’s element is in the HTML namespace and its node document is an HTML document, then for each name in names:
|
||||
// FIXME: Handle the second condition, assume it is an HTML document for now.
|
||||
if (m_associated_element.namespace_uri() == Namespace::HTML) {
|
||||
// 1. Let lowercaseName be name, in ASCII lowercase.
|
||||
// 2. If lowercaseName is not equal to name, remove name from names.
|
||||
names.remove_all_matching([](auto const& name) { return name != name.to_lowercase(); });
|
||||
}
|
||||
|
||||
// 3. Return names.
|
||||
return names;
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#dom-namednodemap-item
|
||||
Attribute const* NamedNodeMap::item(u32 index) const
|
||||
{
|
||||
// 1. If index is equal to or greater than this’s attribute list’s size, then return null.
|
||||
if (index >= m_attributes.size())
|
||||
return nullptr;
|
||||
|
||||
// 2. Otherwise, return this’s attribute list[index].
|
||||
return &m_attributes[index];
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#dom-namednodemap-getnameditem
|
||||
Attribute const* NamedNodeMap::get_named_item(StringView qualified_name) const
|
||||
{
|
||||
return get_attribute(qualified_name);
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#dom-namednodemap-setnameditem
|
||||
ExceptionOr<Attribute const*> NamedNodeMap::set_named_item(Attribute& attribute)
|
||||
{
|
||||
return set_attribute(attribute);
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#dom-namednodemap-removenameditem
|
||||
ExceptionOr<Attribute const*> NamedNodeMap::remove_named_item(StringView qualified_name)
|
||||
{
|
||||
// 1. Let attr be the result of removing an attribute given qualifiedName and element.
|
||||
auto const* attribute = remove_attribute(qualified_name);
|
||||
|
||||
// 2. If attr is null, then throw a "NotFoundError" DOMException.
|
||||
if (!attribute)
|
||||
return NotFoundError::create(String::formatted("Attribute with name '{}' not found", qualified_name));
|
||||
|
||||
// 3. Return attr.
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
|
||||
Attribute* NamedNodeMap::get_attribute(StringView qualified_name, size_t* item_index)
|
||||
{
|
||||
return const_cast<Attribute*>(const_cast<NamedNodeMap const*>(this)->get_attribute(qualified_name, item_index));
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name
|
||||
Attribute const* NamedNodeMap::get_attribute(StringView qualified_name, size_t* item_index) const
|
||||
{
|
||||
if (item_index)
|
||||
*item_index = 0;
|
||||
|
||||
// 1. If element is in the HTML namespace and its node document is an HTML document, then set qualifiedName to qualifiedName in ASCII lowercase.
|
||||
// FIXME: Handle the second condition, assume it is an HTML document for now.
|
||||
Optional<String> qualified_name_lowercase;
|
||||
if (m_associated_element.namespace_uri() == Namespace::HTML) {
|
||||
qualified_name_lowercase = qualified_name.to_lowercase_string();
|
||||
qualified_name = qualified_name_lowercase->view();
|
||||
}
|
||||
|
||||
// 2. Return the first attribute in element’s attribute list whose qualified name is qualifiedName; otherwise null.
|
||||
for (auto const& attribute : m_attributes) {
|
||||
if (attribute.name() == qualified_name)
|
||||
return &attribute;
|
||||
if (item_index)
|
||||
++(*item_index);
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#concept-element-attributes-set
|
||||
ExceptionOr<Attribute const*> NamedNodeMap::set_attribute(Attribute& attribute)
|
||||
{
|
||||
// 1. If attr’s element is neither null nor element, throw an "InUseAttributeError" DOMException.
|
||||
if ((attribute.owner_element() != nullptr) && (attribute.owner_element() != &m_associated_element))
|
||||
return InUseAttributeError::create("Attribute must not already be in use"sv);
|
||||
|
||||
// 2. Let oldAttr be the result of getting an attribute given attr’s namespace, attr’s local name, and element.
|
||||
// FIXME: When getNamedItemNS is implemented, use that instead.
|
||||
size_t old_attribute_index = 0;
|
||||
auto* old_attribute = get_attribute(attribute.local_name(), &old_attribute_index);
|
||||
|
||||
// 3. If oldAttr is attr, return attr.
|
||||
if (old_attribute == &attribute)
|
||||
return &attribute;
|
||||
|
||||
// 4. If oldAttr is non-null, then replace oldAttr with attr.
|
||||
if (old_attribute) {
|
||||
replace_attribute(*old_attribute, attribute, old_attribute_index);
|
||||
}
|
||||
// 5. Otherwise, append attr to element.
|
||||
else {
|
||||
append_attribute(attribute);
|
||||
}
|
||||
|
||||
// 6. Return oldAttr.
|
||||
return old_attribute;
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#concept-element-attributes-replace
|
||||
void NamedNodeMap::replace_attribute(Attribute& old_attribute, Attribute& new_attribute, size_t old_attribute_index)
|
||||
{
|
||||
// 1. Handle attribute changes for oldAttr with oldAttr’s element, oldAttr’s value, and newAttr’s value.
|
||||
// FIXME: The steps to handle an attribute change deal with mutation records and custom element states.
|
||||
// Once those are supported, implement these steps: https://dom.spec.whatwg.org/#handle-attribute-changes
|
||||
|
||||
// 2. Replace oldAttr by newAttr in oldAttr’s element’s attribute list.
|
||||
m_attributes.remove(old_attribute_index);
|
||||
m_attributes.insert(old_attribute_index, new_attribute);
|
||||
|
||||
// 3. Set newAttr’s element to oldAttr’s element.
|
||||
new_attribute.set_owner_element(old_attribute.owner_element());
|
||||
|
||||
// 4 .Set oldAttr’s element to null.
|
||||
old_attribute.set_owner_element(nullptr);
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#concept-element-attributes-append
|
||||
void NamedNodeMap::append_attribute(Attribute& attribute)
|
||||
{
|
||||
// 1. Handle attribute changes for attribute with element, null, and attribute’s value.
|
||||
// FIXME: The steps to handle an attribute change deal with mutation records and custom element states.
|
||||
// Once those are supported, implement these steps: https://dom.spec.whatwg.org/#handle-attribute-changes
|
||||
|
||||
// 2. Append attribute to element’s attribute list.
|
||||
m_attributes.append(attribute);
|
||||
|
||||
// 3. Set attribute’s element to element.
|
||||
attribute.set_owner_element(&m_associated_element);
|
||||
}
|
||||
|
||||
// https://dom.spec.whatwg.org/#concept-element-attributes-remove-by-name
|
||||
Attribute const* NamedNodeMap::remove_attribute(StringView qualified_name)
|
||||
{
|
||||
size_t item_index = 0;
|
||||
|
||||
// 1. Let attr be the result of getting an attribute given qualifiedName and element.
|
||||
auto const* attribute = get_attribute(qualified_name, &item_index);
|
||||
|
||||
// 2. If attr is non-null, then remove attr.
|
||||
if (attribute)
|
||||
m_attributes.remove(item_index);
|
||||
|
||||
// 3. Return attr.
|
||||
return attribute;
|
||||
}
|
||||
|
||||
}
|
63
Userland/Libraries/LibWeb/DOM/NamedNodeMap.h
Normal file
63
Userland/Libraries/LibWeb/DOM/NamedNodeMap.h
Normal file
|
@ -0,0 +1,63 @@
|
|||
/*
|
||||
* Copyright (c) 2021, Tim Flynn <trflynn89@pm.me>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/NonnullRefPtrVector.h>
|
||||
#include <AK/RefCounted.h>
|
||||
#include <AK/String.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <LibWeb/Bindings/Wrappable.h>
|
||||
#include <LibWeb/DOM/ExceptionOr.h>
|
||||
#include <LibWeb/Forward.h>
|
||||
|
||||
namespace Web::DOM {
|
||||
|
||||
// https://dom.spec.whatwg.org/#interface-namednodemap
|
||||
class NamedNodeMap final
|
||||
: public RefCounted<NamedNodeMap>
|
||||
, public Bindings::Wrappable {
|
||||
|
||||
public:
|
||||
using WrapperType = Bindings::NamedNodeMapWrapper;
|
||||
|
||||
static NonnullRefPtr<NamedNodeMap> create(Element const& associated_element);
|
||||
~NamedNodeMap() = default;
|
||||
|
||||
bool is_supported_property_index(u32 index) const;
|
||||
Vector<String> supported_property_names() const;
|
||||
|
||||
size_t length() const { return m_attributes.size(); }
|
||||
bool is_empty() const { return m_attributes.is_empty(); }
|
||||
|
||||
// Methods defined by the spec for JavaScript:
|
||||
Attribute const* item(u32 index) const;
|
||||
Attribute const* get_named_item(StringView qualified_name) const;
|
||||
ExceptionOr<Attribute const*> set_named_item(Attribute& attribute);
|
||||
ExceptionOr<Attribute const*> remove_named_item(StringView qualified_name);
|
||||
|
||||
// Methods defined by the spec for internal use:
|
||||
Attribute* get_attribute(StringView qualified_name, size_t* item_index = nullptr);
|
||||
Attribute const* get_attribute(StringView qualified_name, size_t* item_index = nullptr) const;
|
||||
ExceptionOr<Attribute const*> set_attribute(Attribute& attribute);
|
||||
void replace_attribute(Attribute& old_attribute, Attribute& new_attribute, size_t old_attribute_index);
|
||||
void append_attribute(Attribute& attribute);
|
||||
Attribute const* remove_attribute(StringView qualified_name);
|
||||
|
||||
private:
|
||||
explicit NamedNodeMap(Element const& associated_element);
|
||||
|
||||
Element const& m_associated_element;
|
||||
NonnullRefPtrVector<Attribute> m_attributes;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
namespace Web::Bindings {
|
||||
|
||||
NamedNodeMapWrapper* wrap(JS::GlobalObject&, DOM::NamedNodeMap&);
|
||||
|
||||
}
|
14
Userland/Libraries/LibWeb/DOM/NamedNodeMap.idl
Normal file
14
Userland/Libraries/LibWeb/DOM/NamedNodeMap.idl
Normal file
|
@ -0,0 +1,14 @@
|
|||
[Exposed=Window, LegacyUnenumerableNamedProperties]
|
||||
interface NamedNodeMap {
|
||||
readonly attribute unsigned long length;
|
||||
|
||||
getter Attribute? item(unsigned long index);
|
||||
getter Attribute? getNamedItem(DOMString qualifiedName);
|
||||
// Attribute? getNamedItemNS(DOMString? namespace, DOMString localName);
|
||||
|
||||
[CEReactions] Attribute? setNamedItem(Attribute attr);
|
||||
// [CEReactions] Attribute? setNamedItemNS(Attribute attr);
|
||||
|
||||
[CEReactions] Attribute removeNamedItem(DOMString qualifiedName);
|
||||
// [CEReactions] Attribute removeNamedItemNS(DOMString? namespace, DOMString localName);
|
||||
};
|
|
@ -88,6 +88,7 @@ class EventListener;
|
|||
class EventTarget;
|
||||
class HTMLCollection;
|
||||
class LiveNodeList;
|
||||
class NamedNodeMap;
|
||||
class Node;
|
||||
class NodeList;
|
||||
class ParentNode;
|
||||
|
@ -400,6 +401,7 @@ class MessageChannelWrapper;
|
|||
class MessageEventWrapper;
|
||||
class MessagePortWrapper;
|
||||
class MouseEventWrapper;
|
||||
class NamedNodeMapWrapper;
|
||||
class NodeListWrapper;
|
||||
class NodeWrapper;
|
||||
class PageTransitionEventWrapper;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue