Skip to content

[DomCrawler] Add method flatText() #23626

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Symfony/Component/DomCrawler/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

3.4.0
-----

* Added flag to the `text` method, to strip surrounding and consecutive spaces/newlines.

3.1.0
-----

Expand Down
8 changes: 7 additions & 1 deletion src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,16 +558,22 @@ public function nodeName()
/**
* Returns the node value of the first node of the list.
*
* @param bool $stripSpaces Whether to strip surrounding and consecutive spaces/newlines
*
* @return string The node value
*
* @throws \InvalidArgumentException When current node is empty
*/
public function text()
public function text($stripSpaces = false)
{
if (!$this->nodes) {
throw new \InvalidArgumentException('The current node list is empty.');
}

if ($stripSpaces) {
return trim(preg_replace('/\s+/', ' ', $this->getNode(0)->nodeValue));
}

return $this->getNode(0)->nodeValue;
}

Expand Down
12 changes: 12 additions & 0 deletions src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,18 @@ public function testText()
}
}

public function testStrippedText()
{
$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');

try {
$this->createTestCrawler()->filterXPath('//ol')->text(true);
$this->fail('->text(true) throws an \InvalidArgumentException if the node list is empty');
} catch (\InvalidArgumentException $e) {
$this->assertTrue(true, '->text(true) throws an \InvalidArgumentException if the node list is empty');
}
}

public function testHtml()
{
$this->assertEquals('<img alt="Bar">', $this->createTestCrawler()->filterXPath('//a[5]')->html());
Expand Down
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