diff options
author | Andreas Kling <kling@serenityos.org> | 2023-03-10 14:56:29 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-03-10 14:56:29 +0100 |
commit | fb6d6a985f383b545c5143a69beb938bd92bf41f (patch) | |
tree | f7c6d3aea39539f0b1994bc7b900118dab891038 /Userland/Libraries | |
parent | 8c5c78f1f11c43f5c763ba74e3a1ea9db057b6f4 (diff) | |
download | serenity-fb6d6a985f383b545c5143a69beb938bd92bf41f.zip |
LibWeb: Implement Element.setAttributeNode{,NS}()
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Attr.cpp | 5 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Attr.h | 3 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Element.cpp | 16 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Element.h | 2 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/Element.idl | 3 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/NamedNodeMap.cpp | 6 | ||||
-rw-r--r-- | Userland/Libraries/LibWeb/DOM/NamedNodeMap.h | 6 |
7 files changed, 33 insertions, 8 deletions
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<JS::NonnullGCPtr<Attr>> Attr::create(Document& document, Dep return MUST_OR_THROW_OOM(document.heap().allocate<Attr>(document.realm(), document, QualifiedName(move(local_name), {}, {}), move(value), owner_element)); } +WebIDL::ExceptionOr<JS::NonnullGCPtr<Attr>> Attr::create(Document& document, QualifiedName qualified_name, DeprecatedString value, Element const* owner_element) +{ + return MUST_OR_THROW_OOM(document.heap().allocate<Attr>(document.realm(), document, move(qualified_name), move(value), owner_element)); +} + JS::NonnullGCPtr<Attr> Attr::clone(Document& document) { return *heap().allocate<Attr>(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<JS::NonnullGCPtr<Attr>> create(Document&, DeprecatedFlyString local_name, DeprecatedString value, Element const* = nullptr); + static WebIDL::ExceptionOr<JS::NonnullGCPtr<Attr>> create(Document&, QualifiedName, DeprecatedString value = "", Element const* = nullptr); + static WebIDL::ExceptionOr<JS::NonnullGCPtr<Attr>> create(Document&, DeprecatedFlyString local_name, DeprecatedString value = "", Element const* = nullptr); JS::NonnullGCPtr<Attr> 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 <kling@serenityos.org> + * Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org> * * SPDX-License-Identifier: BSD-2-Clause */ @@ -197,6 +197,20 @@ WebIDL::ExceptionOr<void> 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<JS::GCPtr<Attr>> 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<JS::GCPtr<Attr>> 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<void> set_attribute(DeprecatedFlyString const& name, DeprecatedString const& value); WebIDL::ExceptionOr<void> set_attribute_ns(DeprecatedFlyString const& namespace_, DeprecatedFlyString const& qualified_name, DeprecatedString const& value); + WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_attribute_node(Attr&); + WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_attribute_node_ns(Attr&); void remove_attribute(DeprecatedFlyString const& name); WebIDL::ExceptionOr<bool> toggle_attribute(DeprecatedFlyString const& name, Optional<bool> 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<Attr const*> NamedNodeMap::set_named_item(Attr& attribute) +WebIDL::ExceptionOr<JS::GCPtr<Attr>> NamedNodeMap::set_named_item(Attr& attribute) { return set_attribute(attribute); } // https://dom.spec.whatwg.org/#dom-namednodemap-setnameditemns -WebIDL::ExceptionOr<Attr const*> NamedNodeMap::set_named_item_ns(Attr& attribute) +WebIDL::ExceptionOr<JS::GCPtr<Attr>> 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<Attr const*> NamedNodeMap::set_attribute(Attr& attribute) +WebIDL::ExceptionOr<JS::GCPtr<Attr>> 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<Attr const*> set_named_item(Attr& attribute); - WebIDL::ExceptionOr<Attr const*> set_named_item_ns(Attr& attribute); + WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_named_item(Attr& attribute); + WebIDL::ExceptionOr<JS::GCPtr<Attr>> set_named_item_ns(Attr& attribute); WebIDL::ExceptionOr<Attr const*> remove_named_item(StringView qualified_name); WebIDL::ExceptionOr<Attr const*> 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<Attr const*> set_attribute(Attr& attribute); + WebIDL::ExceptionOr<JS::GCPtr<Attr>> 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); |