Skip to content

Commit

Permalink
Fix a bug when a call of Element::previousSibling() with selector ret…
Browse files Browse the repository at this point in the history
…urns a previous sibling when there is not matching element
  • Loading branch information
Imangazaliev committed Jul 27, 2021
1 parent d50eb38 commit 346db1e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 13 deletions.
14 changes: 3 additions & 11 deletions src/DiDom/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -690,18 +690,14 @@ public function previousSibling($selector = null, $nodeType = null)

$element = new Element($node);

if ($selector === null) {
return $element;
}

if ($element->matches($selector)) {
if ($selector === null || $element->matches($selector)) {
return $element;
}

$node = $node->previousSibling;
}

return new Element($this->node->previousSibling);
return null;
}

/**
Expand Down Expand Up @@ -830,11 +826,7 @@ public function nextSibling($selector = null, $nodeType = null)

$element = new Element($node);

if ($selector === null) {
return $element;
}

if ($element->matches($selector)) {
if ($selector === null || $element->matches($selector)) {
return $element;
}

Expand Down
48 changes: 46 additions & 2 deletions tests/ElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1303,7 +1303,29 @@ public function testPreviousSiblingWithSelector()

$expectedNode = $list->getNode()->childNodes->item(2);

$this->assertEquals($expectedNode, $item->previousSibling('li:has(a[href$=".com"])')->getNode());
$this->assertEquals($expectedNode, $item->previousSibling('li:has(a[href*="google"])')->getNode());
}

public function testPreviousSiblingWithSelectorElementMissed()
{
$html =
'<ul>'.
'<li><a href="https://amazon.com">Amazon</a></li>'.
'<li><a href="https://facebook.com">Facebook</a></li>'.
'<li><a href="https://google.com">Google</a></li>'.
'<li><a href="https://www.w3.org">W3C</a></li>'.
'<li><a href="https://wikipedia.org">Wikipedia</a></li>'.
'</ul>'
;

$document = new Document($html, false);

$list = $document->first('ul');

$item = $list->getNode()->childNodes->item(4);
$item = new Element($item);

$this->assertNull($item->previousSibling('li:has(a[href*="acme"])'));
}

public function testPreviousSiblingWithNodeType()
Expand Down Expand Up @@ -1622,7 +1644,29 @@ public function testNextSiblingWithSelector()

$expectedNode = $list->getNode()->childNodes->item(3);

$this->assertEquals($expectedNode, $item->nextSibling('li:has(a[href$=".org"])')->getNode());
$this->assertEquals($expectedNode, $item->nextSibling('li:has(a[href*="w3"])')->getNode());
}

public function testNextSiblingWithSelectorElementMissed()
{
$html =
'<ul>'.
'<li><a href="https://amazon.com">Amazon</a></li>'.
'<li><a href="https://facebook.com">Facebook</a></li>'.
'<li><a href="https://google.com">Google</a></li>'.
'<li><a href="https://www.w3.org">W3C</a></li>'.
'<li><a href="https://wikipedia.org">Wikipedia</a></li>'.
'</ul>'
;

$document = new Document($html, false);

$list = $document->first('ul');

$item = $list->getNode()->childNodes->item(0);
$item = new Element($item);

$this->assertNull($item->nextSibling('li:has(a[href*="acme"])'));
}

public function testNextSiblingWithNodeType()
Expand Down

0 comments on commit 346db1e

Please sign in to comment.