diff --git a/src/Rct567/DomQuery/DomQuery.php b/src/Rct567/DomQuery/DomQuery.php index 24d01ef..aad550f 100644 --- a/src/Rct567/DomQuery/DomQuery.php +++ b/src/Rct567/DomQuery/DomQuery.php @@ -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); @@ -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); @@ -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); @@ -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)); diff --git a/src/Rct567/DomQuery/DomQueryNodes.php b/src/Rct567/DomQuery/DomQueryNodes.php index fa9b269..eb11abf 100644 --- a/src/Rct567/DomQuery/DomQueryNodes.php +++ b/src/Rct567/DomQuery/DomQueryNodes.php @@ -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 @@ -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; } diff --git a/tests/Rct567/DomQuery/Tests/DomQueryTraversingFilterTest.php b/tests/Rct567/DomQuery/Tests/DomQueryTraversingFilterTest.php index ff14a95..a90a88c 100644 --- a/tests/Rct567/DomQuery/Tests/DomQueryTraversingFilterTest.php +++ b/tests/Rct567/DomQuery/Tests/DomQueryTraversingFilterTest.php @@ -36,6 +36,7 @@ public function testHas() $this->assertEquals('', (string) $dom->find('a')->has('span > u')); $this->assertEquals('', (string) $dom->find('a')->has($dom->find('#here'))); $this->assertEquals('', (string) $dom->find('a')->has($dom->find('#here')->get(0))); # by DOMNode + $this->assertEquals(0, (string) $dom->find('a')->has(null)->length); } /* @@ -57,6 +58,8 @@ public function testFilter() $this->assertEquals('', (string) $selection->filter($dom->find('a.xpp'))); $this->assertEquals('', (string) $selection->filter($dom->find('a')->get(-2))); // filter by DOMNode $this->assertEquals('
2
', (string) $dom->find('*')->filter('header')); + $this->assertEquals(2, $dom->find('a[class]')->filter(null)->length); + $this->assertEquals('', (string) $dom->find('a[class]')->filter(null)); } /* @@ -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);