From ed081799636f2b9e52f2c23da4648fe4bec35108 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Tue, 12 Dec 2023 12:22:32 +0000 Subject: [PATCH] fix: remove attributes after binding step fixes #478 --- src/HTMLAttributeBinder.php | 8 +++++++- test/phpunit/HTMLAttributeBinderTest.php | 13 +++++++++++++ test/phpunit/TestHelper/HTMLPageContent.php | 7 +++++++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/HTMLAttributeBinder.php b/src/HTMLAttributeBinder.php index 7c98025..397fbe7 100644 --- a/src/HTMLAttributeBinder.php +++ b/src/HTMLAttributeBinder.php @@ -32,6 +32,8 @@ public function bind( $element = $element->documentElement; } + $attributesToRemove = []; + /** * @var string $attrName * @var Attr $attr @@ -80,9 +82,13 @@ public function bind( ); if(!$attr->ownerElement->hasAttribute("data-rebind")) { - $attr->ownerElement->removeAttribute($attrName); + array_push($attributesToRemove, $attrName); } } + + foreach($attributesToRemove as $attrName) { + $element->removeAttribute($attrName); + } } public function expandAttributes(Element $element):void { diff --git a/test/phpunit/HTMLAttributeBinderTest.php b/test/phpunit/HTMLAttributeBinderTest.php index e6d4c87..24682dd 100644 --- a/test/phpunit/HTMLAttributeBinderTest.php +++ b/test/phpunit/HTMLAttributeBinderTest.php @@ -127,4 +127,17 @@ public function testBind_dateTimeInterface():void { $sut->bind(null, $dateTime, $outputElement); self::assertSame("Tue, 05 Apr 1988 17:23:00 GMT", $outputElement->textContent); } + + public function testBind_multipleBindAttributes():void { + $document = new HTMLDocument(HTMLPageContent::HTML_MULTIPLE_BIND_ATTRIBUTES); + $sut = new HTMLAttributeBinder(); + $link1 = $document->getElementById("link1"); + $link2 = $document->getElementById("link2"); + $sut->bind("url", "https://www.php.gt/", $link1); + $sut->bind("url", "https://www.php.gt/", $link2); + self::assertSame("https://www.php.gt/", $link1->textContent); + self::assertSame("https://www.php.gt/", $link2->href); + self::assertSame("https://www.php.gt/", $link1->href); + self::assertSame("https://www.php.gt/", $link2->textContent); + } } diff --git a/test/phpunit/TestHelper/HTMLPageContent.php b/test/phpunit/TestHelper/HTMLPageContent.php index 4adfba9..e178c24 100644 --- a/test/phpunit/TestHelper/HTMLPageContent.php +++ b/test/phpunit/TestHelper/HTMLPageContent.php @@ -1101,6 +1101,13 @@ class HTMLPageContent { HTML; + const HTML_MULTIPLE_BIND_ATTRIBUTES = << +example.com +example.com +HTML; + + public static function createHTML(string $html = ""):HTMLDocument { return new HTMLDocument($html); }