Skip to content

Commit

Permalink
fix: remove attributes after binding step
Browse files Browse the repository at this point in the history
fixes #478
  • Loading branch information
g105b committed Dec 12, 2023
1 parent 5ab216b commit ed08179
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/HTMLAttributeBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public function bind(
$element = $element->documentElement;
}

$attributesToRemove = [];

/**
* @var string $attrName
* @var Attr $attr
Expand Down Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions test/phpunit/HTMLAttributeBinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
7 changes: 7 additions & 0 deletions test/phpunit/TestHelper/HTMLPageContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,13 @@ class HTMLPageContent {
</ul>
HTML;

const HTML_MULTIPLE_BIND_ATTRIBUTES = <<<HTML
<!doctype html>
<a id="link1" data-bind:text="url" data-bind:href="url" target="_blank" href="/">example.com</a>
<a id="link2" data-bind:href="url" data-bind:text="url" target="_blank" href="/">example.com</a>
HTML;


public static function createHTML(string $html = ""):HTMLDocument {
return new HTMLDocument($html);
}
Expand Down

0 comments on commit ed08179

Please sign in to comment.