8000 feature #32440 [DomCrawler] add a normalizeWhitespace argument to tex… · Koc/symfony@7d6b657 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7d6b657

Browse files
feature symfony#32440 [DomCrawler] add a normalizeWhitespace argument to text() method (Simperfit)
This PR was merged into the 4.4 branch. Discussion ---------- [DomCrawler] add a normalizeWhitespace argument to text() method | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes <!-- please update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | symfony#21302, symfony#23626 <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | todo <!-- required for new features --> <!-- Replace this notice by a short README for your feature/bugfix. This will help people understand your PR and can be used as a start for the documentation. Additionally (see https://symfony.com/roadmap): - Bug fixes must be submitted against the lowest maintained branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against branch 4.4. - Legacy code removals go to the master branch. --> I've taken symfony#24412, rebased with 4.4, fixed the comments in the PR. cc @xabbuh @stof @nicolas-grekas Commits ------- 1746718 [DomCrawler] add a value() method, normalize whitespaces
2 parents 50da16b + 1746718 commit 7d6b657

File tree

3 files changed

+24
-3
lines changed

3 files changed

+24
-3
lines changed

src/Symfony/Component/DomCrawler/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ CHANGELOG
88
* Added `Crawler::matches()` method.
99
* Added `Crawler::closest()` method.
1010
* Added `Crawler::outerHtml()` method.
11+
* Added an argument to the `Crawler::text()` method to opt-in normalizing whitespaces.
1112

1213
4.3.0
1314
-----

src/Symfony/Component/DomCrawler/Crawler.php

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

593593
/**
594-
* Returns the node value of the first node of the list.
594+
* Returns the text of the first node of the list.
595+
*
596+
* Pass true as the 2nd argument to normalize whitespaces.
595597
*
596598
* @param mixed $default When provided and the current node is empty, this value is returned and no exception is thrown
597599
*
598600
* @return string The node value
599601
*
600602
* @throws \InvalidArgumentException When current node is empty
601603
*/
602-
public function text(/* $default = null */)
604+
public function text(/* $default = null, $normalizeWhitespace = true */)
603605
{
604606
if (!$this->nodes) {
605607
if (0 < \func_num_args()) {
@@ -609,7 +611,13 @@ public function text(/* $default = null */)
609611
throw new \InvalidArgumentException('The current node list is empty.');
610612
}
611613

612-
return $this->getNode(0)->nodeValue;
614+
$text = $this->getNode(0)->nodeValue;
615+
616+
if (\func_num_args() > 1 && func_get_arg(1)) {
617+
return trim(preg_replace('/(?:\s{2,}+|[^\S ])/', ' ', $text));
618+
}
619+
620+
return $text;
613621
}
614622

615623
/**

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,14 @@ public function testEq()
253253
$this->assertCount(0, $crawler->eq(100), '->eq() returns an empty crawler if the nth node does not exist');
254254
}
255255

256+
public function testNormalizeWhiteSpace()
257+
{
258+
$crawler = $this->createTestCrawler()->filterXPath('//p');
259+
$this->assertSame('Elsa <3', $crawler->text(null, true), '->text(null, true) returns the text with normalized whitespace');
260+
$this->assertNotSame('Elsa <3', $crawler->text(null, false));
261+
$this->assertNotSame('Elsa <3', $crawler->text());
262+
}
263+
256264
public function testEach()
257265
{
258266
$data = $this->createTestCrawler()->filterXPath('//ul[1]/li')->each(function ($node, $i) {
@@ -1291,6 +1299,10 @@ public function createTestCrawler($uri = null)
12911299
<li>Two Bis</li>
12921300
<li>Three Bis</li>
12931301
</ul>
1302+
<p class="whitespace">
1303+
Elsa
1304+
&lt;3
1305+
</p>
12941306
<div id="parent">
12951307
<div id="child"></div>
12961308
<div id="child2" xmlns:foo="http://example.com"></div>

0 commit comments

Comments
 (0)
0