diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/CsrfFormLoginTest.php b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/CsrfFormLoginTest.php index dc2aeaec50541..5b2999fed0d2e 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/CsrfFormLoginTest.php +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/CsrfFormLoginTest.php @@ -29,7 +29,7 @@ public function testFormLoginAndLogoutWithCsrfTokens($config) $crawler = $client->followRedirect(); - $text = $crawler->text(); + $text = $crawler->text(null, true); $this->assertStringContainsString('Hello johannes!', $text); $this->assertStringContainsString('You\'re browsing to path "/profile".', $text); @@ -56,7 +56,7 @@ public function testFormLoginWithInvalidCsrfToken($config) $this->assertRedirect($client->getResponse(), '/login'); - $text = $client->followRedirect()->text(); + $text = $client->followRedirect()->text(null, true); $this->assertStringContainsString('Invalid CSRF token.', $text); } @@ -75,7 +75,7 @@ public function testFormLoginWithCustomTargetPath($config) $this->assertRedirect($client->getResponse(), '/foo'); - $text = $client->followRedirect()->text(); + $text = $client->followRedirect()->text(null, true); $this->assertStringContainsString('Hello johannes!', $text); $this->assertStringContainsString('You\'re browsing to path "/foo".', $text); } @@ -96,7 +96,7 @@ public function testFormLoginRedirectsToProtectedResourceAfterLogin($config) $client->submit($form); $this->assertRedirect($client->getResponse(), '/protected-resource'); - $text = $client->followRedirect()->text(); + $text = $client->followRedirect()->text(null, true); $this->assertStringContainsString('Hello johannes!', $text); $this->assertStringContainsString('You\'re browsing to path "/protected-resource".', $text); } diff --git a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/FormLoginTest.php b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/FormLoginTest.php index 1959d05576014..641ef0e519a1d 100644 --- a/src/Symfony/Bundle/SecurityBundle/Tests/Functional/FormLoginTest.php +++ b/src/Symfony/Bundle/SecurityBundle/Tests/Functional/FormLoginTest.php @@ -27,7 +27,7 @@ public function testFormLogin($config) $this->assertRedirect($client->getResponse(), '/profile'); - $text = $client->followRedirect()->text(); + $text = $client->followRedirect()->text(null, true); $this->assertStringContainsString('Hello johannes!', $text); $this->assertStringContainsString('You\'re browsing to path "/profile".', $text); } @@ -47,7 +47,7 @@ public function testFormLogout($config) $this->assertRedirect($client->getResponse(), '/profile'); $crawler = $client->followRedirect(); - $text = $crawler->text(); + $text = $crawler->text(null, true); $this->assertStringContainsString('Hello johannes!', $text); $this->assertStringContainsString('You\'re browsing to path "/profile".', $text); @@ -80,7 +80,7 @@ public function testFormLoginWithCustomTargetPath($config) $this->assertRedirect($client->getResponse(), '/foo'); - $text = $client->followRedirect()->text(); + $text = $client->followRedirect()->text(null, true); $this->assertStringContainsString('Hello johannes!', $text); $this->assertStringContainsString('You\'re browsing to path "/foo".', $text); } @@ -101,7 +101,7 @@ public function testFormLoginRedirectsToProtectedResourceAfterLogin($config) $client->submit($form); $this->assertRedirect($client->getResponse(), '/protected_resource'); - $text = $client->followRedirect()->text(); + $text = $client->followRedirect()->text(null, true); $this->assertStringContainsString('Hello johannes!', $text); $this->assertStringContainsString('You\'re browsing to path "/protected_resource".', $text); } diff --git a/src/Symfony/Component/DomCrawler/Crawler.php b/src/Symfony/Component/DomCrawler/Crawler.php index f176d1c6466e9..1be828e999a91 100644 --- a/src/Symfony/Component/DomCrawler/Crawler.php +++ b/src/Symfony/Component/DomCrawler/Crawler.php @@ -593,7 +593,7 @@ public function nodeName() /** * Returns the text of the first node of the list. * - * Pass true as the 2nd argument to normalize whitespaces. + * Pass true as the second argument to normalize whitespaces. * * @param string|null $default When not null: the value to return when the current node is empty * @param bool $normalizeWhitespace Whether whitespaces should be trimmed and normalized to single spaces @@ -602,7 +602,7 @@ public function nodeName() * * @throws \InvalidArgumentException When current node is empty */ - public function text(/* string $default = null, bool $normalizeWhitespace = false */) + public function text(/* string $default = null, bool $normalizeWhitespace = true */) { if (!$this->nodes) { if (0 < \func_num_args() && null !== func_get_arg(0)) { @@ -614,6 +614,14 @@ public function text(/* string $default = null, bool $normalizeWhitespace = fals $text = $this->getNode(0)->nodeValue; + if (\func_num_args() <= 1) { + if (trim(preg_replace('/(?:\s{2,}+|[^\S ])/', ' ', $text)) !== $text) { + @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); + } + + return $text; + } + if (\func_num_args() > 1 && func_get_arg(1)) { return trim(preg_replace('/(?:\s{2,}+|[^\S ])/', ' ', $text)); } diff --git a/src/Symfony/Component/DomCrawler/Tests/AbstractCrawlerTest.php b/src/Symfony/Component/DomCrawler/Tests/AbstractCrawlerTest.php index 4a60ca83b0b4b..266b4be6f513f 100644 --- a/src/Symfony/Component/DomCrawler/Tests/AbstractCrawlerTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/AbstractCrawlerTest.php @@ -258,6 +258,14 @@ 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)); + } + + /** + * @group legacy + */ + public function testLegacyNormalizeWhiteSpace() + { + $crawler = $this->createTestCrawler()->filterXPath('//p'); $this->assertNotSame('Elsa <3', $crawler->text()); }