8000 [DomCrawler] add a value() method, normalize whitespaces · symfony/symfony@f4d63fc · GitHub
[go: up one dir, main page]

Skip to content

Commit f4d63fc

Browse files
committed
[DomCrawler] add a value() method, normalize whitespaces
1 parent 85058f5 commit f4d63fc

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

src/Symfony/Component/DomCrawler/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
-----
66

77
* Added `Form::getName()` method.
8+
* Added an argument to the `Crawler::text()` method to opt-in normalizing whitespaces.
89

910
4.3.0
1011
-----

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -554,15 +554,17 @@ public function nodeName()
554554
}
555555

556556
/**
557-
* Returns the node value of the first node of the list.
557+
* Returns the text of the first node of the list.
558+
*
559+
* Pass true as the 2nd argument to normalize whitespaces.
558560
*
559561
* @param mixed $default When provided and the current node is empty, this value is returned and no exception is thrown
560562
*
561563
* @return string The node value
562564
*
563565
* @throws \InvalidArgumentException When current node is empty
564566
*/
565-
public function text(/* $default = null */)
567+
public function text(/* $default = null, $normalizeWhitespace = true */)
566568
{
567569
if (!$this->nodes) {
568570
if (0 < \func_num_args()) {
@@ -572,7 +574,13 @@ public function text(/* $default = null */)
572574
throw new \InvalidArgumentException('The current node list is empty.');
573575
}
574576

575-
return $this->getNode(0)->nodeValue;
577+
$text = $this->getNode(0)->nodeValue;
578+
579+
if (\func_num_args() > 1 && func_get_arg(1)) {
580+
return trim(preg_replace('/(?:\s{2,}+|[^\S ])/', ' ', $text));
581+
}
582+
583+
return $text;
576584
}
577585

578586
/**

src/Symfony/Component/DomCrawler/Tests/AbstractCrawlerTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ public function testEq()
257257
$this->assertCount(0, $crawler->eq(100), '->eq() returns an empty crawler if the nth node does not exist');
258258
}
259259

260+
public function testNormalizeWhiteSpace()
261+
{
262+
$crawler = $this->createTestCrawler()->filterXPath('//p');
263+
$this->assertEquals('Elsa', $crawler->text(null, true), '->text(null, true) returns the text with normalized whitespace');
264+
$this->assertEquals(' Elsa ', $crawler->text(null, false), '->text(null, true) returns the text with non normalized whitespace');
265+
}
266+
260267
public function testEach()
261268
{
262269
$data = $this->createTestCrawler()->filterXPath('//ul[1]/li')->each(function ($node, $i) {
@@ -1204,6 +1211,7 @@ public function createTestCrawler($uri = null)
12041211
<li>Two Bis</li>
12051212
<li>Three Bis</li>
12061213
</ul>
1214+
<p class="whitespace"> Elsa </p>
12071215
<div id="parent">
12081216
<div id="child"></div>
12091217
<div id="child2" xmlns:foo="http://example.com"></div>

0 commit comments

Comments
 (0)
0