diff --git a/src/Html.php b/src/Html.php index d6f207b..936f4b1 100644 --- a/src/Html.php +++ b/src/Html.php @@ -6,6 +6,7 @@ use DOMElement; use DOMNode; use DOMText; +use function assert; class Html { @@ -74,12 +75,14 @@ public function toSnippet(string $id): Snippet { public function extractAsSnippets(Selector $selector, string $targetId): SnippetList { $selection = $selector->select($this->dom->documentElement); + if ($selection->isEmpty()) { throw new TempladoException('Selection result is empty - cannot extract'); } $list = new SnippetList(); - foreach($selection as $item) { + + foreach ($selection as $item) { assert($item instanceof DOMNode); if ($item instanceof DOMText) { @@ -104,7 +107,6 @@ public function extractAsSnippets(Selector $selector, string $targetId): Snippet return $list; } - public function asString(Filter $filter = null): string { $content = $this->serializeDomDocument(); $content = (new EmptyElementsFilter())->apply($content); diff --git a/src/Templado.php b/src/Templado.php index d740719..bc5dc2f 100644 --- a/src/Templado.php +++ b/src/Templado.php @@ -45,8 +45,7 @@ public static function parseHtmlString(string $string): Html { return new Html($dom); } - private static function formatError(\LibXMLError $error): string - { + private static function formatError(\LibXMLError $error): string { return \sprintf( '%s (Line %d, Column %d)', \trim($error->message), diff --git a/src/formdata/FormDataRenderer.php b/src/formdata/FormDataRenderer.php index 7344a1e..4598f0f 100644 --- a/src/formdata/FormDataRenderer.php +++ b/src/formdata/FormDataRenderer.php @@ -70,11 +70,10 @@ private function findFormElement(DOMElement $context, string $identifier): DOMEl if ($context->localName === 'form' && ($context->getAttribute('id') === $identifier || $context->getAttribute('name') === $identifier)) { - return $context; } - $xp = new DOMXPath($context->ownerDocument); + $xp = new DOMXPath($context->ownerDocument); $result = $xp->query( \sprintf('.//*[local-name() = "form" and (@id = "%1$s" or @name = "%1$s")]', $identifier), $context diff --git a/src/snippet/SnippetRenderer.php b/src/snippet/SnippetRenderer.php index 02cb5e1..9754281 100644 --- a/src/snippet/SnippetRenderer.php +++ b/src/snippet/SnippetRenderer.php @@ -42,6 +42,7 @@ private function process(DOMElement $context): void { */ private function processCurrent(): void { $nextSibling = $this->currentContext->nextSibling; + if ($this->currentContext->hasAttribute('id')) { $id = $this->currentContext->getAttribute('id'); @@ -54,6 +55,7 @@ private function processCurrent(): void { } $actualNext = $this->currentContext->nextSibling; + if ($this->currentContext->hasChildNodes()) { $this->process($this->currentContext); } @@ -67,6 +69,7 @@ private function processCurrent(): void { $this->process($actualNext); } $actualNext = $actualNext->nextSibling; + if ($actualNext === null || $actualNext->isSameNode($nextSibling)) { return; } diff --git a/src/snippet/TempladoSnippet.php b/src/snippet/TempladoSnippet.php index 1c5f15b..05654cd 100644 --- a/src/snippet/TempladoSnippet.php +++ b/src/snippet/TempladoSnippet.php @@ -65,7 +65,10 @@ private function replaceNode(DOMElement $node): DOMNode { foreach ($root->childNodes as $child) { $imported = $node->ownerDocument->importNode($child, true); $parent->insertBefore($imported, $node); - if ($first === null && $imported instanceof DOMElement) { $first = $imported; } + + if ($first === null && $imported instanceof DOMElement) { + $first = $imported; + } } $parent->removeChild($node); diff --git a/tests/HTMLTest.php b/tests/HTMLTest.php index 961cc85..6aac131 100644 --- a/tests/HTMLTest.php +++ b/tests/HTMLTest.php @@ -261,7 +261,8 @@ public function testSimpleSnippetsCanBeExtracted(): void { $testDom = new DOMDocument(); $this->assertCount(3, $list); - foreach($list as $snippet) { + + foreach ($list as $snippet) { $this->assertInstanceOf(SimpleSnippet::class, $snippet); $this->assertSame('some-id', $snippet->getTargetId()); @@ -292,7 +293,7 @@ public function testTextSnippetsCanBeExtracted(): void { $this->assertCount(1, $list); - foreach($list as $snippet) { + foreach ($list as $snippet) { $this->assertInstanceOf(TextSnippet::class, $snippet); $this->assertSame('some-id', $snippet->getTargetId()); @@ -317,7 +318,5 @@ public function testTryingToExtractNonElementNodeThrowsException(): void { $this->expectException(TempladoException::class); (new Html($dom))->extractAsSnippets($selector, 'some-id'); - } - } diff --git a/tests/formdata/FormDataRendererTest.php b/tests/formdata/FormDataRendererTest.php index 1bb8668..92b9117 100644 --- a/tests/formdata/FormDataRendererTest.php +++ b/tests/formdata/FormDataRendererTest.php @@ -61,7 +61,6 @@ public function testFormElementFoundOnRootElementById(): void { $src->documentElement, $exp->documentElement ); - } public function testFormElementFoundOnRootElementByName(): void { diff --git a/tests/issues/SnippetNestingBug/NestedSnippetTest.php b/tests/issues/SnippetNestingBug/NestedSnippetTest.php index a296a4a..b271bb3 100644 --- a/tests/issues/SnippetNestingBug/NestedSnippetTest.php +++ b/tests/issues/SnippetNestingBug/NestedSnippetTest.php @@ -5,7 +5,6 @@ use PHPUnit\Framework\TestCase; class NestedSnippetTest extends TestCase { - use DomDocumentsEqualTrait; public function testReplacementWithMultipleSiblingBug(): void {