From 1e9e2b656420746405c9ca5f41ab6eb072651ed3 Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Sat, 11 Jan 2025 17:56:05 +0000 Subject: [PATCH] LibWeb: Clone all attribute properties when cloning a single node Previously, the namespace of the attributes on the cloned element was not being set. --- Libraries/LibWeb/DOM/Node.cpp | 19 +++++- .../dom/nodes/Node-cloneNode-svg.txt | 9 +++ .../dom/nodes/Node-cloneNode-svg.html | 63 +++++++++++++++++++ 3 files changed, 88 insertions(+), 3 deletions(-) create mode 100644 Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Node-cloneNode-svg.txt create mode 100644 Tests/LibWeb/Text/input/wpt-import/dom/nodes/Node-cloneNode-svg.html diff --git a/Libraries/LibWeb/DOM/Node.cpp b/Libraries/LibWeb/DOM/Node.cpp index 1262e56afdb..24c6dbeb6ba 100644 --- a/Libraries/LibWeb/DOM/Node.cpp +++ b/Libraries/LibWeb/DOM/Node.cpp @@ -1099,11 +1099,24 @@ WebIDL::ExceptionOr> Node::clone_single_node(Document& document) c auto element_copy = TRY(DOM::create_element(document, element.local_name(), element.namespace_uri(), element.prefix(), element.is_value())); // 2. For each attribute of node’s attribute list: - element.for_each_attribute([&](auto& name, auto& value) { - // FIXME: 1. Let copyAttribute be the result of cloning a single node given attribute and document. + Optional maybe_exception; + element.for_each_attribute([&](Attr const& attr) { + // 1. Let copyAttribute be the result of cloning a single node given attribute and document. + auto copy_attribute_or_error = attr.clone_single_node(document); + if (copy_attribute_or_error.is_error()) { + maybe_exception = copy_attribute_or_error.release_error(); + return; + } + + auto copy_attribute = copy_attribute_or_error.release_value(); + // 2. Append copyAttribute to copy. - element_copy->append_attribute(name, value); + element_copy->append_attribute(verify_cast(*copy_attribute)); }); + + if (maybe_exception.has_value()) + return *maybe_exception; + copy = move(element_copy); } diff --git a/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Node-cloneNode-svg.txt b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Node-cloneNode-svg.txt new file mode 100644 index 00000000000..57beb120c7c --- /dev/null +++ b/Tests/LibWeb/Text/expected/wpt-import/dom/nodes/Node-cloneNode-svg.txt @@ -0,0 +1,9 @@ +Harness status: OK + +Found 4 tests + +4 Pass +Pass cloned should have the right properties +Pass cloned 's xmlns:xlink attribute should have the right properties +Pass cloned should have the right properties +Pass cloned 's xlink:href attribute should have the right properties \ No newline at end of file diff --git a/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Node-cloneNode-svg.html b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Node-cloneNode-svg.html new file mode 100644 index 00000000000..9afef0a2506 --- /dev/null +++ b/Tests/LibWeb/Text/input/wpt-import/dom/nodes/Node-cloneNode-svg.html @@ -0,0 +1,63 @@ + + +Cloning of SVG elements and attributes + + + + + + + + + +