Skip to content

Commit

Permalink
Fix Query::prev() method
Browse files Browse the repository at this point in the history
  • Loading branch information
ElGigi committed Aug 16, 2022
1 parent 0e89620 commit ac55acd
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 13 additions & 1 deletion src/Query/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down Expand Up @@ -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);
}

Expand Down
2 changes: 1 addition & 1 deletion src/XpathSolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 11 additions & 5 deletions tests/Query/QueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)');
Expand All @@ -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()
Expand Down

0 comments on commit ac55acd

Please sign in to comment.