Skip to content

Commit

Permalink
Fix for :nth-child(n+B) to select B-th and all following elements
Browse files Browse the repository at this point in the history
  • Loading branch information
super-dm3 committed Nov 6, 2024
1 parent 020143c commit 1f10bfa
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ docs/*
doc/*
test/fakepear
vendor/
composer.lock
composer.lock
.phpunit.result.cache
.idea/
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
QueryPath Changelog
===========================

# 4.0.2

- Fix for :nth-child(n+B) to select B-th and all following elements

# 4.0.1

- Only define global functions qp(), htmlqp(), and html5qp() if they haven't been defined already.
Expand Down
10 changes: 8 additions & 2 deletions src/CSS/QueryPathEventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -734,9 +734,15 @@ protected function parseAnB($rule)
throw new ParseException("nth-child value is invalid.");
}

// Each of these is legal: 1, -1, and -. '-' is shorthand for -1.
// Each of these is legal: 1, -1, - and <empty>. '-' is shorthand for -1. <empty> is shorthand for 1
$aVal = trim($rule[0]);
$aVal = ($aVal == '-') ? -1 : (int) $aVal;
if ($aVal === '') {
$aVal = 1;
} elseif ($aVal === '-') {
$aVal = -1;
} else {
$aVal = (int) $aVal;
}

$bVal = ! empty($rule[1]) ? (int) trim($rule[1]) : 0;

Expand Down
7 changes: 7 additions & 0 deletions tests/QueryPath/CSS/QueryPathEventHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -714,6 +714,13 @@ public function testPseudoClassNthChild()
$matches = $handler->getMatches();
$this->assertEquals(0, $matches->count());

// Test nth-child(n+3) (3+ elements)
$handler = new QueryPathEventHandler($doc);
$handler->find('i:nth-child(n+3)');
$matches = $handler->getMatches();
$this->assertEquals(3, $matches->count());
$this->assertEquals('four', $this->nthMatch($matches, 1)->getAttribute('id'));

// Test nth-child(-n+3) (First three lines)
// $handler = new QueryPathEventHandler($doc);
// $handler->find('i:nth-child(-n+3)');
Expand Down

0 comments on commit 1f10bfa

Please sign in to comment.