Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[DomCrawler] Change ->text() , ->html() and ->attr() methods to retur… #28614

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

Closed
wants to merge 2 commits into from
Closed
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: 5 additions & 11 deletions 16 src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -537,13 +537,11 @@ public function children(/* string $selector = null */)
* @param string $attribute The attribute name
*
* @return string|null The attribute value or null if the attribute does not exist
*
* @throws \InvalidArgumentException When current node is empty
*/
public function attr($attribute)
{
if (!$this->nodes) {
throw new \InvalidArgumentException('The current node list is empty.');
return null;
}

$node = $this->getNode(0);
Expand All @@ -570,14 +568,12 @@ public function nodeName()
/**
* Returns the node value of the first node of the list.
*
* @return string The node value
*
* @throws \InvalidArgumentException When current node is empty
* @return string|null The node value
*/
public function text()
{
if (!$this->nodes) {
throw new \InvalidArgumentException('The current node list is empty.');
return null;
}

return $this->getNode(0)->nodeValue;
Expand All @@ -586,14 +582,12 @@ public function text()
/**
* Returns the first node of the list as HTML.
*
* @return string The node html
*
* @throws \InvalidArgumentException When current node is empty
* @return string|null The node html
*/
public function html()
{
if (!$this->nodes) {
throw new \InvalidArgumentException('The current node list is empty.');
return null;
}

$html = '';
Expand Down
24 changes: 3 additions & 21 deletions 24 src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,13 +350,7 @@ public function testReduce()
public function testAttr()
{
$this->assertEquals('first', $this->createTestCrawler()->filterXPath('//li')->attr('class'), '->attr() returns the attribute of the first element of the node list');

try {
$this->createTestCrawler()->filterXPath('//ol')->attr('class');
$this->fail('->attr() throws an \InvalidArgumentException if the node list is empty');
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->attr() throws an \InvalidArgumentException if the node list is empty');
}
$this->assertNull($this->createTestCrawler()->filterXPath('//ol')->attr('class'), '->attr() returns null if the node list is empty');
}

public function testMissingAttrValueIsNull()
Expand Down Expand Up @@ -385,26 +379,14 @@ public function testNodeName()
public function testText()
{
$this->assertEquals('One', $this->createTestCrawler()->filterXPath('//li')->text(), '->text() returns the node value of the first element of the node list');

try {
$this->createTestCrawler()->filterXPath('//ol')->text();
$this->fail('->text() throws an \InvalidArgumentException if the node list is empty');
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->text() throws an \InvalidArgumentException if the node list is empty');
}
$this->assertNull($this->createTestCrawler()->filterXPath('//ol')->text(), '->text() returns null if the node list is empty');
}

public function testHtml()
{
$this->assertEquals('<img alt="Bar">', $this->createTestCrawler()->filterXPath('//a[5]')->html());
$this->assertEquals('<input type="text" value="TextValue" name="TextName"><input type="submit" value="FooValue" name="FooName" id="FooId"><input type="button" value="BarValue" name="BarName" id="BarId"><button value="ButtonValue" name="ButtonName" id="ButtonId"></button>', trim(preg_replace('~>\s+<~', '><', $this->createTestCrawler()->filterXPath('//form[@id="FooFormId"]')->html())));

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');
}
$this->assertNull($this->createTestCrawler()->filterXPath('//ol')->html(), '->html() returns null if the node list is empty');
}

public function testExtract()
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.