Skip to content

Lightpanda: the headless browser designed for AI and automation

dom: detach cloned Attr from its owner element

Adrià Arrufat51974bb34692
diff --git a/src/browser/tests/element/attributes.html b/src/browser/tests/element/attributes.html
index 0929a3d9e..0192c9347 100644
--- a/src/browser/tests/element/attributes.html
+++ b/src/browser/tests/element/attributes.html
@@ -266,6 +266,41 @@
}
</script>
+<div id=clone-src class=c data-x=1></div>
+<div id=clone-dst></div>
+<script id=attributeNodeClone>
+ {
+ const src = $('#clone-src');
+ const dst = $('#clone-dst');
+
+ const original = src.getAttributeNode('data-x');
+ const clone = original.cloneNode();
+ testing.expectEqual('data-x', clone.name);
+ testing.expectEqual('1', clone.value);
+ testing.expectEqual(null, clone.ownerElement);
+ testing.expectEqual(src, original.ownerElement);
+
+ testing.expectEqual(null, dst.setAttributeNode(clone));
+ testing.expectEqual(dst, clone.ownerElement);
+ testing.expectEqual('1', dst.getAttribute('data-x'));
+ testing.expectEqual('1', src.getAttribute('data-x'));
+
+ testing.withError((err) => {
+ testing.expectEqual(10, err.code);
+ testing.expectEqual('InUseAttributeError', err.name);
+ }, () => dst.setAttributeNode(src.getAttributeNode('class')));
+ testing.expectEqual(src, src.getAttributeNode('class').ownerElement);
+ testing.expectEqual(false, dst.hasAttribute('class'));
+
+ const replacement = original.cloneNode();
+ replacement.value = '2';
+ testing.expectEqual(clone, dst.setAttributeNode(replacement));
+ testing.expectEqual(null, clone.ownerElement);
+ testing.expectEqual(dst, replacement.ownerElement);
+ testing.expectEqual('2', dst.getAttribute('data-x'));
+ }
+</script>
+
<div id="nsa"></div>
<script id=non-string-attr>
{
diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zig
index f10dc128c..13cd1a12e 100644
--- a/src/browser/webapi/Element.zig
+++ b/src/browser/webapi/Element.zig
@@ -925,8 +925,7 @@ pub fn setAttributeNode(self: *Element, attr: *Attribute, frame: *Frame) !?*Attr
if (el == self) {
return attr;
}
- attr._element = null;
- _ = try el.removeAttributeNode(attr, frame);
+ return error.InUseAttribute;
}
return self._attributes.putAttribute(attr, self, frame);
diff --git a/src/browser/webapi/element/Attribute.zig b/src/browser/webapi/element/Attribute.zig
index c5486490b..914fe0123 100644
--- a/src/browser/webapi/element/Attribute.zig
+++ b/src/browser/webapi/element/Attribute.zig
@@ -90,7 +90,7 @@ pub fn isEqualNode(self: *const Attribute, other: *const Attribute) bool {
pub fn clone(self: *const Attribute, frame: *Frame) !*Attribute {
return frame._factory.node(Attribute{
- ._element = self._element,
+ ._element = null,
._name = self._name,
._value = self._value,
});