8000 [DomCrawler] add a normalizeWhitespace argument to text() method by Simperfit · Pull Request #32440 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DomCrawler] add a normalizeWhitespace argument to text() method #32440

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

Merged
merged 1 commit into from
Sep 27, 2019
Merged
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
1 change: 1 addition & 0 deletions src/Symfony/Component/DomCrawler/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Added `Crawler::matches()` method.
* Added `Crawler::closest()` method.
* Added `Crawler::outerHtml()` method.
* Added an argument to the `Crawler::text()` method to opt-in normalizing whitespaces.

4.3.0
-----
Expand Down
14 changes: 11 additions & 3 deletions src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -591,15 +591,17 @@ public function nodeName()
}

/**
* Returns the node value of the first node of the list.
* Returns the text of the first node of the list.
*
* Pass true as the 2nd argument to normalize whitespaces.
*
* @param mixed $default When provided and the current node is empty, this value is returned and no exception is thrown
*
* @return string The node value
*
* @throws \InvalidArgumentException When current node is empty
*/
public function text(/* $default = null */)
public function text(/* $default = null, $normalizeWhitespace = true */)
{
if (!$this->nodes) {
if (0 < \func_num_args()) {
Expand All @@ -609,7 +611,13 @@ public function text(/* $default = null */)
throw new \InvalidArgumentException('The current node list is empty.');
}

return $this->getNode(0)->nodeValue;
$text = $this->getNode(0)->nodeValue;

if (\func_num_args() > 1 && func_get_arg(1)) {
return trim(preg_replace('/(?:\s{2,}+|[^\S ])/', ' ', $text));
}

return $text;
}

/**
Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/DomCrawler/Tests/AbstractCrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,14 @@ public function testEq()
$this->assertCount(0, $crawler->eq(100), '->eq() returns an empty crawler if the nth node does not exist');
}

public function testNormalizeWhiteSpace()
{
$crawler = $this->createTestCrawler()->filterXPath('//p');
$this->assertSame('Elsa <3', $crawler->text(null, true), '->text(null, true) returns the text with normalized whitespace');
$this->assertNotSame('Elsa <3', $crawler->text(null, false));
$this->assertNotSame('Elsa <3', $crawler->text());
}

public function testEach()
{
$data = $this->createTestCrawler()->filterXPath('//ul[1]/li')->each(function ($node, $i) {
Expand Down Expand Up @@ -1291,6 +1299,10 @@ public function createTestCrawler($uri = null)
<li>Two Bis</li>
<li>Three Bis</li>
</ul>
<p class="whitespace">
Elsa
&lt;3
</p>
<div id="parent">
<div id="child"></div>
<div id="child2" xmlns:foo="http://example.com"></div>
Expand Down
0