Skip to content

Commit ba44e04

Browse files
Call DOMTraverser directly in remove() and replaceAll()
find() deep-clones the match set through inst(), and QueryPath::with() builds a whole DOMQuery, only for the matches to be read and discarded. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 419525f commit ba44e04

4 files changed

Lines changed: 41 additions & 64 deletions

File tree

‎src/Helpers/QueryMutators.php‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DOMDocument;
66
use DOMDocumentFragment;
7+
use QueryPath\CSS\DOMTraverser;
78
use QueryPath\CSS\ParseException;
89
use QueryPath\DOMQuery;
910
use QueryPath\Exception;
@@ -794,7 +795,8 @@ public function appendTo(DOMQuery $dest): Query
794795
*/
795796
public function remove($selector = null): Query
796797
{
797-
$matches = empty($selector) ? $this->matches : $this->find($selector)->matches;
798+
// Not find(), which deep-clones the match set via inst().
799+
$matches = empty($selector) ? $this->matches : (new DOMTraverser($this->matches))->find($selector)->matches();
798800

799801
$found = new SplObjectStorage();
800802
foreach ($matches as $item) {
@@ -838,7 +840,13 @@ public function replaceAll($selector, DOMDocument $document): Query
838840
{
839841
$replacement = $this->matches->count() > 0 ? $this->getFirstMatch() : $this->document->createTextNode('');
840842

841-
foreach (QueryPath::with($document, $selector, $this->options)->get() as $item) {
843+
// As in qp($document), the document element stands in for the document, so it can match.
844+
$scope = new SplObjectStorage();
845+
if ($document->documentElement) {
846+
$scope->offsetSet($document->documentElement);
847+
}
848+
849+
foreach ((new DOMTraverser($scope))->find($selector)->matches() as $item) {
842850
$node = $replacement->cloneNode();
843851
$node = $document->importNode($node);
844852
$item->parentNode->replaceChild($node, $item);

‎src/qp_functions.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
* The core classes that compose QueryPath.
4141
*
4242
* The QueryPath classes contain the brunt of the QueryPath code. If you are
43-
* interested in working with just the CSS engine, you may want to look at CssEventHandler,
43+
* interested in working with just the CSS engine, you may want to look at QueryPath\CSS\DOMTraverser,
4444
* which can be used without the rest of QueryPath. If you are interested in looking
4545
* carefully at QueryPath's implementation details, then the QueryPath class is where you
4646
* should begin. If you are interested in writing extensions, than you may want to look at

‎tests/Issues/Issue126Test.php‎

Lines changed: 29 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -28,61 +28,54 @@ class Issue126Test extends TestCase
2828
private const NESTED_XML = '<?xml version="1.0"?><root><p><i>a</i><b>x</b></p><p><i>b</i></p><i>c</i></root>';
2929

3030
/**
31-
* The tables from #123 and #124, as find() answers them.
31+
* The tables from #123 and #124 against each factory's document: the selector, what
32+
* find() matches, and the list items left behind.
3233
*
3334
* @return array
3435
*/
3536
public function selectorProvider(): array
3637
{
37-
return [
38-
'matches (#123)' => ['li:matches(.x)', ['b']],
39-
'not eq(0) (#124)' => ['li:not(:eq(0))', ['b', 'c']],
40-
'not first (#124)' => ['li:not(:first)', ['b', 'c']],
41-
'not last (#124)' => ['li:not(:last)', ['a', 'b']],
42-
'not odd (#124)' => ['li:not(:odd)', ['a', 'c']],
43-
'not class (#124)' => ['li:not(.x)', ['a', 'c']],
38+
$selectors = [
39+
'li:matches(.x)' => [['b'], ['a', 'c']],
40+
'li:not(:eq(0))' => [['b', 'c'], ['a']],
41+
'li:not(:first)' => [['b', 'c'], ['a']],
42+
'li:not(:last)' => [['a', 'b'], ['c']],
43+
'li:not(:odd)' => [['a', 'c'], ['b']],
44+
'li:not(.x)' => [['a', 'c'], ['b']],
4445
];
45-
}
4646

47-
/**
48-
* Each selector from selectorProvider() against each factory's document.
49-
*
50-
* @return array
51-
*/
52-
public function factorySelectorProvider(): array
53-
{
5447
$cases = [];
5548
foreach (['qp' => self::LIST_XML, 'htmlqp' => self::LIST_HTML, 'html5qp' => self::LIST_HTML] as $factory => $markup) {
56-
foreach ($this->selectorProvider() as $name => $case) {
57-
$cases[$factory . ': ' . $name] = array_merge([$factory, $markup], $case);
49+
foreach ($selectors as $selector => list($matched, $left)) {
50+
$cases[$factory . ' ' . $selector] = [$factory, $markup, $selector, $matched, $left];
5851
}
5952
}
6053

6154
return $cases;
6255
}
6356

6457
/**
65-
* @dataProvider factorySelectorProvider
58+
* @dataProvider selectorProvider
6659
*/
67-
public function testRemoveAgreesWithFind(string $factory, string $markup, string $selector, array $expected)
60+
public function testRemoveAgreesWithFind(string $factory, string $markup, string $selector, array $matched, array $left)
6861
{
69-
$this->assertSame($expected, $this->ids($factory($markup)->find($selector)));
62+
$this->assertSame($matched, $this->ids($factory($markup)->find($selector)));
7063

7164
$qp = $factory($markup);
72-
$this->assertSame($expected, $this->ids($qp->remove($selector)));
73-
$this->assertSame(array_values(array_diff(['a', 'b', 'c'], $expected)), $this->ids($qp->find('li')));
65+
$this->assertSame($matched, $this->ids($qp->remove($selector)));
66+
$this->assertSame($left, $this->ids($qp->find('li')));
7467
}
7568

7669
/**
77-
* @dataProvider factorySelectorProvider
70+
* @dataProvider selectorProvider
7871
*/
79-
public function testReplaceAllAgreesWithFind(string $factory, string $markup, string $selector, array $expected)
72+
public function testReplaceAllAgreesWithFind(string $factory, string $markup, string $selector, array $matched, array $left)
8073
{
8174
$document = $factory($markup)->get(0)->ownerDocument;
8275
qp('<?xml version="1.0"?><root><z/></root>', 'z')->replaceAll($selector, $document);
8376

84-
$this->assertSame(array_values(array_diff(['a', 'b', 'c'], $expected)), $this->ids(qp($document)->find('li')));
85-
$this->assertCount(count($expected), qp($document)->find('z'));
77+
$this->assertSame($left, $this->ids(qp($document)->find('li')));
78+
$this->assertCount(count($matched), qp($document)->find('z'));
8679
}
8780

8881
public function testRemoveSearchesDescendantsOnly()
@@ -119,42 +112,24 @@ public function testReplaceAllSearchesTheWholeDocument()
119112
$this->assertSame(['z', 'z', 'i'], $this->tags(qp($document)->children()));
120113
}
121114

122-
/**
123-
* @return string[]
124-
*/
125115
private function ids(DOMQuery $qp): array
126116
{
127-
$ids = [];
128-
foreach ($qp->get() as $node) {
129-
$ids[] = $node->getAttribute('id');
130-
}
131-
132-
return $ids;
117+
return array_map(function ($node) {
118+
return $node->getAttribute('id');
119+
}, $qp->get());
133120
}
134121

135-
/**
136-
* @return string[]
137-
*/
138122
private function text(DOMQuery $qp): array
139123
{
140-
$text = [];
141-
foreach ($qp->get() as $node) {
142-
$text[] = $node->textContent;
143-
}
144-
145-
return $text;
124+
return array_map(function ($node) {
125+
return $node->textContent;
126+
}, $qp->get());
146127
}
147128

148-
/**
149-
* @return string[]
150-
*/
151129
private function tags(DOMQuery $qp): array
152130
{
153-
$tags = [];
154-
foreach ($qp->get() as $node) {
155-
$tags[] = $node->tagName;
156-
}
157-
158-
return $tags;
131+
return array_map(function ($node) {
132+
return $node->tagName;
133+
}, $qp->get());
159134
}
160135
}

‎tests/Issues/Issue66Test.php‎

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,7 @@ public function testPositionalPseudoClassesIndexTheMatchedSet($selector, array $
7272
*/
7373
public function testRemoveAgreesWithFind($selector, array $expected)
7474
{
75-
$removed = $this->textOf(html5qp(self::HTML)->remove($selector));
76-
sort($removed);
77-
78-
$sorted = $expected;
79-
sort($sorted);
80-
81-
$this->assertSame($sorted, $removed);
75+
$this->assertSame($expected, $this->textOf(html5qp(self::HTML)->remove($selector)));
8276
}
8377

8478
/**

0 commit comments

Comments
 (0)