Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ QueryPath Changelog
- Consolidate the sibling and ancestor walks behind `next()`, `prev()`, `nextAll()`, `prevAll()`, `nextUntil()`, `prevUntil()`, `parent()`, `parents()`, `parentsUntil()` and `closest()` into one helper, so a fix to one no longer has to be repeated in its mirror (issue #115). As a side effect, `parentsUntil()` no longer loops forever on an element that is not attached to the document, and `nextUntil('')` and `prevUntil('')` treat an empty selector as no selector, as the other methods already did, instead of throwing `QueryPath\CSS\ParseException`. `closest('')` returns an empty set, as in jQuery. `nextUntil()`, `prevUntil()` and `parentsUntil()` also stop at an element already collected from an earlier starting element, as `nextAll()` and `prevAll()` do, so `nextUntil()` on each of 2000 siblings falls from about 100 ms to 1 ms
- **Behaviour change.** `prevAll()`, `prevUntil()`, `nextAll()`, `nextUntil()`, `siblings()`, `parent()` and `closest()` now return the results for a multi-element set in jQuery's order, instead of grouped by starting element (issue #117). `prevAll()` and `prevUntil()` return reverse document order, as `parents()` and `parentsUntil()` already did, and the others return document order. `qp($xml, 'c')->add('d')->prevAll()` now returns `c, b, a` rather than `b, a, c`. `next()` and `prev()` keep the order of the starting elements, as in jQuery, and the results for a single starting element are unchanged
- **Behaviour change.** A positional pseudo-class (`:first`, `:eq()`, `:last`, ...) in the stop selector of `nextUntil()`, `prevUntil()` and `parentsUntil()` now counts across the whole document, as jQuery's does, instead of seeing each sibling or ancestor as a one-element set (issue #118). `nextUntil(':first')` used to stop at once and now walks to the end, and `nextUntil(':eq(3)')` stops at the fourth element in the document. `closest()` no longer matches a positional selector at all, as in jQuery, where `closest(':first')` used to return the element itself. `next()`, `prev()` and `parent()` still test each candidate on its own, which agrees with jQuery for a single starting element
- **Breaking behaviour change.** `even()` and `odd()` now count from zero, as jQuery's `.even()` and `.odd()` and QueryPath's own `:even` and `:odd` pseudo-classes do (issue #145). `even()` keeps the 1st, 3rd, 5th, ... matches and `odd()` keeps the 2nd, 4th, ..., the reverse of before. Swap the calls to keep the previous result

# 4.2.0

Expand Down
52 changes: 24 additions & 28 deletions src/Helpers/QueryFilters.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,56 +373,52 @@ public function eachLambda($lambda): Query
}

/**
* Get the even elements, so counter-intuitively 1, 3, 5, etc.
* Get the matches at zero-based even positions (0, 2, 4, ...), as jQuery's .even() and
* the :even pseudo-class do.
*
* @return DOMQuery
* A DOMQuery wrapping all of the children.
* A DOMQuery wrapping the matches at even positions.
* @throws ParseException
* @see parent()
* @see parents()
* @see next()
* @see prev()
* @see odd()
* @since 2.1
* @author eabrand
* @see removeChildren()
*/
public function even(): Query
{
$found = new SplObjectStorage();
$even = false;
foreach ($this->matches as $m) {
if ($even && $m->nodeType === XML_ELEMENT_NODE) {
$found->offsetSet($m);
}
$even = $even ? false : true;
}

return $this->inst($found, null);
return $this->positional('even');
}

/**
* Get the odd elements, so counter-intuitively 0, 2, 4, etc.
* Get the matches at zero-based odd positions (1, 3, 5, ...), as jQuery's .odd() and
* the :odd pseudo-class do.
*
* @return DOMQuery
* A DOMQuery wrapping all of the children.
* A DOMQuery wrapping the matches at odd positions.
* @throws ParseException
* @see parent()
* @see parents()
* @see next()
* @see prev()
* @see even()
* @since 2.1
* @author eabrand
* @see removeChildren()
*/
public function odd(): Query
{
return $this->positional('odd');
}

/**
* Apply a positional pseudo-class (`even`, `odd`, ...) to the match set, as filter() would.
*
* @param string $name
*
* @return DOMQuery
* @throws ParseException
*/
private function positional(string $name): Query
{
$found = new SplObjectStorage();
$odd = true;
foreach ($this->matches as $m) {
if ($odd && $m->nodeType === XML_ELEMENT_NODE) {
foreach (Util::applyPositionalPseudoClass(iterator_to_array($this->matches, false), $name) as $m) {
if ($m->nodeType === XML_ELEMENT_NODE) {
$found->offsetSet($m);
}
$odd = $odd ? false : true;
}

return $this->inst($found, null);
Expand Down
38 changes: 38 additions & 0 deletions tests/Issues/Issue145Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* @file
*
* Regression tests for https://github.com/GravityPDF/querypath/issues/145
*
* even() and odd() count from zero, as jQuery and the :even/:odd pseudo-classes do.
* Expected values are jQuery 3.7.1's.
*/

namespace QueryPathTests;

class Issue145Test extends TestCase
{
private const XML = '<?xml version="1.0"?><r><i id="a"/><i id="b"/><i id="c"/><i id="d"/><i id="e"/></r>';

public function testEvenKeepsTheZeroBasedEvenPositions()
{
$this->assertSame('a,c,e', $this->idList(qp(self::XML, 'i')->even()));
$this->assertSame('a', $this->idList(qp(self::XML, '#a')->even()));
$this->assertSame('', $this->idList(qp(self::XML, 'nothing')->even()));
}

public function testOddKeepsTheZeroBasedOddPositions()
{
$this->assertSame('b,d', $this->idList(qp(self::XML, 'i')->odd()));
$this->assertSame('', $this->idList(qp(self::XML, '#a')->odd()));
$this->assertSame('', $this->idList(qp(self::XML, 'nothing')->odd()));
}

public function testEvenAndOddAgreeWithThePseudoClasses()
{
$qp = qp(self::XML, 'i');

$this->assertSame($this->idList($qp->filter(':even')), $this->idList($qp->even()));
$this->assertSame($this->idList($qp->filter(':odd')), $this->idList($qp->odd()));
}
}
4 changes: 2 additions & 2 deletions tests/QueryPath/DOMQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2460,14 +2460,14 @@ public function testEven()
{
$file = DATA_FILE;
$this->assertEquals(1, qp($file, 'inner')->even()->count());
$this->assertEquals(2, qp($file, 'li')->even()->count());
$this->assertEquals(3, qp($file, 'li')->even()->count());
}

public function testOdd()
{
$file = DATA_FILE;
$this->assertEquals(1, qp($file, 'inner')->odd()->count());
$this->assertEquals(3, qp($file, 'li')->odd()->count());
$this->assertEquals(2, qp($file, 'li')->odd()->count());
}

public function testFirst()
Expand Down
Loading