From cee10c9a8251a288b2310855a067939038b26747 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Thu, 9 Feb 2023 23:01:36 +0100 Subject: [PATCH] Deep rebind attribute callbacks after clone --- src/BaseHtmlElement.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/BaseHtmlElement.php b/src/BaseHtmlElement.php index 0bc86f8d..f346be2b 100644 --- a/src/BaseHtmlElement.php +++ b/src/BaseHtmlElement.php @@ -395,6 +395,21 @@ public function renderUnwrapped() public function __clone() { + $copy = $this->copy(); + if ($copy !== null) { + $copy->then(function (SplObjectStorage $copies): void { + foreach ($copies as $ignored) { + // SplObjectMap always iterates over the attached objects that + // are the elements before they have been cloned. + // But we want to work with the cloned element. + $copy = $copies->getInfo(); + if ($copy instanceof self) { + $copy->getAttributes()->rebindAttributeCallbacks($this->objectId(), $this); + } + } + }); + } + parent::__clone(); if ($this->attributes !== null) {