Skip to content

Commit

Permalink
Remove element's child nodes when empty dataset is bound as a list (#125
Browse files Browse the repository at this point in the history
)

* Fix return type for getElementById, closes #65

* Update PHPDoc to include inherited changes

* Rename function for removing template attributes

* Set value for certain input fields differently

* Bind list to document fragment for efficiency
for #77

* Remove unbindable check

* Allow returning any iterable, not just array - fixes #88

* Test that multiple kvps can be bound within the same parameter

* Add failing test for #124

* Empty element's child nodes when binding empty dataset
Closes #124
  • Loading branch information
g105b committed Jan 15, 2020
1 parent fcccb78 commit ec45af4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 31 deletions.
61 changes: 31 additions & 30 deletions composer.lock

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

4 changes: 4 additions & 0 deletions src/Bindable.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,10 @@ public function bindList(
string $templateName = null
):int {
if(empty($kvpList)) {
if($this->children->length === 0) {
$this->innerHTML = "";
}

return 0;
}

Expand Down
10 changes: 9 additions & 1 deletion test/unit/BindableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ public function testTemplateNameIsAddedWhenNamed() {

foreach(["one", "two", "three"] as $num) {
$t = $document->getTemplate("list-item");
$t->innerText = $num;
$t->innerHTML = $num;
$inserted = $t->insertTemplate();

self::assertStringContainsString("t-list-item", $inserted->getAttribute("class"));
Expand Down Expand Up @@ -651,4 +651,12 @@ public function testBindKeyValueBool() {
self::assertFalse($listItems[0]->classList->contains("completed"));
self::assertTrue($listItems[1]->classList->contains("completed"));
}

public function testBindListEmptySetsInnerHtmlToEmpty() {
$document = new HTMLDocument(Helper::HTML_TODO_LIST);
$document->extractTemplates();
$ul = $document->getElementById("todo-list");
$ul->bindList([]);
self::assertEquals("", $ul->innerHTML);
}
}

0 comments on commit ec45af4

Please sign in to comment.