Skip to content

Commit

Permalink
[DomCrawler] Added node name getter
Browse files Browse the repository at this point in the history
  • Loading branch information
fejese committed Jul 23, 2014
1 parent ee0a074 commit 2fee576
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Symfony/Component/DomCrawler/Crawler.php
Expand Up @@ -524,6 +524,22 @@ public function attr($attribute)
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.
*
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
Expand Up @@ -358,6 +358,18 @@ public function testMissingAttrValueIsNull()
$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');
Expand Down

0 comments on commit 2fee576

Please sign in to comment.