From 2411dadc35c2cfecef2d532df16625786a96d7e1 Mon Sep 17 00:00:00 2001 From: Kenneth Myhra Date: Wed, 15 Feb 2023 07:44:06 +0100 Subject: [PATCH] LibWeb: Make factory method of DOM::StaticNodeList fallible --- Userland/Libraries/LibWeb/DOM/Attr.cpp | 4 ++- .../Libraries/LibWeb/DOM/CharacterData.cpp | 4 ++- Userland/Libraries/LibWeb/DOM/Node.cpp | 26 ++++++++++++++----- .../Libraries/LibWeb/DOM/StaticNodeList.cpp | 4 +-- .../Libraries/LibWeb/DOM/StaticNodeList.h | 2 +- 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/Userland/Libraries/LibWeb/DOM/Attr.cpp b/Userland/Libraries/LibWeb/DOM/Attr.cpp index 8e8e1bcc2a..915689cbec 100644 --- a/Userland/Libraries/LibWeb/DOM/Attr.cpp +++ b/Userland/Libraries/LibWeb/DOM/Attr.cpp @@ -82,7 +82,9 @@ void Attr::set_value(DeprecatedString value) void Attr::handle_attribute_changes(Element& element, DeprecatedString const& old_value, [[maybe_unused]] DeprecatedString const& new_value) { // 1. Queue a mutation record of "attributes" for element with attribute’s local name, attribute’s namespace, oldValue, « », « », null, and null. - element.queue_mutation_record(MutationType::attributes, local_name(), namespace_uri(), old_value, StaticNodeList::create(realm(), {}), StaticNodeList::create(realm(), {}), nullptr, nullptr); + auto added_node_list = StaticNodeList::create(realm(), {}).release_value_but_fixme_should_propagate_errors(); + auto removed_node_list = StaticNodeList::create(realm(), {}).release_value_but_fixme_should_propagate_errors(); + element.queue_mutation_record(MutationType::attributes, local_name(), namespace_uri(), old_value, added_node_list, removed_node_list, nullptr, nullptr); // FIXME: 2. If element is custom, then enqueue a custom element callback reaction with element, callback name "attributeChangedCallback", and an argument list containing attribute’s local name, oldValue, newValue, and attribute’s namespace. diff --git a/Userland/Libraries/LibWeb/DOM/CharacterData.cpp b/Userland/Libraries/LibWeb/DOM/CharacterData.cpp index a346b8111f..48744202ab 100644 --- a/Userland/Libraries/LibWeb/DOM/CharacterData.cpp +++ b/Userland/Libraries/LibWeb/DOM/CharacterData.cpp @@ -71,7 +71,9 @@ WebIDL::ExceptionOr CharacterData::replace_data(size_t offset, size_t coun count = length - offset; // 4. Queue a mutation record of "characterData" for node with null, null, node’s data, « », « », null, and null. - queue_mutation_record(MutationType::characterData, {}, {}, m_data, StaticNodeList::create(realm(), {}), StaticNodeList::create(realm(), {}), nullptr, nullptr); + auto added_node_list = TRY(StaticNodeList::create(realm(), {})); + auto removed_node_list = TRY(StaticNodeList::create(realm(), {})); + queue_mutation_record(MutationType::characterData, {}, {}, m_data, added_node_list, removed_node_list, nullptr, nullptr); // 5. Insert data into node’s data after offset code units. // 6. Let delete offset be offset + data’s length. diff --git a/Userland/Libraries/LibWeb/DOM/Node.cpp b/Userland/Libraries/LibWeb/DOM/Node.cpp index 04c9dc1146..cf26114c36 100644 --- a/Userland/Libraries/LibWeb/DOM/Node.cpp +++ b/Userland/Libraries/LibWeb/DOM/Node.cpp @@ -391,7 +391,9 @@ void Node::insert_before(JS::NonnullGCPtr node, JS::GCPtr child, boo // 2. Queue a tree mutation record for node with « », nodes, null, and null. // NOTE: This step intentionally does not pay attention to the suppress observers flag. - node->queue_tree_mutation_record(StaticNodeList::create(realm(), {}), StaticNodeList::create(realm(), nodes), nullptr, nullptr); + auto added_node_list = StaticNodeList::create(realm(), {}).release_value_but_fixme_should_propagate_errors(); + auto removed_node_list = StaticNodeList::create(realm(), nodes).release_value_but_fixme_should_propagate_errors(); + node->queue_tree_mutation_record(added_node_list, removed_node_list, nullptr, nullptr); } // 5. If child is non-null, then: @@ -452,8 +454,11 @@ void Node::insert_before(JS::NonnullGCPtr node, JS::GCPtr child, boo } // 8. If suppress observers flag is unset, then queue a tree mutation record for parent with nodes, « », previousSibling, and child. - if (!suppress_observers) - queue_tree_mutation_record(StaticNodeList::create(realm(), move(nodes)), StaticNodeList::create(realm(), {}), previous_sibling.ptr(), child.ptr()); + if (!suppress_observers) { + auto added_node_list = StaticNodeList::create(realm(), move(nodes)).release_value_but_fixme_should_propagate_errors(); + auto removed_node_list = StaticNodeList::create(realm(), {}).release_value_but_fixme_should_propagate_errors(); + queue_tree_mutation_record(added_node_list, removed_node_list, previous_sibling.ptr(), child.ptr()); + } // 9. Run the children changed steps for parent. children_changed(); @@ -605,7 +610,9 @@ void Node::remove(bool suppress_observers) if (!suppress_observers) { Vector> removed_nodes; removed_nodes.append(JS::make_handle(*this)); - parent->queue_tree_mutation_record(StaticNodeList::create(realm(), {}), StaticNodeList::create(realm(), move(removed_nodes)), old_previous_sibling.ptr(), old_next_sibling.ptr()); + auto added_node_list = StaticNodeList::create(realm(), {}).release_value_but_fixme_should_propagate_errors(); + auto removed_node_list = StaticNodeList::create(realm(), move(removed_nodes)).release_value_but_fixme_should_propagate_errors(); + parent->queue_tree_mutation_record(added_node_list, removed_node_list, old_previous_sibling.ptr(), old_next_sibling.ptr()); } // 21. Run the children changed steps for parent. @@ -697,7 +704,9 @@ WebIDL::ExceptionOr> Node::replace_child(JS::NonnullGCPtr insert_before(node, reference_child, true); // 14. Queue a tree mutation record for parent with nodes, removedNodes, previousSibling, and referenceChild. - queue_tree_mutation_record(StaticNodeList::create(realm(), move(nodes)), StaticNodeList::create(realm(), move(removed_nodes)), previous_sibling.ptr(), reference_child.ptr()); + auto added_node_list = TRY(StaticNodeList::create(realm(), move(nodes))); + auto removed_node_list = TRY(StaticNodeList::create(realm(), move(removed_nodes))); + queue_tree_mutation_record(added_node_list, removed_node_list, previous_sibling.ptr(), reference_child.ptr()); // 15. Return child. return child; @@ -1167,8 +1176,11 @@ void Node::replace_all(JS::GCPtr node) insert_before(*node, nullptr, true); // 7. If either addedNodes or removedNodes is not empty, then queue a tree mutation record for parent with addedNodes, removedNodes, null, and null. - if (!added_nodes.is_empty() || !removed_nodes.is_empty()) - queue_tree_mutation_record(StaticNodeList::create(realm(), move(added_nodes)), StaticNodeList::create(realm(), move(removed_nodes)), nullptr, nullptr); + if (!added_nodes.is_empty() || !removed_nodes.is_empty()) { + auto added_node_list = StaticNodeList::create(realm(), move(added_nodes)).release_value_but_fixme_should_propagate_errors(); + auto removed_node_list = StaticNodeList::create(realm(), move(removed_nodes)).release_value_but_fixme_should_propagate_errors(); + queue_tree_mutation_record(added_node_list, removed_node_list, nullptr, nullptr); + } } // https://dom.spec.whatwg.org/#string-replace-all diff --git a/Userland/Libraries/LibWeb/DOM/StaticNodeList.cpp b/Userland/Libraries/LibWeb/DOM/StaticNodeList.cpp index db926c6187..5a863cc175 100644 --- a/Userland/Libraries/LibWeb/DOM/StaticNodeList.cpp +++ b/Userland/Libraries/LibWeb/DOM/StaticNodeList.cpp @@ -9,9 +9,9 @@ namespace Web::DOM { -JS::NonnullGCPtr StaticNodeList::create(JS::Realm& realm, Vector> static_nodes) +WebIDL::ExceptionOr> StaticNodeList::create(JS::Realm& realm, Vector> static_nodes) { - return realm.heap().allocate(realm, realm, move(static_nodes)).release_allocated_value_but_fixme_should_propagate_errors(); + return MUST_OR_THROW_OOM(realm.heap().allocate(realm, realm, move(static_nodes))); } StaticNodeList::StaticNodeList(JS::Realm& realm, Vector> static_nodes) diff --git a/Userland/Libraries/LibWeb/DOM/StaticNodeList.h b/Userland/Libraries/LibWeb/DOM/StaticNodeList.h index d5ef8f896e..472a853672 100644 --- a/Userland/Libraries/LibWeb/DOM/StaticNodeList.h +++ b/Userland/Libraries/LibWeb/DOM/StaticNodeList.h @@ -16,7 +16,7 @@ class StaticNodeList final : public NodeList { WEB_PLATFORM_OBJECT(StaticNodeList, NodeList); public: - static JS::NonnullGCPtr create(JS::Realm&, Vector>); + static WebIDL::ExceptionOr> create(JS::Realm&, Vector>); virtual ~StaticNodeList() override;