Skip to content

Commit

Permalink
317 select value (#318)
Browse files Browse the repository at this point in the history
* feature: implement select value binding
closes #317

* stan: improve lvl 6 checks

* build: bump dependencies

* stan: fix option class
  • Loading branch information
g105b authored Jan 30, 2022
1 parent a039790 commit 822e7b3
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 22 deletions.
40 changes: 20 additions & 20 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion src/HTMLAttributeBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Gt\Dom\DOMTokenList;
use Gt\Dom\Element;
use Gt\Dom\Facade\DOMTokenListFactory;
use Gt\Dom\HTMLElement\HTMLOptionElement;
use Gt\Dom\HTMLElement\HTMLSelectElement;

class HTMLAttributeBinder {
private TableBinder $tableBinder;
Expand Down Expand Up @@ -181,7 +183,22 @@ private function setBindProperty(
);
}
else {
$element->setAttribute($bindProperty, $bindValue);
if($element instanceof HTMLSelectElement
&& $bindProperty === "value") {
/** @var HTMLOptionElement $option */
foreach($element->options as $option) {
$optionValue = $option->value;
if($bindValue == $optionValue) {
$option->selected = true;
}
else {
$option->selected = false;
}
}
}
else {
$element->setAttribute($bindProperty, $bindValue);
}
}

break;
Expand Down
2 changes: 1 addition & 1 deletion src/TemplateElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public function removeOriginalElement():void {
}

public function getClone():Element {
/** @var Element $element */
/** @noinspection PhpUnnecessaryLocalVariableInspection */
/** @var Element $element */
$element = $this->originalElement->cloneNode(true);
return $element;
}
Expand Down
54 changes: 54 additions & 0 deletions test/phpunit/HTMLAttributeBinderTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace Gt\DomTemplate\Test;

use Gt\Dom\HTMLElement\HTMLOptionElement;
use Gt\Dom\HTMLElement\HTMLSelectElement;
use Gt\DomTemplate\HTMLAttributeBinder;
use Gt\DomTemplate\Test\TestFactory\DocumentTestFactory;
use PHPUnit\Framework\TestCase;
Expand All @@ -12,4 +14,56 @@ public function testBind_wholeDocument():void {
$sut->bind("language", "en_GB", $document);
self::assertSame("en_GB", $document->documentElement->getAttribute("lang"));
}

public function testBind_selectValue():void {
$document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_SELECT_OPTIONS_WITH_VALUE);
$select = $document->querySelector("select[name='drink']");
$sut = new HTMLAttributeBinder();
$valueToSelect = "tea";
$sut->bind("drink", $valueToSelect, $select);

/** @var HTMLOptionElement $option */
foreach($document->querySelectorAll("select option") as $option) {
$value = $option->getAttribute("value");
if($value === $valueToSelect) {
self::assertTrue($option->hasAttribute("selected"));
}
else {
self::assertFalse($option->hasAttribute("selected"));
}
}
}

public function testBind_selectValue_noOptions():void {
$document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_SELECT_OPTIONS_WITHOUT_VALUE);
$select = $document->querySelector("select[name='drink']");
$sut = new HTMLAttributeBinder();
$valueToSelect = "Tea";
$sut->bind("drink", $valueToSelect, $select);

/** @var HTMLOptionElement $option */
foreach($document->querySelectorAll("select option") as $option) {
if($option->value === $valueToSelect) {
self::assertTrue($option->hasAttribute("selected"));
}
else {
self::assertFalse($option->hasAttribute("selected"));
}
}
}

public function testBind_selectValue_optionDoesNotExist():void {
$document = DocumentTestFactory::createHTML(DocumentTestFactory::HTML_SELECT_OPTIONS_WITHOUT_VALUE);
/** @var HTMLSelectElement $select */
$select = $document->querySelector("select[name='drink']");
$sut = new HTMLAttributeBinder();
$valueToSelect = "Grape Juice";
$select->options[2]->selected = true;
$sut->bind("drink", $valueToSelect, $select);

/** @var HTMLOptionElement $option */
foreach($document->querySelectorAll("select option") as $option) {
self::assertFalse($option->hasAttribute("selected"));
}
}
}
34 changes: 34 additions & 0 deletions test/phpunit/TestFactory/DocumentTestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,40 @@ class DocumentTestFactory {
</main>
HTML;

const HTML_SELECT_OPTIONS_WITH_VALUE = <<<HTML
<!doctype html>
<form>
<label>
<span>Select your drink preference</span>
<select name="drink" data-bind:value="drink">
<option></option>
<option value="coffee">Coffee</option>
<option value="tea">Tea</option>
<option value="chocolate">Chocolate</option>
<option value="soda">Soda</option>
<option value="water">Water</option>
</select>
</label>
</form>
HTML;

const HTML_SELECT_OPTIONS_WITHOUT_VALUE = <<<HTML
<!doctype html>
<form>
<label>
<span>Select your drink preference</span>
<select name="drink" data-bind:value="drink">
<option></option>
<option>Coffee</option>
<option>Tea</option>
<option>Chocolate</option>
<option>Soda</option>
<option>Water</option>
</select>
</label>
</form>
HTML;


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

0 comments on commit 822e7b3

Please sign in to comment.