Skip to content

Commit 95dbf53

Browse files
gravitypdf-botclaude
authored andcommitted
Remove, rather than keep, SplObjectStorage nodes in not()
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4d81ac8 commit 95dbf53

3 files changed

Lines changed: 19 additions & 1 deletion

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ QueryPath Changelog
3333
- Fix `tag()` emitting an "Undefined property" warning and returning `NULL` when the first match is not an element, such as a text, comment or processing instruction node returned by `contents()`. It now returns an empty string, as it does for an empty match set (issue #93)
3434
- Fix QueryPath's error handler staying installed for the rest of the request when an exception other than a `QueryPath\Exception` (a `TypeError`, or one thrown by a stream wrapper, for example) escaped from parsing a document, inserting markup, or `writeXML()`/`writeHTML()` (issue #95)
3535
- Fix `writeHTML()` throwing `QueryPath\ParseException` instead of `QueryPath\IOException` when the file cannot be written, as `writeXML()` and `writeHTML5()` do (issue #92). `IOException` extends `ParseException`, so existing `catch (ParseException $e)` blocks still catch it
36+
- Fix `not()` keeping only the given nodes, instead of removing them, when passed an `SplObjectStorage` such as `get(null, true)` returns (issue #75)
3637

3738
# 4.1.0
3839

‎src/Helpers/QueryFilters.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,7 @@ public function not($selector): Query
716716
}
717717
} elseif ($selector instanceof SplObjectStorage) {
718718
foreach ($this->matches as $m) {
719-
if ($selector->offsetExists($m)) {
719+
if (! $selector->offsetExists($m)) {
720720
$found->offsetSet($m);
721721
}
722722
}

‎tests/QueryPath/DOMQueryTest.php‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -773,6 +773,23 @@ public function testNot()
773773
$this->assertEquals(1, $qp->not($arr)->count());
774774
}
775775

776+
public function testNotRemovesTheSameNodesForEveryArgumentType()
777+
{
778+
$qp = qp(DATA_FILE, 'li');
779+
$exclude = $qp->branch()->filter('#one, #three');
780+
$ids = function ($q) {
781+
return array_map(function ($n) {
782+
return $n->getAttribute('id');
783+
}, $q->get());
784+
};
785+
786+
$expected = ['two', 'four', 'five'];
787+
$this->assertSame($expected, $ids($qp->not('#one, #three')), 'Selector');
788+
$this->assertSame($expected, $ids($qp->not($exclude->get())), 'Array');
789+
$this->assertSame($expected, $ids($qp->not($exclude->get(null, true))), 'SplObjectStorage');
790+
$this->assertSame(['two', 'three', 'four', 'five'], $ids($qp->not($exclude->get(0))), 'DOMElement');
791+
}
792+
776793
public function testSlice()
777794
{
778795
$file = DATA_FILE;

0 commit comments

Comments
 (0)