From 720f7ba7463c67fe205cedfce7bde6a6ba3b14c8 Mon Sep 17 00:00:00 2001 From: Timothy Flynn Date: Fri, 1 Sep 2023 10:42:20 -0400 Subject: [PATCH] LibWeb: Define getting and setting an attribute value --- Userland/Libraries/LibWeb/DOM/Element.cpp | 36 +++++++++++++++++++++++ Userland/Libraries/LibWeb/DOM/Element.h | 5 ++++ 2 files changed, 41 insertions(+) diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index 8e7865a04d..0896b8023e 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -109,6 +109,20 @@ DeprecatedString Element::get_attribute(DeprecatedFlyString const& name) const return attribute->value(); } +// https://dom.spec.whatwg.org/#concept-element-attributes-get-value +DeprecatedString Element::get_attribute_value(DeprecatedFlyString const& local_name, DeprecatedFlyString const& namespace_) const +{ + // 1. Let attr be the result of getting an attribute given namespace, localName, and element. + auto const* attribute = m_attributes->get_attribute_ns(namespace_, local_name); + + // 2. If attr is null, then return the empty string. + if (!attribute) + return DeprecatedString::empty(); + + // 3. Return attr’s value. + return attribute->value(); +} + // https://dom.spec.whatwg.org/#dom-element-getattributenode JS::GCPtr Element::get_attribute_node(DeprecatedFlyString const& name) const { @@ -211,6 +225,28 @@ WebIDL::ExceptionOr Element::set_attribute_ns(DeprecatedFlyString const& n return set_attribute(extracted_qualified_name.local_name(), value); } +// https://dom.spec.whatwg.org/#concept-element-attributes-set-value +void Element::set_attribute_value(DeprecatedFlyString const& local_name, DeprecatedString const& value, DeprecatedFlyString const& prefix, DeprecatedFlyString const& namespace_) +{ + // 1. Let attribute be the result of getting an attribute given namespace, localName, and element. + auto* attribute = m_attributes->get_attribute_ns(namespace_, local_name); + + // 2. If attribute is null, create an attribute whose namespace is namespace, namespace prefix is prefix, local name + // is localName, value is value, and node document is element’s node document, then append this attribute to element, + // and then return. + if (!attribute) { + QualifiedName name { local_name, prefix, namespace_ }; + + auto new_attribute = Attr::create(document(), move(name), value); + m_attributes->append_attribute(new_attribute); + + return; + } + + // 3. Change attribute to value. + attribute->change_attribute(value); +} + // https://dom.spec.whatwg.org/#dom-element-setattributenode WebIDL::ExceptionOr> Element::set_attribute_node(Attr& attr) { diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index 61c9a3b2f2..9e3821ca12 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -90,12 +90,17 @@ public: bool has_attribute(DeprecatedFlyString const& name) const; bool has_attribute_ns(DeprecatedFlyString namespace_, DeprecatedFlyString const& name) const; bool has_attributes() const; + DeprecatedString attribute(DeprecatedFlyString const& name) const { return get_attribute(name); } DeprecatedString get_attribute(DeprecatedFlyString const& name) const; + DeprecatedString get_attribute_value(DeprecatedFlyString const& local_name, DeprecatedFlyString const& namespace_ = {}) const; + virtual WebIDL::ExceptionOr set_attribute(DeprecatedFlyString const& name, DeprecatedString const& value); WebIDL::ExceptionOr set_attribute_ns(DeprecatedFlyString const& namespace_, DeprecatedFlyString const& qualified_name, DeprecatedString const& value); + void set_attribute_value(DeprecatedFlyString const& local_name, DeprecatedString const& value, DeprecatedFlyString const& prefix = {}, DeprecatedFlyString const& namespace_ = {}); WebIDL::ExceptionOr> set_attribute_node(Attr&); WebIDL::ExceptionOr> set_attribute_node_ns(Attr&); + virtual void remove_attribute(DeprecatedFlyString const& name); WebIDL::ExceptionOr toggle_attribute(DeprecatedFlyString const& name, Optional force); size_t attribute_list_size() const;