Added html method into Crawler class
* This method allow to get html inside desired element * New method is tested
This commit is contained in:
parent
bc6d68803e
commit
a8d2e06387
@ -480,6 +480,28 @@ class Crawler extends \SplObjectStorage
|
||||
return $this->getNode(0)->nodeValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the html of the first node of the list.
|
||||
*
|
||||
* @return string The node html
|
||||
*
|
||||
* @throws \InvalidArgumentException When current node is empty
|
||||
*/
|
||||
public function html()
|
||||
{
|
||||
if (!count($this)) {
|
||||
throw new \InvalidArgumentException('The current node list is empty.');
|
||||
}
|
||||
|
||||
$html = '';
|
||||
foreach ($this->getNode(0)->childNodes as $child) {
|
||||
|
||||
$html .= $child->ownerDocument->saveXML($child);
|
||||
}
|
||||
|
||||
return str_replace(' ', '', $html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts information from the list of nodes.
|
||||
*
|
||||
|
@ -319,6 +319,18 @@ EOF
|
||||
}
|
||||
}
|
||||
|
||||
public function testHtml()
|
||||
{
|
||||
$this->assertEquals('<img alt="Bar"/>', $this->createTestCrawler()->filterXPath('//a[5]')->html(), '->html() returns the html of the first element of the node list');
|
||||
|
||||
try {
|
||||
$this->createTestCrawler()->filterXPath('//ol')->html();
|
||||
$this->fail('->html() throws an \InvalidArgumentException if the node list is empty');
|
||||
} catch (\InvalidArgumentException $e) {
|
||||
$this->assertTrue(true, '->html() throws an \InvalidArgumentException if the node list is empty');
|
||||
}
|
||||
}
|
||||
|
||||
public function testExtract()
|
||||
{
|
||||
$crawler = $this->createTestCrawler()->filterXPath('//ul[1]/li');
|
||||
|
Reference in New Issue
Block a user