[DomCrawler] normalizeWhitespace should be true by default

This commit is contained in:
Kévin Dunglas 2019-10-28 14:12:30 +01:00
parent 42be5f8132
commit 54d46eef67
No known key found for this signature in database
GPG Key ID: 4D04EBEF06AAF3A6
4 changed files with 26 additions and 10 deletions

View File

@ -29,7 +29,7 @@ class CsrfFormLoginTest extends AbstractWebTestCase
$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 @@ class CsrfFormLoginTest extends AbstractWebTestCase
$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 @@ class CsrfFormLoginTest extends AbstractWebTestCase
$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 @@ class CsrfFormLoginTest extends AbstractWebTestCase
$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);
}

View File

@ -27,7 +27,7 @@ class FormLoginTest extends AbstractWebTestCase
$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 @@ class FormLoginTest extends AbstractWebTestCase
$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 @@ class FormLoginTest extends AbstractWebTestCase
$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 @@ class FormLoginTest extends AbstractWebTestCase
$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);
}

View File

@ -593,7 +593,7 @@ class Crawler implements \Countable, \IteratorAggregate
/**
* 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 @@ class Crawler implements \Countable, \IteratorAggregate
*
* @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 @@ class Crawler implements \Countable, \IteratorAggregate
$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));
}

View File

@ -258,6 +258,14 @@ abstract class AbstractCrawlerTest extends TestCase
$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());
}