diff --git a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp index 5146580e19..e121b3f1b9 100644 --- a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp +++ b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp @@ -1,6 +1,7 @@ /* * Copyright (c) 2021, Tim Flynn * Copyright (c) 2022, Andreas Kling + * Copyright (c) 2022, Alexander Narsudinov * * SPDX-License-Identifier: BSD-2-Clause */ @@ -132,6 +133,33 @@ Attr const* NamedNodeMap::get_attribute(StringView qualified_name, size_t* item_ return nullptr; } +// https://dom.spec.whatwg.org/#concept-element-attributes-get-by-namespace +Attr* NamedNodeMap::get_attribute_ns(StringView namespace_, StringView local_name, size_t* item_index) +{ + return const_cast(const_cast(this)->get_attribute_ns(namespace_, local_name, item_index)); +} + +// https://dom.spec.whatwg.org/#concept-element-attributes-get-by-namespace +Attr const* NamedNodeMap::get_attribute_ns(StringView namespace_, StringView local_name, size_t* item_index) const +{ + if (item_index) + *item_index = 0; + + // 1. If namespace is the empty string, then set it to null. + if (namespace_.is_empty()) + namespace_ = {}; + + // 2. Return the attribute in element’s attribute list whose namespace is namespace and local name is localName, if any; otherwise null. + for (auto const& attribute : m_attributes) { + if (attribute->namespace_uri() == namespace_ && attribute->local_name() == local_name) + return attribute.ptr(); + if (item_index) + ++(*item_index); + } + + return nullptr; +} + // https://dom.spec.whatwg.org/#concept-element-attributes-set WebIDL::ExceptionOr NamedNodeMap::set_attribute(Attr& attribute) { diff --git a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h index bc2f2a1f84..b79e65a12f 100644 --- a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h +++ b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h @@ -1,6 +1,7 @@ /* * Copyright (c) 2021, Tim Flynn * Copyright (c) 2022, Andreas Kling + * Copyright (c) 2022, Alexander Narsudinov * * SPDX-License-Identifier: BSD-2-Clause */ @@ -40,7 +41,9 @@ public: // Methods defined by the spec for internal use: Attr* get_attribute(StringView qualified_name, size_t* item_index = nullptr); + Attr* get_attribute_ns(StringView namespace_, StringView local_name, size_t* item_index = nullptr); Attr const* get_attribute(StringView qualified_name, size_t* item_index = nullptr) const; + Attr const* get_attribute_ns(StringView namespace_, StringView local_name, size_t* item_index = nullptr) const; WebIDL::ExceptionOr set_attribute(Attr& attribute); void replace_attribute(Attr& old_attribute, Attr& new_attribute, size_t old_attribute_index); void append_attribute(Attr& attribute);