Skip to content

Commit

Permalink
BaseHtmlElement: Rebind Attribute callbacks when cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
Timm Ortloff committed Dec 15, 2022
1 parent 68c821b commit 15ff4e8
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/BaseHtmlElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace ipl\Html;

use ReflectionException;
use RuntimeException;

/**
Expand Down Expand Up @@ -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
*
Expand All @@ -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();
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 15ff4e8

Please sign in to comment.