Skip to content

Commit 15b6633

Browse files
committed
added support for giving null to methods not/filter/has
1 parent bc64a7b commit 15b6633

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

src/Rct567/DomQuery/DomQuery.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,8 @@ public function not($selector)
555555
$result->addDomNode($node);
556556
}
557557
}
558+
} elseif ($selector === null) {
559+
$result->addDomNodes($this->nodes);
558560
} else {
559561
$selection = self::create($this->document)->find($selector);
560562

@@ -591,7 +593,7 @@ public function not($selector)
591593
public function add($selector, $context=null)
592594
{
593595
$result = $this->createChildInstance();
594-
$result->nodes = $this->nodes;
596+
$result->addDomNodes($this->nodes);
595597

596598
$selection = $this->getTargetResult($selector, $context);
597599

@@ -628,6 +630,8 @@ public function filter($selector)
628630
$result->addDomNode($node);
629631
}
630632
}
633+
} elseif ($selector === null) {
634+
$result->addDomNodes($this->nodes);
631635
} else {
632636
$selection = self::create($this->document)->find($selector);
633637

@@ -741,7 +745,7 @@ public function has($selector)
741745
{
742746
$result = $this->createChildInstance();
743747

744-
if ($this->length > 0) {
748+
if ($this->length > 0 && $selector !== null) {
745749
foreach ($this as $node) {
746750
if ($node->find($selector)->length > 0) {
747751
$result->addDomNode($node->get(0));

src/Rct567/DomQuery/DomQueryNodes.php

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,33 @@ public function loadDomNodeList(\DOMNodeList $dom_node_list, $prepend=false)
317317
}
318318

319319
/**
320-
* Add node to result set.
320+
* Add an array of nodes to the result set.
321+
*
322+
* @param \DOMNode[] $dom_nodes
323+
*
324+
* @return void
325+
*/
326+
public function addDomNodes(array $dom_nodes)
327+
{
328+
if (!isset($dom_nodes[0])) {
329+
return;
330+
}
331+
332+
foreach ($dom_nodes as $dom_node) {
333+
$this->nodes[] = $dom_node;
334+
}
335+
336+
$this->length = \count($this->nodes);
337+
338+
if ($dom_nodes[0] instanceof \DOMDocument) {
339+
$this->setDomDocument($dom_nodes[0]);
340+
} else {
341+
$this->setDomDocument($dom_nodes[0]->ownerDocument);
342+
}
343+
}
344+
345+
/**
346+
* Add a single node to the result set.
321347
*
322348
* @param \DOMNode $dom_node
323349
* @param bool $prepend
@@ -327,7 +353,7 @@ public function loadDomNodeList(\DOMNodeList $dom_node_list, $prepend=false)
327353
public function addDomNode(\DOMNode $dom_node, $prepend=false)
328354
{
329355
if ($prepend) {
330-
array_unshift($this->nodes, $dom_node);
356+
\array_unshift($this->nodes, $dom_node);
331357
} else {
332358
$this->nodes[] = $dom_node;
333359
}

tests/Rct567/DomQuery/Tests/DomQueryTraversingFilterTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function testHas()
3636
$this->assertEquals('<a class="x"><span id="here"><u></u></span></a>', (string) $dom->find('a')->has('span > u'));
3737
$this->assertEquals('<a class="x"><span id="here"><u></u></span></a>', (string) $dom->find('a')->has($dom->find('#here')));
3838
$this->assertEquals('<a class="x"><span id="here"><u></u></span></a>', (string) $dom->find('a')->has($dom->find('#here')->get(0))); # by DOMNode
39+
$this->assertEquals(0, (string) $dom->find('a')->has(null)->length);
3940
}
4041

4142
/*
@@ -57,6 +58,8 @@ public function testFilter()
5758
$this->assertEquals('<a class="xpp"></a>', (string) $selection->filter($dom->find('a.xpp')));
5859
$this->assertEquals('<a class="x"></a>', (string) $selection->filter($dom->find('a')->get(-2))); // filter by DOMNode
5960
$this->assertEquals('<header>2</header>', (string) $dom->find('*')->filter('header'));
61+
$this->assertEquals(2, $dom->find('a[class]')->filter(null)->length);
62+
$this->assertEquals('<a class="x"></a><a class="xpp"></a>', (string) $dom->find('a[class]')->filter(null));
6063
}
6164

6265
/*
@@ -68,6 +71,7 @@ public function testNot()
6871
$selection = $dom->find('a');
6972
$this->assertEquals(5, $selection->length);
7073
$this->assertEquals(5, $selection->not('p')->length);
74+
$this->assertEquals((string) $selection, (string) $selection->not(null));
7175
$this->assertEquals(0, $selection->not('a')->length);
7276
$this->assertEquals(4, $selection->not('#mmm')->length);
7377
$this->assertEquals(3, $selection->not('#mmm')->not('.xpp')->length);

0 commit comments

Comments
 (0)