From c5c51abaae7d601dcd6219c8a2f99c643eb9ce93 Mon Sep 17 00:00:00 2001 From: Timm Ortloff Date: Thu, 15 Dec 2022 17:22:35 +0100 Subject: [PATCH] BaseHtmlElement: Rebind Attribute callbacks when cloning --- src/BaseHtmlElement.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/BaseHtmlElement.php b/src/BaseHtmlElement.php index 41b6e4b5..ddc84107 100644 --- a/src/BaseHtmlElement.php +++ b/src/BaseHtmlElement.php @@ -2,6 +2,7 @@ namespace ipl\Html; +use ReflectionException; use RuntimeException; /** @@ -75,6 +76,9 @@ abstract class BaseHtmlElement extends HtmlDocument /** @var string Tag of element. Set this property in order to provide the element's tag when extending this class */ protected $tag; + /** @var self */ + private $selfRef; + /** * Get the attributes of the element * @@ -83,6 +87,8 @@ abstract class BaseHtmlElement extends HtmlDocument public function getAttributes() { if ($this->attributes === null) { + $this->selfRef = $this; + $default = $this->getDefaultAttributes(); if (empty($default)) { $this->attributes = new Attributes(); @@ -353,12 +359,19 @@ public function renderUnwrapped() ); } + /** + * @throws ReflectionException + */ public function __clone() { parent::__clone(); if ($this->attributes !== null) { $this->attributes = clone $this->attributes; + + $this->attributes->rebind($this->selfRef, $this); } + + $this->selfRef = $this; } }