Ver código fonte

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.
Alexander Narsudinov 2 anos atrás
pai
commit
1a0fbe1e85

+ 28 - 0
Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp

@@ -1,6 +1,7 @@
 /*
  * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2022, Alexander Narsudinov <a.narsudinov@gmail.com>
  *
  * 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<Attr*>(const_cast<NamedNodeMap const*>(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<Attr const*> NamedNodeMap::set_attribute(Attr& attribute)
 {

+ 3 - 0
Userland/Libraries/LibWeb/DOM/NamedNodeMap.h

@@ -1,6 +1,7 @@
 /*
  * Copyright (c) 2021, Tim Flynn <trflynn89@serenityos.org>
  * Copyright (c) 2022, Andreas Kling <kling@serenityos.org>
+ * Copyright (c) 2022, Alexander Narsudinov <a.narsudinov@gmail.com>
  *
  * 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<Attr const*> set_attribute(Attr& attribute);
     void replace_attribute(Attr& old_attribute, Attr& new_attribute, size_t old_attribute_index);
     void append_attribute(Attr& attribute);