diff --git a/src/DiDom/Node.php b/src/DiDom/Node.php index adad96c..ad3350c 100644 --- a/src/DiDom/Node.php +++ b/src/DiDom/Node.php @@ -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; } /** @@ -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; } diff --git a/tests/ElementTest.php b/tests/ElementTest.php index 73df0b0..548b2c6 100644 --- a/tests/ElementTest.php +++ b/tests/ElementTest.php @@ -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 = + '' + ; + + $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() @@ -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 = + '' + ; + + $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()