Skip to content

Commit 36bbcab

Browse files
committed
[DomCrawler] normalizeWhitespace should be true by default
1 parent 39fc6cf commit 36bbcab

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

Crawler.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ public function nodeName()
593593
/**
594594
* Returns the text of the first node of the list.
595595
*
596-
* Pass true as the 2nd argument to normalize whitespaces.
596+
* Pass true as the second argument to normalize whitespaces.
597597
*
598598
* @param string|null $default When not null: the value to return when the current node is empty
599599
* @param bool $normalizeWhitespace Whether whitespaces should be trimmed and normalized to single spaces
@@ -602,7 +602,7 @@ public function nodeName()
602602
*
603603
* @throws \InvalidArgumentException When current node is empty
604604
*/
605-
public function text(/* string $default = null, bool $normalizeWhitespace = false */)
605+
public function text(/* string $default = null, bool $normalizeWhitespace = true */)
606606
{
607607
if (!$this->nodes) {
608608
if (0 < \func_num_args() && null !== func_get_arg(0)) {
@@ -614,6 +614,14 @@ public function text(/* string $default = null, bool $normalizeWhitespace = fals
614614

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

617+
if (\func_num_args() <= 1) {
618+
if (trim(preg_replace('/(?:\s{2,}+|[^\S ])/', ' ', $text)) !== $text) {
619+
@trigger_error(sprintf('"%s()" will normalize whitespaces by default in Symfony 5.0, set the second "$normalizeWhitespace" argument to false to retrieve the non-normalized version of the text.', __METHOD__), E_USER_DEPRECATED);
620+
}
621+
622+
return $text;
623+
}
624+
617625
if (\func_num_args() > 1 && func_get_arg(1)) {
618626
return trim(preg_replace('/(?:\s{2,}+|[^\S ])/', ' ', $text));
619627
}

Tests/AbstractCrawlerTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,14 @@ public function testNormalizeWhiteSpace()
258258
$crawler = $this->createTestCrawler()->filterXPath('//p');
259259
$this->assertSame('Elsa <3', $crawler->text(null, true), '->text(null, true) returns the text with normalized whitespace');
260260
$this->assertNotSame('Elsa <3', $crawler->text(null, false));
261+
}
262+
263+
/**
264+
* @group legacy
265+
*/
266+
public function testLegacyNormalizeWhiteSpace()
267+
{
268+
$crawler = $this->createTestCrawler()->filterXPath('//p');
261269
$this->assertNotSame('Elsa <3', $crawler->text());
262270
}
263271

0 commit comments

Comments
 (0)