feature #11426 [DomCrawler] Added node name getter (fejese)
This PR was merged into the 2.6-dev branch.
Discussion
----------
[DomCrawler] Added node name getter
[DomCrawler] Added node name getter
Based on the use case came up in a SO question (http://stackoverflow.com/questions/22564465/getting-the-tag-name-of-a-element-crawled-with-domcrawler-in-php) I propose a new helper function to get the name of the active node
| Q | A
| ------------- | ---
| Bug fix? | no
| New feature? | yes
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets |
| License | MIT
| Doc PR | symfony/symfony-docs#4039
(Follow up of symfony/symfony#11351)
Commits
-------
2fee576
[DomCrawler] Added node name getter
This commit is contained in:
commit
96cd78e43b
@ -524,6 +524,22 @@ class Crawler extends \SplObjectStorage
|
|||||||
return $node->hasAttribute($attribute) ? $node->getAttribute($attribute) : null;
|
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.
|
* Returns the node value of the first node of the list.
|
||||||
*
|
*
|
||||||
|
@ -358,6 +358,18 @@ EOF
|
|||||||
$this->assertNull($div->attr('missing-attr'), '->attr() reads missing attributes correctly');
|
$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()
|
public function testText()
|
||||||
{
|
{
|
||||||
$this->assertEquals('One', $this->createTestCrawler()->filterXPath('//li')->text(), '->text() returns the node value of the first element of the node list');
|
$this->assertEquals('One', $this->createTestCrawler()->filterXPath('//li')->text(), '->text() returns the node value of the first element of the node list');
|
||||||
|
Reference in New Issue
Block a user