Skip to content

Commit

Permalink
Fix for #105 (#121)
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

* Remove unused functions

* Extract templates after expanding components. Closes #105
  • Loading branch information
g105b committed Dec 11, 2019
1 parent 5447e2f commit fcccb78
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 22 deletions.
18 changes: 0 additions & 18 deletions src/HTMLDocument.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,24 +112,6 @@ public function getUnnamedTemplate(
return $clone;
}

public function pushTemplateParentStack(
string $name,
Element $element
):void {
if(!isset($this->templateParentStack[$name])) {
$this->templateParentStack[$name] = [];
}

$this->templateParentStack[$name] []= $element;
}

public function insertTemplateParentStack():void {
ksort($this->templateParentStack);
foreach($this->templateParentStack as $name => $elementList) {

}
}

public function getParentOfUnnamedTemplate(
Element $element,
bool $requireMatchingPath = false
Expand Down
1 change: 0 additions & 1 deletion src/TemplateParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ protected function loadComponent(string $name, string $path):DocumentFragment {
}

$fragment->appendHTML($html);
$fragment->extractTemplates();
return $fragment;
}

Expand Down
16 changes: 16 additions & 0 deletions test/unit/Helper/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,22 @@ class Helper {
<li>This is an item in the list</li>
HTML;

const COMPONENT_WITH_TEMPLATE = <<<HTML
<ul>
<li data-template data-bind:text>Test</li>
</ul>
HTML;

const HTML_SIMPLE_LIST_COMPONENT = <<<HTML
<!doctype html>
<meta charset="utf-8" />
<title>Simple componentised list test</title>
<main>
<simple-list></simple-list>
</main>
HTML;


const HTML_BIND_UNKNOWN_PROPERTY = <<<HTML
<!doctype html>
<meta charset="utf-8" />
Expand Down
32 changes: 29 additions & 3 deletions test/unit/TemplateParentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ public function testComponentWithinTemplate() {
Helper::HTML_TEMPLATE_WITH_NESTED_COMPONENT,
$templateDir
);
$document->extractTemplates();
$document->expandComponents();
$document->extractTemplates();

self::assertCount(
0,
Expand Down Expand Up @@ -243,8 +243,7 @@ public function testNestedComponentsExpandWhenTemplateInserted() {
$templateDir
);
$document->expandComponents();

$document->getTemplate("title-definition-item")->insertTemplate();
$document->extractTemplates();

$section = $document->querySelector("section");
$ol = $section->lastElementChild;
Expand Down Expand Up @@ -420,4 +419,31 @@ public function testExistingClassesAreAddedToComponents() {
$component->classList->contains("source-class")
);
}

/** @link https://github.com/PhpGt/DomTemplate/issues/105 */
public function testTemplateWithinComponentIsAddedCorrectly() {
$componentDir = self::TEST_DIR . "/" . self::COMPONENT_PATH;
file_put_contents(
"$componentDir/simple-list.html",
Helper::COMPONENT_WITH_TEMPLATE
);

$document = new HTMLDocument(
Helper::HTML_SIMPLE_LIST_COMPONENT,
$componentDir
);
$document->expandComponents();
$document->extractTemplates();

$componentElement = $document->querySelector(".c-simple-list");
self::assertCount(0, $componentElement->children);

$componentElement->bindList([
"one",
"two",
"three",
]);

self::assertCount(3, $componentElement->children);
}
}

0 comments on commit fcccb78

Please sign in to comment.