From 45b63b463ad62f9e554b4f2b14e990ce3440defc Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Fri, 10 Nov 2023 10:04:18 +1300 Subject: [PATCH] LibWeb: Add Element::for_each_attribute(Function) This is useful when you are wanting to interate over attributes and need more information than just the name and value. --- Userland/Libraries/LibWeb/DOM/Element.cpp | 13 +++++++++---- Userland/Libraries/LibWeb/DOM/Element.h | 2 ++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index c2ba81f298..cd77eed9d1 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -1885,12 +1885,17 @@ void Element::set_prefix(Optional value) m_qualified_name.set_prefix(move(value)); } +void Element::for_each_attribute(Function callback) const +{ + for (size_t i = 0; i < m_attributes->length(); ++i) + callback(*m_attributes->item(i)); +} + void Element::for_each_attribute(Function callback) const { - for (size_t i = 0; i < m_attributes->length(); ++i) { - auto const* attribute = m_attributes->item(i); - callback(attribute->name(), attribute->value().to_deprecated_string()); - } + for_each_attribute([&callback](Attr const& attr) { + callback(attr.name(), attr.value().to_deprecated_string()); + }); } Layout::NodeWithStyle* Element::layout_node() diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index f40114b7bf..9daefee8fd 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -137,6 +137,8 @@ public: int client_width() const; int client_height() const; + void for_each_attribute(Function) const; + void for_each_attribute(Function) const; bool has_class(FlyString const&, CaseSensitivity = CaseSensitivity::CaseSensitive) const;