From 0793daf80f70c5438eebf7da2192d5ac4023904b Mon Sep 17 00:00:00 2001 From: vlakoff Date: Sat, 22 Jul 2017 15:45:20 +0200 Subject: [PATCH 1/2] [DomCrawler] Add method flatText() --- src/Symfony/Component/DomCrawler/CHANGELOG.md | 5 +++++ src/Symfony/Component/DomCrawler/Crawler.php | 19 +++++++++++++++++++ .../DomCrawler/Tests/CrawlerTest.php | 12 ++++++++++++ 3 files changed, 36 insertions(+) diff --git a/src/Symfony/Component/DomCrawler/CHANGELOG.md b/src/Symfony/Component/DomCrawler/CHANGELOG.md index e65176f5ac0b4..29aeb3b3e80e8 100644 --- a/src/Symfony/Component/DomCrawler/CHANGELOG.md +++ b/src/Symfony/Component/DomCrawler/CHANGELOG.md @@ -1,6 +1,11 @@ CHANGELOG ========= +3.4.0 +----- + +* Added `flatText` method to get node value with spaces and newlines normalized. + 3.1.0 ----- diff --git a/src/Symfony/Component/DomCrawler/Crawler.php b/src/Symfony/Component/DomCrawler/Crawler.php index 5d201dff1eef6..7e1fc21ed007e 100644 --- a/src/Symfony/Component/DomCrawler/Crawler.php +++ b/src/Symfony/Component/DomCrawler/Crawler.php @@ -571,6 +571,25 @@ public function text() return $this->getNode(0)->nodeValue; } + /** + * Returns the flattened node value of the first node of the list. + * + * Surrounding spaces/newlines are removed, + * and consecutive spaces/newlines are reduced to one space. + * + * @return string The flattened node value + * + * @throws \InvalidArgumentException When current node is empty + */ + public function flatText() + { + if (!$this->nodes) { + throw new \InvalidArgumentException('The current node list is empty.'); + } + + return trim(preg_replace('/\s+/', ' ', $this->getNode(0)->nodeValue)); + } + /** * Returns the first node of the list as HTML. * diff --git a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php index c787869ca7940..0d831f725c9f1 100644 --- a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php @@ -394,6 +394,18 @@ public function testText() } } + public function testFlatText() + { + $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'); + + try { + $this->createTestCrawler()->filterXPath('//ol')->flatText(); + $this->fail('->flatText() throws an \InvalidArgumentException if the node list is empty'); + } catch (\InvalidArgumentException $e) { + $this->assertTrue(true, '->flatText() throws an \InvalidArgumentException if the node list is empty'); + } + } + public function testHtml() { $this->assertEquals('Bar', $this->createTestCrawler()->filterXPath('//a[5]')->html()); From 71e8b51d184a6d743bb1367e375bbdd4b5af0ee0 Mon Sep 17 00:00:00 2001 From: vlakoff Date: Tue, 3 Oct 2017 16:40:32 +0200 Subject: [PATCH 2/2] Add a flag to text() instead of adding a method --- src/Symfony/Component/DomCrawler/CHANGELOG.md | 2 +- src/Symfony/Component/DomCrawler/Crawler.php | 25 +++++-------------- .../DomCrawler/Tests/CrawlerTest.php | 10 ++++---- 3 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/Symfony/Component/DomCrawler/CHANGELOG.md b/src/Symfony/Component/DomCrawler/CHANGELOG.md index 29aeb3b3e80e8..455abb0618d44 100644 --- a/src/Symfony/Component/DomCrawler/CHANGELOG.md +++ b/src/Symfony/Component/DomCrawler/CHANGELOG.md @@ -4,7 +4,7 @@ CHANGELOG 3.4.0 ----- -* Added `flatText` method to get node value with spaces and newlines normalized. +* Added flag to the `text` method, to strip surrounding and consecutive spaces/newlines. 3.1.0 ----- diff --git a/src/Symfony/Component/DomCrawler/Crawler.php b/src/Symfony/Component/DomCrawler/Crawler.php index 7e1fc21ed007e..5ffe6a1a5f891 100644 --- a/src/Symfony/Component/DomCrawler/Crawler.php +++ b/src/Symfony/Component/DomCrawler/Crawler.php @@ -558,36 +558,23 @@ 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.'); } - return $this->getNode(0)->nodeValue; - } - - /** - * Returns the flattened node value of the first node of the list. - * - * Surrounding spaces/newlines are removed, - * and consecutive spaces/newlines are reduced to one space. - * - * @return string The flattened node value - * - * @throws \InvalidArgumentException When current node is empty - */ - public function flatText() - { - if (!$this->nodes) { - throw new \InvalidArgumentException('The current node list is empty.'); + if ($stripSpaces) { + return trim(preg_replace('/\s+/', ' ', $this->getNode(0)->nodeValue)); } - return trim(preg_replace('/\s+/', ' ', $this->getNode(0)->nodeValue)); + return $this->getNode(0)->nodeValue; } /** diff --git a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php index 0d831f725c9f1..fcd2cc7f9368c 100644 --- a/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php +++ b/src/Symfony/Component/DomCrawler/Tests/CrawlerTest.php @@ -394,15 +394,15 @@ public function testText() } } - public function testFlatText() + public function testStrippedText() { - $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'); + $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')->flatText(); - $this->fail('->flatText() throws an \InvalidArgumentException if the node list is empty'); + $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, '->flatText() throws an \InvalidArgumentException if the node list is empty'); + $this->assertTrue(true, '->text(true) throws an \InvalidArgumentException if the node list is empty'); } } 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