From 1812221a2d513811b15ab00b881d123675c9950d Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sun, 5 Nov 2023 11:11:01 +1300 Subject: [PATCH] LibWeb: Add FlyString versions of NamedNodeMap::get_attribute_ns This is the API for NamedNodeMap which we are wanting to eventually use instead of taking a StringView. Currently we just end up deferring to the StringView versions of these functions, but at some stage in the future, this will allow us to have O(1) comparison when making attribute lookups. In the meantime, the advantage of this API is that it makes it much less awkward to use than the StringView variant when you have an Optional namespace to pass through. --- Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp | 17 +++++++++++++++++ Userland/Libraries/LibWeb/DOM/NamedNodeMap.h | 3 +++ 2 files changed, 20 insertions(+) diff --git a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp index fb919fadf6..ecfa0cadf2 100644 --- a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp +++ b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp @@ -176,6 +176,23 @@ 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 const* NamedNodeMap::get_attribute_ns(Optional const& namespace_, FlyString const& local_name, size_t* item_index) const +{ + // FIXME: We shouldn't need to do any conversion when looking up in the node map. + StringView namespace_view; + if (namespace_.has_value()) + namespace_view = namespace_->bytes_as_string_view(); + + return get_attribute_ns(namespace_view, local_name, item_index); +} + +// https://dom.spec.whatwg.org/#concept-element-attributes-get-by-namespace +Attr* NamedNodeMap::get_attribute_ns(Optional const& namespace_, FlyString const& 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* NamedNodeMap::get_attribute_ns(StringView namespace_, StringView local_name, size_t* item_index) { diff --git a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h index 82fd1d5af2..3c1591f97e 100644 --- a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h +++ b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h @@ -52,6 +52,9 @@ public: void replace_attribute(Attr& old_attribute, Attr& new_attribute, size_t old_attribute_index); void append_attribute(Attr& attribute); + Attr* get_attribute_ns(Optional const& namespace_, FlyString const& local_name, size_t* item_index = nullptr); + Attr const* get_attribute_ns(Optional const& namespace_, FlyString const& local_name, size_t* item_index = nullptr) const; + // FIXME: This should take a 'FlyString cosnt&' Attr const* remove_attribute(StringView qualified_name); Attr const* remove_attribute_ns(StringView namespace_, StringView local_name);