Skip to content

Commit 71e8b51

Browse files
committed
Add a flag to text() instead of adding a method
1 parent 0793daf commit 71e8b51

File tree

3 files changed

+12
-25
lines changed

3 files changed

+12
-25
lines changed

src/Symfony/Component/DomCrawler/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CHANGELOG
44
3.4.0
55
-----
66

7-
* Added `flatText` method to get node value with spaces and newlines normalized.
7+
* Added flag to the `text` method, to strip surrounding and consecutive spaces/newlines.
88

99
3.1.0
1010
-----

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -558,36 +558,23 @@ public function nodeName()
558558
/**
559559
* Returns the node value of the first node of the list.
560560
*
561+
* @param bool $stripSpaces Whether to strip surrounding and consecutive spaces/newlines
562+
*
561563
* @return string The node value
562564
*
563565
* @throws \InvalidArgumentException When current node is empty
564566
*/
565-
public function text()
567+
public function text($stripSpaces = false)
566568
{
567569
if (!$this->nodes) {
568570
throw new \InvalidArgumentException('The current node list is empty.');
569571
}
570572

571-
return $this->getNode(0)->nodeValue;
572-
}
573-
574-
/**
575-
* Returns the flattened node value of the first node of the list.
576-
*
577-
* Surrounding spaces/newlines are removed,
578-
* and consecutive spaces/newlines are reduced to one space.
579-
*
580-
* @return string The flattened node value
581-
*
582-
* @throws \InvalidArgumentException When current node is empty
583-
*/
584-
public function flatText()
585-
{
586-
if (!$this->nodes) {
587-
throw new \InvalidArgumentException('The current node list is empty.');
573+
if ($stripSpaces) {
574+
return trim(preg_replace('/\s+/', ' ', $this->getNode(0)->nodeValue));
588575
}
589576

590-
return trim(preg_replace('/\s+/', ' ', $this->getNode(0)->nodeValue));
577+
return $this->getNode(0)->nodeValue;
591578
}
592579

593580
/**

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,15 +394,15 @@ public function testText()
394394
}
395395
}
396396

397-
public function testFlatText()
397+
public function testStrippedText()
398398
{
399-
$this->assertEquals('Fabien\'s Foo', $this->createTestCrawler()->filterXPath('//a[2]')->flatText(), '->flatText() returns the flattened node value of the first element of the node list');
399+
$this->assertEquals('Fabien\'s Foo', $this->createTestCrawler()->filterXPath('//a[2]')->text(true), '->text(true) returns the stripped node value of the first element of the node list');
400400

401401
try {
402-
$this->createTestCrawler()->filterXPath('//ol')->flatText();
403-
$this->fail('->flatText() throws an \InvalidArgumentException if the node list is empty');
402+
$this->createTestCrawler()->filterXPath('//ol')->text(true);
403+
$this->fail('->text(true) throws an \InvalidArgumentException if the node list is empty');
404404
} catch (\InvalidArgumentException $e) {
405-
$this->assertTrue(true, '->flatText() throws an \InvalidArgumentException if the node list is empty');
405+
$this->assertTrue(true, '->text(true) throws an \InvalidArgumentException if the node list is empty');
406406
}
407407
}
408408

0 commit comments

Comments
 (0)