[DomCrawler] Added node name getter

This commit is contained in:
Endre Fejes 2014-07-18 23:16:02 +02:00
parent ee0a0740f8
commit 2fee5769be
2 changed files with 28 additions and 0 deletions

View File

@ -524,6 +524,22 @@ class Crawler extends \SplObjectStorage
return $node->hasAttribute($attribute) ? $node->getAttribute($attribute) : null;
}
/**
* Returns the node name of the first node of the list.
*
* @return string The node name
*
* @throws \InvalidArgumentException When current node is empty
*/
public function nodeName()
{
if (!count($this)) {
throw new \InvalidArgumentException('The current node list is empty.');
}
return $this->getNode(0)->nodeName;
}
/**
* Returns the node value of the first node of the list.
*

View File

@ -358,6 +358,18 @@ EOF
$this->assertNull($div->attr('missing-attr'), '->attr() reads missing attributes correctly');
}
public function testNodeName()
{
$this->assertEquals('li', $this->createTestCrawler()->filterXPath('//li')->nodeName(), '->nodeName() returns the node name of the first element of the node list');
try {
$this->createTestCrawler()->filterXPath('//ol')->nodeName();
$this->fail('->nodeName() throws an \InvalidArgumentException if the node list is empty');
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->nodeName() throws an \InvalidArgumentException if the node list is empty');
}
}
public function testText()
{
$this->assertEquals('One', $this->createTestCrawler()->filterXPath('//li')->text(), '->text() returns the node value of the first element of the node list');