Skip to content

[DomCrawler] Add method flatText() #23626

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 1 commit
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
Prev Previous commit
Add a flag to text() instead of adding a method
  • Loading branch information
vlakoff committed Oct 3, 2017
commit 71e8b51d184a6d743bb1367e375bbdd4b5af0ee0
2 changes: 1 addition & 1 deletion src/Symfony/Component/DomCrawler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CHANGELOG
3.4.0
-----

* Added `flatText` method to get node value with spaces and newlines normalized.
* Added flag to the `text` method, to strip surrounding and consecutive spaces/newlines.

3.1.0
-----
Expand Down
25 changes: 6 additions & 19 deletions src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,36 +558,23 @@ public function nodeName()
/**
* Returns the node value of the first node of the list.
*
* @param bool $stripSpaces Whether to strip surrounding and consecutive spaces/newlines
*
* @return string The node value
*
* @throws \InvalidArgumentException When current node is empty
*/
public function text()
public function text($stripSpaces = false)
{
if (!$this->nodes) {
throw new \InvalidArgumentException('The current node list is empty.');
}

return $this->getNode(0)->nodeValue;
}

/**
* Returns the flattened node value of the first node of the list.
*
* Surrounding spaces/newlines are removed,
* and consecutive spaces/newlines are reduced to one space.
*
* @return string The flattened node value
*
* @throws \InvalidArgumentException When current node is empty
*/
public function flatText()
{
if (!$this->nodes) {
throw new \InvalidArgumentException('The current node list is empty.');
if ($stripSpaces) {
return trim(preg_replace('/\s+/', ' ', $this->getNode(0)->nodeValue));
}

return trim(preg_replace('/\s+/', ' ', $this->getNode(0)->nodeValue));
return $this->getNode(0)->nodeValue;
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,15 +394,15 @@ public function testText()
}
}

public function testFlatText()
public function testStrippedText()
{
$this->assertEquals('Fabien\'s Foo', $this->createTestCrawler()->filterXPath('//a[2]')->flatText(), '->flatText() returns the flattened node value of the first element of the node list');
$this->assertEquals('Fabien\'s Foo', $this->createTestCrawler()->filterXPath('//a[2]')->text(true), '->text(true) returns the stripped node value of the first element of the node list');

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

Expand Down