diff --git a/Userland/Libraries/LibWeb/DOM/Attr.cpp b/Userland/Libraries/LibWeb/DOM/Attr.cpp index fc5ce191f6..527a07d7cd 100644 --- a/Userland/Libraries/LibWeb/DOM/Attr.cpp +++ b/Userland/Libraries/LibWeb/DOM/Attr.cpp @@ -18,6 +18,11 @@ WebIDL::ExceptionOr> Attr::create(Document& document, Dep return MUST_OR_THROW_OOM(document.heap().allocate(document.realm(), document, QualifiedName(move(local_name), {}, {}), move(value), owner_element)); } +WebIDL::ExceptionOr> Attr::create(Document& document, QualifiedName qualified_name, DeprecatedString value, Element const* owner_element) +{ + return MUST_OR_THROW_OOM(document.heap().allocate(document.realm(), document, move(qualified_name), move(value), owner_element)); +} + JS::NonnullGCPtr Attr::clone(Document& document) { return *heap().allocate(realm(), document, m_qualified_name, m_value, nullptr).release_allocated_value_but_fixme_should_propagate_errors(); diff --git a/Userland/Libraries/LibWeb/DOM/Attr.h b/Userland/Libraries/LibWeb/DOM/Attr.h index d3d88bb69d..ee94fd802c 100644 --- a/Userland/Libraries/LibWeb/DOM/Attr.h +++ b/Userland/Libraries/LibWeb/DOM/Attr.h @@ -18,7 +18,8 @@ class Attr final : public Node { WEB_PLATFORM_OBJECT(Attr, Node); public: - static WebIDL::ExceptionOr> create(Document&, DeprecatedFlyString local_name, DeprecatedString value, Element const* = nullptr); + static WebIDL::ExceptionOr> create(Document&, QualifiedName, DeprecatedString value = "", Element const* = nullptr); + static WebIDL::ExceptionOr> create(Document&, DeprecatedFlyString local_name, DeprecatedString value = "", Element const* = nullptr); JS::NonnullGCPtr clone(Document&); virtual ~Attr() override = default; diff --git a/Userland/Libraries/LibWeb/DOM/Element.cpp b/Userland/Libraries/LibWeb/DOM/Element.cpp index 9bb5861080..7b947883b5 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.cpp +++ b/Userland/Libraries/LibWeb/DOM/Element.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2018-2022, Andreas Kling + * Copyright (c) 2018-2023, Andreas Kling * * SPDX-License-Identifier: BSD-2-Clause */ @@ -197,6 +197,20 @@ WebIDL::ExceptionOr Element::set_attribute_ns(DeprecatedFlyString const& n return set_attribute(extracted_qualified_name.local_name(), value); } +// https://dom.spec.whatwg.org/#dom-element-setattributenode +WebIDL::ExceptionOr> Element::set_attribute_node(Attr& attr) +{ + // The setAttributeNode(attr) and setAttributeNodeNS(attr) methods steps are to return the result of setting an attribute given attr and this. + return m_attributes->set_attribute(attr); +} + +// https://dom.spec.whatwg.org/#dom-element-setattributenodens +WebIDL::ExceptionOr> Element::set_attribute_node_ns(Attr& attr) +{ + // The setAttributeNode(attr) and setAttributeNodeNS(attr) methods steps are to return the result of setting an attribute given attr and this. + return m_attributes->set_attribute(attr); +} + // https://dom.spec.whatwg.org/#dom-element-removeattribute void Element::remove_attribute(DeprecatedFlyString const& name) { diff --git a/Userland/Libraries/LibWeb/DOM/Element.h b/Userland/Libraries/LibWeb/DOM/Element.h index e64d1de6dc..db40cf0c21 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.h +++ b/Userland/Libraries/LibWeb/DOM/Element.h @@ -71,6 +71,8 @@ public: DeprecatedString get_attribute(DeprecatedFlyString const& name) const; 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); + WebIDL::ExceptionOr> set_attribute_node(Attr&); + WebIDL::ExceptionOr> set_attribute_node_ns(Attr&); void remove_attribute(DeprecatedFlyString const& name); WebIDL::ExceptionOr toggle_attribute(DeprecatedFlyString const& name, Optional force); size_t attribute_list_size() const { return m_attributes->length(); } diff --git a/Userland/Libraries/LibWeb/DOM/Element.idl b/Userland/Libraries/LibWeb/DOM/Element.idl index 6614c8c841..c933976cf1 100644 --- a/Userland/Libraries/LibWeb/DOM/Element.idl +++ b/Userland/Libraries/LibWeb/DOM/Element.idl @@ -30,6 +30,9 @@ interface Element : Node { DOMString? getAttribute(DOMString qualifiedName); undefined setAttribute(DOMString qualifiedName, DOMString value); [CEReactions] undefined setAttributeNS(DOMString? namespace , DOMString qualifiedName , DOMString value); + [CEReactions] Attr? setAttributeNode(Attr attr); + [CEReactions] Attr? setAttributeNodeNS(Attr attr); + undefined removeAttribute(DOMString qualifiedName); [CEReactions] boolean toggleAttribute(DOMString qualifiedName, optional boolean force); boolean hasAttribute(DOMString qualifiedName); diff --git a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp index 04b9c62d1f..9219add054 100644 --- a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp +++ b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp @@ -95,13 +95,13 @@ Attr const* NamedNodeMap::get_named_item_ns(StringView namespace_, StringView lo } // https://dom.spec.whatwg.org/#dom-namednodemap-setnameditem -WebIDL::ExceptionOr NamedNodeMap::set_named_item(Attr& attribute) +WebIDL::ExceptionOr> NamedNodeMap::set_named_item(Attr& attribute) { return set_attribute(attribute); } // https://dom.spec.whatwg.org/#dom-namednodemap-setnameditemns -WebIDL::ExceptionOr NamedNodeMap::set_named_item_ns(Attr& attribute) +WebIDL::ExceptionOr> NamedNodeMap::set_named_item_ns(Attr& attribute) { return set_attribute(attribute); } @@ -195,7 +195,7 @@ Attr const* NamedNodeMap::get_attribute_ns(StringView namespace_, StringView loc } // https://dom.spec.whatwg.org/#concept-element-attributes-set -WebIDL::ExceptionOr NamedNodeMap::set_attribute(Attr& attribute) +WebIDL::ExceptionOr> NamedNodeMap::set_attribute(Attr& attribute) { // 1. If attr’s element is neither null nor element, throw an "InUseAttributeError" DOMException. if ((attribute.owner_element() != nullptr) && (attribute.owner_element() != &associated_element())) diff --git a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h index af46bd3475..9c6cbe7356 100644 --- a/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h +++ b/Userland/Libraries/LibWeb/DOM/NamedNodeMap.h @@ -37,8 +37,8 @@ public: Attr const* item(u32 index) const; Attr const* get_named_item(StringView qualified_name) const; Attr const* get_named_item_ns(StringView namespace_, StringView local_name) const; - WebIDL::ExceptionOr set_named_item(Attr& attribute); - WebIDL::ExceptionOr set_named_item_ns(Attr& attribute); + WebIDL::ExceptionOr> set_named_item(Attr& attribute); + WebIDL::ExceptionOr> set_named_item_ns(Attr& attribute); WebIDL::ExceptionOr remove_named_item(StringView qualified_name); WebIDL::ExceptionOr remove_named_item_ns(StringView namespace_, StringView local_name); @@ -47,7 +47,7 @@ public: 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 set_attribute(Attr& attribute); + WebIDL::ExceptionOr> set_attribute(Attr& attribute); 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);