From a7bebcb8c62ef7f34e85edd476ebf9aeeaaa228f Mon Sep 17 00:00:00 2001 From: Jake Jackson Date: Sun, 11 Dec 2022 21:31:04 +1100 Subject: [PATCH] Fix return value of wrapAll() when count() is 0 --- CHANGELOG.md | 3 ++- src/Helpers/QueryMutators.php | 2 +- tests/QueryPath/DOMQueryTest.php | 3 +++ 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 11b1bf2..c0a3746 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,10 @@ QueryPath Changelog =========================== -# 3.2.4 +# 4.0.0 - Reverse logic in DomQuery::html5() so that DomQuery::html5() returns the content of the current match, and DomQuery::html5('') replaces the content of the current matches. This matches the existing logic used in DomQuery::html(). +- Return DOMQuery object if QueryMutators::wrapAll() has no matches (instead of null). This aligns the method with the Docblock return type. # 3.2.3 diff --git a/src/Helpers/QueryMutators.php b/src/Helpers/QueryMutators.php index dacfc76..c0043c5 100644 --- a/src/Helpers/QueryMutators.php +++ b/src/Helpers/QueryMutators.php @@ -453,7 +453,7 @@ public function wrap($markup): Query public function wrapAll($markup) { if ($this->matches->count() === 0) { - return; + return $this; } $data = $this->prepareInsert($markup); diff --git a/tests/QueryPath/DOMQueryTest.php b/tests/QueryPath/DOMQueryTest.php index 9926d31..39c49fd 100644 --- a/tests/QueryPath/DOMQueryTest.php +++ b/tests/QueryPath/DOMQueryTest.php @@ -1034,6 +1034,9 @@ public function testWrapAll() 'li' )->wrapAll('
')->get(0)->ownerDocument->saveXML(); $this->assertEquals(5, qp($xml, '.testWrap > inside > center > li')->count()); + + // verify wrapAll() returns DomQuery object is no matches + $this->assertInstanceOf(DOMQuery::class, qp($file, '#non-existing-selector')->wrapAll('')); } public function testWrapInner()