From 530d5adc62d9383a53d7e888c826affeec270c14 Mon Sep 17 00:00:00 2001 From: Alexander Narsudinov Date: Sat, 17 Dec 2022 17:08:51 +0300 Subject: LibWeb: Add NamedNodeMap::removeNamedItemNS() method This patch adds implementation of the missing `removeNamedItemNS()` method. --- Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp | 30 ++++++++++++++++++++++++++ Userland/Libraries/LibWeb/DOM/NamedNodeMap.h | 2 ++ Userland/Libraries/LibWeb/DOM/NamedNodeMap.idl | 2 +- 3 files changed, 33 insertions(+), 1 deletion(-) (limited to 'Userland/Libraries/LibWeb/DOM') diff --git a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp index 2c64ace03d..f0a4d41a06 100644 --- a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp +++ b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp @@ -106,6 +106,20 @@ WebIDL::ExceptionOr NamedNodeMap::remove_named_item(StringView qual return attribute; } +// https://dom.spec.whatwg.org/#dom-namednodemap-removenameditemns +WebIDL::ExceptionOr NamedNodeMap::remove_named_item_ns(StringView namespace_, StringView local_name) +{ + // 1. Let attr be the result of removing an attribute given namespace, localName, and element. + auto const* attribute = remove_attribute_ns(namespace_, local_name); + + // 2. If attr is null, then throw a "NotFoundError" DOMException. + if (!attribute) + return WebIDL::NotFoundError::create(realm(), DeprecatedString::formatted("Attribute with namespace '{}' and local name '{}' not found", namespace_, local_name)); + + // 3. Return attr. + return attribute; +} + // https://dom.spec.whatwg.org/#concept-element-attributes-get-by-name Attr* NamedNodeMap::get_attribute(StringView qualified_name, size_t* item_index) { @@ -257,6 +271,22 @@ Attr const* NamedNodeMap::remove_attribute(StringView qualified_name) return attribute; } +// https://dom.spec.whatwg.org/#concept-element-attributes-remove-by-namespace +Attr const* NamedNodeMap::remove_attribute_ns(StringView namespace_, StringView local_name) +{ + size_t item_index = 0; + + // 1. Let attr be the result of getting an attribute given namespace, localName, and element. + auto const* attribute = get_attribute_ns(namespace_, local_name, &item_index); + + // 2. If attr is non-null, then remove attr. + if (attribute) + remove_attribute_at_index(item_index); + + // 3. Return attr. + return attribute; +} + JS::Value NamedNodeMap::item_value(size_t index) const { auto const* node = item(index); diff --git a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h index 9e6d25c60c..566a584ebd 100644 --- a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h +++ b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h @@ -39,6 +39,7 @@ public: Attr const* get_named_item_ns(StringView namespace_, StringView local_name) const; WebIDL::ExceptionOr set_named_item(Attr& attribute); WebIDL::ExceptionOr remove_named_item(StringView qualified_name); + WebIDL::ExceptionOr remove_named_item_ns(StringView namespace_, StringView local_name); // Methods defined by the spec for internal use: Attr* get_attribute(StringView qualified_name, size_t* item_index = nullptr); @@ -49,6 +50,7 @@ public: void replace_attribute(Attr& old_attribute, Attr& new_attribute, size_t old_attribute_index); void append_attribute(Attr& attribute); Attr const* remove_attribute(StringView qualified_name); + Attr const* remove_attribute_ns(StringView namespace_, StringView local_name); private: explicit NamedNodeMap(Element&); diff --git a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.idl b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.idl index 8fa192a842..af50fb2511 100644 --- a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.idl +++ b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.idl @@ -12,5 +12,5 @@ interface NamedNodeMap { // [CEReactions] Attr? setNamedItemNS(Attr attr); [CEReactions] Attr removeNamedItem(DOMString qualifiedName); - // [CEReactions] Attr removeNamedItemNS(DOMString? namespace, DOMString localName); + [CEReactions] Attr removeNamedItemNS(DOMString? namespace, DOMString localName); }; -- cgit v1.2.3