Skip to content

Commit 3b11a76

Browse files
committed
feature #34151 [DomCrawler] normalizeWhitespace should be true by default (dunglas)
This PR was squashed before being merged into the 4.4 branch (closes #34151). Discussion ---------- [DomCrawler] normalizeWhitespace should be true by default | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | n/a | License | MIT | Doc PR | n/a According to the [DOM](https://www.w3.org/TR/DOM-Level-3-Core/core.html#Text3-isElementContentWhitespace) and [WebDriver](https://www.w3.org/TR/webdriver/#get-element-text) specs, browsers always return the normalized text. In Panther, because of WebDriver, it's not even possible without dirty hacks to retrieve the "non normalized" text. For compatibility with Panther it's mandatory to set this new parameter (introduced in 4.4) to `true` by default. I propose to change the default value to true in 5.0, it has the benefit of: * being spec-compliant (in 5.0, text will be normalized by default) * being cleaner when using Panther (`$node->text()` instead of `$node->text(null, true)`, passing true is mandatory because Panther doesn't support retrieving the non-normalized text) For backward compatible with 4.x versions, if no argument is passed and the returned text isn't the same than the normalized one, a notice is triggered. Commits ------- 54d46ee [DomCrawler] normalizeWhitespace should be true by default
2 parents 42be5f8 + 54d46ee commit 3b11a76

File tree

4 files changed

+26
-10
lines changed

4 files changed

+26
-10
lines changed

src/Symfony/Bundle/SecurityBundle/Tests/Functional/CsrfFormLoginTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function testFormLoginAndLogoutWithCsrfTokens($config)
2929

3030
$crawler = $client->followRedirect();
3131

32-
$text = $crawler->text();
32+
$text = $crawler->text(null, true);
3333
$this->assertStringContainsString('Hello johannes!', $text);
3434
$this->assertStringContainsString('You\'re browsing to path "/profile".', $text);
3535

@@ -56,7 +56,7 @@ public function testFormLoginWithInvalidCsrfToken($config)
5656

5757
$this->assertRedirect($client->getResponse(), '/login');
5858

59-
$text = $client->followRedirect()->text();
59+
$text = $client->followRedirect()->text(null, true);
6060
$this->assertStringContainsString('Invalid CSRF token.', $text);
6161
}
6262

@@ -75,7 +75,7 @@ public function testFormLoginWithCustomTargetPath($config)
7575

7676
$this->assertRedirect($client->getResponse(), '/foo');
7777

78-
$text = $client->followRedirect()->text();
78+
$text = $client->followRedirect()->text(null, true);
7979
$this->assertStringContainsString('Hello johannes!', $text);
8080
$this->assertStringContainsString('You\'re browsing to path "/foo".', $text);
8181
}
@@ -96,7 +96,7 @@ public function testFormLoginRedirectsToProtectedResourceAfterLogin($config)
9696
$client->submit($form);
9797
$this->assertRedirect($client->getResponse(), '/protected-resource');
9898

99-
$text = $client->followRedirect()->text();
99+
$text = $client->followRedirect()->text(null, true);
100100
$this->assertStringContainsString('Hello johannes!', $text);
101101
$this->assertStringContainsString('You\'re browsing to path "/protected-resource".', $text);
102102
}

src/Symfony/Bundle/SecurityBundle/Tests/Functional/FormLoginTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function testFormLogin($config)
2727

2828
$this->assertRedirect($client->getResponse(), '/profile');
2929

30-
$text = $client->followRedirect()->text();
30+
$text = $client->followRedirect()->text(null, true);
3131
$this->assertStringContainsString('Hello johannes!', $text);
3232
$this->assertStringContainsString('You\'re browsing to path "/profile".', $text);
3333
}
@@ -47,7 +47,7 @@ public function testFormLogout($config)
4747
$this->assertRedirect($client->getResponse(), '/profile');
4848

4949
$crawler = $client->followRedirect();
50-
$text = $crawler->text();
50+
$text = $crawler->text(null, true);
5151

5252
$this->assertStringContainsString('Hello johannes!', $text);
5353
$this->assertStringContainsString('You\'re browsing to path "/profile".', $text);
@@ -80,7 +80,7 @@ public function testFormLoginWithCustomTargetPath($config)
8080

8181
$this->assertRedirect($client->getResponse(), '/foo');
8282

83-
$text = $client->followRedirect()->text();
83+
$text = $client->followRedirect()->text(null, true);
8484
$this->assertStringContainsString('Hello johannes!', $text);
8585
$this->assertStringContainsString('You\'re browsing to path "/foo".', $text);
8686
}
@@ -101,7 +101,7 @@ public function testFormLoginRedirectsToProtectedResourceAfterLogin($config)
101101
$client->submit($form);
102102
$this->assertRedirect($client->getResponse(), '/protected_resource');
103103

104-
$text = $client->followRedirect()->text();
104+
$text = $client->followRedirect()->text(null, true);
105105
$this->assertStringContainsString('Hello johannes!', $text);
106106
$this->assertStringContainsString('You\'re browsing to path "/protected_resource".', $text);
107107
}

src/Symfony/Component/DomCrawler/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
}

src/Symfony/Component/DomCrawler/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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy