Skip to content

Commit

Permalink
feature: trim whitespace when there are only template children
Browse files Browse the repository at this point in the history
closes #363
  • Loading branch information
g105b committed Jan 17, 2023
1 parent 3def753 commit 3dbda21
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/TemplateCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ private function extractTemplates(Document $document):void {

private function findMatch(Element $context):TemplateElement {
$contextPath = (string)(new NodePathCalculator($context));
/** @noinspection RegExpRedundantEscape */
$contextPath = preg_replace(
"/(\[\d+\])/",
"",
Expand Down
15 changes: 15 additions & 0 deletions src/TemplateElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use Gt\Dom\Element;
use Gt\Dom\Node;
use Gt\Dom\Text;
use Throwable;

class TemplateElement {
private string $templateParentPath;
Expand Down Expand Up @@ -36,6 +37,20 @@ public function __construct(

public function removeOriginalElement():void {
$this->originalElement->remove();
try {
$parent = $this->getTemplateParent();
if(count($parent->children) === 0) {
if($firstNode = $parent->childNodes[0] ?? null) {
if(trim($firstNode->wholeText) === "") {
$parent->innerHTML = "";
}
}
}
}
// In nested lists, there may not be an actual element attached to the document
// yet, but the parent still has a path - this outcome is expected and
// completely fine in this case.
catch(Throwable) {}
}

public function getClone():Node|Element {
Expand Down
26 changes: 26 additions & 0 deletions test/phpunit/TemplateCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,30 @@ public function testBindListData_nestedList_manual():void {
}
}
}

public function testConstructor_removesWhitespace():void {
$document = new HTMLDocument(DocumentTestFactory::HTML_LIST_TEMPLATE);
new TemplateCollection($document);
self::assertSame("", $document->querySelector("ul")->innerHTML);
}

public function testConstructor_nonTemplateChildrenArePreserved():void {
$document = new HTMLDocument(DocumentTestFactory::HTML_LIST_WITH_TEXTNODE);
new TemplateCollection($document);
$ulChildren = $document->querySelector("ul")->children;
self::assertCount(1, $ulChildren);
self::assertSame("This list item will always show at the end", $ulChildren[0]->textContent);
}

public function testConstructor_nonTemplateChildrenArePreservedInOrder():void {
$document = new HTMLDocument(DocumentTestFactory::HTML_LIST_WITH_TEXTNODE);
$sut = new TemplateCollection($document);
$ulChildren = $document->querySelector("ul")->children;
$template = $sut->get($document);
$template->insertTemplate();
$template->insertTemplate();
$template->insertTemplate();
self::assertCount(4, $ulChildren);
self::assertSame("This list item will always show at the end", $ulChildren[3]->textContent);
}
}
10 changes: 10 additions & 0 deletions test/phpunit/TestFactory/DocumentTestFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,16 @@ class DocumentTestFactory {
</ol>
HTML;

const HTML_LIST_WITH_TEXTNODE = <<<HTML
<!doctype html>
<!doctype html>
<ul>
<li data-template data-bind:text>Template item!</li>
<li>This list item will always show at the end</li>
</ul>
HTML;


const HTML_TWO_LISTS = <<<HTML
<!doctype html>
<div id="favourites">
Expand Down

0 comments on commit 3dbda21

Please sign in to comment.