Skip to content

Commit

Permalink
added support for giving null to methods not/filter/has
Browse files Browse the repository at this point in the history
  • Loading branch information
Rct567 committed Jan 18, 2024
1 parent bc64a7b commit 15b6633
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/Rct567/DomQuery/DomQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -555,6 +555,8 @@ public function not($selector)
$result->addDomNode($node);
}
}
} elseif ($selector === null) {
$result->addDomNodes($this->nodes);
} else {
$selection = self::create($this->document)->find($selector);

Expand Down Expand Up @@ -591,7 +593,7 @@ public function not($selector)
public function add($selector, $context=null)
{
$result = $this->createChildInstance();
$result->nodes = $this->nodes;
$result->addDomNodes($this->nodes);

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

Expand Down Expand Up @@ -628,6 +630,8 @@ public function filter($selector)
$result->addDomNode($node);
}
}
} elseif ($selector === null) {
$result->addDomNodes($this->nodes);
} else {
$selection = self::create($this->document)->find($selector);

Expand Down Expand Up @@ -741,7 +745,7 @@ public function has($selector)
{
$result = $this->createChildInstance();

if ($this->length > 0) {
if ($this->length > 0 && $selector !== null) {
foreach ($this as $node) {
if ($node->find($selector)->length > 0) {
$result->addDomNode($node->get(0));
Expand Down
30 changes: 28 additions & 2 deletions src/Rct567/DomQuery/DomQueryNodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,33 @@ public function loadDomNodeList(\DOMNodeList $dom_node_list, $prepend=false)
}

/**
* Add node to result set.
* Add an array of nodes to the result set.
*
* @param \DOMNode[] $dom_nodes
*
* @return void
*/
public function addDomNodes(array $dom_nodes)
{
if (!isset($dom_nodes[0])) {
return;
}

foreach ($dom_nodes as $dom_node) {
$this->nodes[] = $dom_node;
}

$this->length = \count($this->nodes);

if ($dom_nodes[0] instanceof \DOMDocument) {
$this->setDomDocument($dom_nodes[0]);
} else {
$this->setDomDocument($dom_nodes[0]->ownerDocument);
}
}

/**
* Add a single node to the result set.
*
* @param \DOMNode $dom_node
* @param bool $prepend
Expand All @@ -327,7 +353,7 @@ public function loadDomNodeList(\DOMNodeList $dom_node_list, $prepend=false)
public function addDomNode(\DOMNode $dom_node, $prepend=false)
{
if ($prepend) {
array_unshift($this->nodes, $dom_node);
\array_unshift($this->nodes, $dom_node);
} else {
$this->nodes[] = $dom_node;
}
Expand Down
4 changes: 4 additions & 0 deletions tests/Rct567/DomQuery/Tests/DomQueryTraversingFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function testHas()
$this->assertEquals('<a class="x"><span id="here"><u></u></span></a>', (string) $dom->find('a')->has('span > u'));
$this->assertEquals('<a class="x"><span id="here"><u></u></span></a>', (string) $dom->find('a')->has($dom->find('#here')));
$this->assertEquals('<a class="x"><span id="here"><u></u></span></a>', (string) $dom->find('a')->has($dom->find('#here')->get(0))); # by DOMNode
$this->assertEquals(0, (string) $dom->find('a')->has(null)->length);
}

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

/*
Expand All @@ -68,6 +71,7 @@ public function testNot()
$selection = $dom->find('a');
$this->assertEquals(5, $selection->length);
$this->assertEquals(5, $selection->not('p')->length);
$this->assertEquals((string) $selection, (string) $selection->not(null));
$this->assertEquals(0, $selection->not('a')->length);
$this->assertEquals(4, $selection->not('#mmm')->length);
$this->assertEquals(3, $selection->not('#mmm')->not('.xpp')->length);
Expand Down

0 comments on commit 15b6633

Please sign in to comment.