Skip to content

[DomCrawler] Added node name getter #11426

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 25, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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