diff --git a/CHANGELOG.md b/CHANGELOG.md index 3998a7f..25164ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,11 +4,12 @@ All notable changes to this project will be documented in this file. This projec to [Semantic Versioning] (http://semver.org/). For change log format, use [Keep a Changelog] (http://keepachangelog.com/). -## [2.0.0-beta3] - In progress +## [2.0.0-beta3] - 2022-08-16 ### Fixed - Fix the "end" operator `=$` for attribute comparison selector +- Fix `Query::prev()` method ## [2.0.0-beta2] - 2022-01-13 diff --git a/src/Query/Query.php b/src/Query/Query.php index 74a2563..cb74c1b 100644 --- a/src/Query/Query.php +++ b/src/Query/Query.php @@ -300,7 +300,12 @@ public function children(?string $selector = null): static } return new Query( - $this->xpath(sprintf('./child::*[boolean(%s)]', $this->selector($selector, XpathSolver::CONTEXT_SELF))), + $this->xpath( + sprintf( + './child::*[boolean(%s)]', + $this->selector($selector, XpathSolver::CONTEXT_SELF)[0] ?? '0' + ) + ), null, $this->htmlSelector ); @@ -638,6 +643,13 @@ public function nextAll(string $selector = null): static */ public function prev(string $selector = null): static { +// return new Query( +// $this->xpath( +// sprintf( +// './preceding-sibling::*[1]', +// ) +// ),null, $this->htmlSelector); +// $this->xpath(); return new Query($this->selector($selector ?? '*', XpathSolver::CONTEXT_PREV), null, $this->htmlSelector); } diff --git a/src/XpathSolver.php b/src/XpathSolver.php index 50f9050..a3a818a 100644 --- a/src/XpathSolver.php +++ b/src/XpathSolver.php @@ -32,7 +32,7 @@ class XpathSolver public const CONTEXT_PARENTS = 'ancestor::'; public const CONTEXT_NEXT = 'following-sibling::*[1]/self::'; public const CONTEXT_NEXT_ALL = 'following-sibling::'; - public const CONTEXT_PREV = 'preceding-sibling::*[last()]/self::'; + public const CONTEXT_PREV = 'preceding-sibling::*[1]/self::'; public const CONTEXT_PREV_ALL = 'preceding-sibling::'; protected CssSelectorParser $parser; diff --git a/tests/Query/QueryTest.php b/tests/Query/QueryTest.php index 8aa804f..58c7b25 100644 --- a/tests/Query/QueryTest.php +++ b/tests/Query/QueryTest.php @@ -286,13 +286,16 @@ public function testPrev() $query = $htmlSelector->query(__DIR__ . '/../files/test.html', true); $result = $query->find('footer > div:last :last-child'); + $result2 = $result->prev(); $this->assertCount(1, $result2, (string)$result2->getSelector()); - $this->assertEquals('Contact 4', (string)$result2->get(0), (string)$result2->getSelector()); + $this->assertEquals('Contact 10', (string)$result2->get(0), (string)$result2->getSelector()); $result2 = $result->prev('span'); + $this->assertCount(1, $result2, (string)$result2->getSelector()); + $result2 = $result->prev('button'); $this->assertCount(0, $result2, (string)$result2->getSelector()); $result = $query->find('footer > ul:last :eq(1)'); @@ -314,16 +317,19 @@ public function testPrevAll() $this->assertEquals('Contact 4', (string)$result2->get(0), (string)$result2->getSelector()); $this->assertEquals('Contact 5', (string)$result2->get(1), (string)$result2->getSelector()); - $result2 = $result->prev('span'); - - $this->assertCount(0, $result2, (string)$result2->getSelector()); - $result = $query->find('footer > ul:last :last-child'); $result = $result->prevAll(); $this->assertCount(2, $result, (string)$result2->getSelector()); $this->assertEquals('Link 4.1', (string)$result->get(0), (string)$result->getSelector()); $this->assertEquals('Link 4.2', (string)$result->get(1), (string)$result->getSelector()); + + $result = $query->find('footer > div:last :eq(3)'); + $result = $result->prevAll(); + $this->assertCount(3, $result, (string)$result->getSelector()); + $this->assertEquals('Contact 4', (string)$result->get(0), (string)$result->getSelector()); + $this->assertEquals('Contact 5', (string)$result->get(1), (string)$result->getSelector()); + $this->assertEquals('Contact 6', (string)$result->get(2), (string)$result->getSelector()); } public function testSerializeArray()