From 3dbda21373262cb8bf6ca7dfeb56c10fcf7f7f52 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Tue, 17 Jan 2023 11:41:17 +0000 Subject: [PATCH] feature: trim whitespace when there are only template children closes #363 --- src/TemplateCollection.php | 1 + src/TemplateElement.php | 15 +++++++++++ test/phpunit/TemplateCollectionTest.php | 26 +++++++++++++++++++ .../TestFactory/DocumentTestFactory.php | 10 +++++++ 4 files changed, 52 insertions(+) diff --git a/src/TemplateCollection.php b/src/TemplateCollection.php index b81b736..a68548d 100644 --- a/src/TemplateCollection.php +++ b/src/TemplateCollection.php @@ -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+\])/", "", diff --git a/src/TemplateElement.php b/src/TemplateElement.php index 48ccc64..a87c0eb 100644 --- a/src/TemplateElement.php +++ b/src/TemplateElement.php @@ -4,6 +4,7 @@ use Gt\Dom\Element; use Gt\Dom\Node; use Gt\Dom\Text; +use Throwable; class TemplateElement { private string $templateParentPath; @@ -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 { diff --git a/test/phpunit/TemplateCollectionTest.php b/test/phpunit/TemplateCollectionTest.php index f35d1dc..cf16f9c 100644 --- a/test/phpunit/TemplateCollectionTest.php +++ b/test/phpunit/TemplateCollectionTest.php @@ -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); + } } diff --git a/test/phpunit/TestFactory/DocumentTestFactory.php b/test/phpunit/TestFactory/DocumentTestFactory.php index 2a77fbf..bea5ca3 100644 --- a/test/phpunit/TestFactory/DocumentTestFactory.php +++ b/test/phpunit/TestFactory/DocumentTestFactory.php @@ -244,6 +244,16 @@ class DocumentTestFactory { HTML; + const HTML_LIST_WITH_TEXTNODE = << + + +HTML; + + const HTML_TWO_LISTS = <<