public function testToArray() { $root = new HtmlNode(new Tag('root')); $parent = new HtmlNode(new Tag('div')); $child1 = new HtmlNode(new Tag('a')); $child2 = new HtmlNode(new Tag('p')); $child3 = new HtmlNode(new Tag('a')); $root->addChild($parent); $parent->addChild($child1); $parent->addChild($child2); $child2->addChild($child3); $selector = new Selector('a'); $collection = $selector->find($root); $array = $collection->toArray(); $lastA = end($array); $this->assertEquals($child3->id(), $lastA->id()); }
public function testFindChildUsingChildSelector() { $root = new HtmlNode(new Tag('root')); $parent = new HtmlNode(new Tag('div')); $child1 = new HtmlNode(new Tag('ul')); $child2 = new HtmlNode(new Tag('span')); $child3 = new HtmlNode(new Tag('ul')); $root->addChild($parent); $parent->addChild($child1); $child2->addChild($child3); $parent->addChild($child2); $selector = new Selector('div > ul'); $this->assertEquals(1, count($selector->find($root))); }
/** * Find elements by css selector * * @param string $selector * @param int $nth * @return array */ public function find($selector, $nth = null) { $selector = new Selector($selector); $nodes = $selector->find($this); if (!is_null($nth)) { // return nth-element or array if (isset($nodes[$nth])) { return $nodes[$nth]; } return null; } return $nodes; }
public function testFindXpathKeySelector() { $parent = new HtmlNode(new Tag('div')); $child1 = new HtmlNode(new Tag('a')); $child2 = new HtmlNode(new Tag('p')); $child3 = new HtmlNode(new Tag('a')); $child3->getTag()->setAttributes(['class' => ['value' => 'link outer', 'doubleQuote' => false]]); $parent->addChild($child1); $parent->addChild($child2); $parent->addChild($child3); $selector = new Selector('div[1]'); $this->assertEquals($parent->id(), $selector->find($parent)[0]->id()); }