[DomCrawler] Added node name getter
This commit is contained in:
parent
ee0a0740f8
commit
2fee5769be
@ -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