From 1a0fbe1e857018b3dc77c8319c54529cae9ffc90 Mon Sep 17 00:00:00 2001 From: Alexander Narsudinov Date: Fri, 16 Dec 2022 17:31:56 +0300 Subject: [PATCH] LibWeb: Add internal get_attribute_ns() methods of NamedNodeMap This patch adds methods for querying element by namespace and local name. These methods are defined by the spec for internal usage, but weren't implemented in LibWeb yet. --- .../Libraries/LibWeb/DOM/NamedNodeMap.cpp | 28 +++++++++++++++++++ Userland/Libraries/LibWeb/DOM/NamedNodeMap.h | 3 ++ 2 files changed, 31 insertions(+) diff --git a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp index 5146580e19d..e121b3f1b9a 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 bc2f2a1f842..b79e65a12f2 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);