Skip to content

Commit

Permalink
feature: @ bind key synonymous for name (#493)
Browse files Browse the repository at this point in the history
* feature: @ bind key synonymous for name
closes #476

* ci: upgrade ci to v4 phpunit
  • Loading branch information
g105b authored Apr 26, 2024
1 parent 716acb1 commit 0cc70a4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ jobs:
run: tar -xvf /tmp/github-actions/build.tar ./

- name: PHP Unit tests
uses: php-actions/phpunit@v3
uses: php-actions/phpunit@v4
env:
XDEBUG_MODE: cover
with:
Expand Down
9 changes: 7 additions & 2 deletions src/HTMLAttributeBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function bind(
// skip this attribute.
$trimmedAttrValue = ltrim($attrValue, ":!?");
$trimmedAttrValue = strtok($trimmedAttrValue, " ");
if($key !== $trimmedAttrValue) {
if($key !== $trimmedAttrValue && $trimmedAttrValue !== "@") {
continue;
}
if($attrValue !== $trimmedAttrValue) {
Expand Down Expand Up @@ -126,7 +126,12 @@ public function expandAttributes(Element $element):void {
}

if($attrValue[0] === "@") {
$otherAttrName = substr($attrValue, 1);
if($attrValue === "@") {
$otherAttrName = "name";
}
else {
$otherAttrName = substr($attrValue, 1);
}
$element->setAttribute(
$attrName,
$element->getAttribute($otherAttrName)
Expand Down
12 changes: 12 additions & 0 deletions test/phpunit/HTMLAttributeBinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,16 @@ public function testBind_multipleAttributes():void {
self::assertSame("value1", $outputElement->dataset->get("attr1"));
self::assertSame("value2", $outputElement->dataset->get("attr2"));
}

public function testExpandAttributes_atCharacter():void {
$document = new HTMLDocument(HTMLPageContent::HTML_BASIC_FORM_WITH_AT_BINDER);
$sut = new HTMLAttributeBinder();
$fromInput = $document->querySelector("input[name=from]");
$toInput = $document->querySelector("input[name=to]");
$sut->bind("from", "London", $fromInput);
$sut->bind("to", "Derby", $toInput);

self::assertSame("London", $fromInput->value);
self::assertSame("Derby", $toInput->value);
}
}
10 changes: 10 additions & 0 deletions test/phpunit/TestHelper/HTMLPageContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,16 @@ class HTMLPageContent {
</ul>
HTML;

const HTML_BASIC_FORM_WITH_AT_BINDER = <<<HTML
<!doctype html>
<form>
<input name="from" data-bind:value="@" />
<input name="to" data-bind:value="@" />
</form>
HTML;


const HTML_SALES = <<<HTML
<!doctype html>
<h1>Sales</h1>
Expand Down

0 comments on commit 0cc70a4

Please sign in to comment.