8000 feature #42338 [DomCrawler] Added Crawler::innerText() method (Bilge) · symfony/symfony@0ddeeff · GitHub
[go: up one dir, main page]

Skip to content

Commit 0ddeeff

Browse files
committed
feature #42338 [DomCrawler] Added Crawler::innerText() method (Bilge)
This PR was squashed before being merged into the 5.4 branch. Discussion ---------- [DomCrawler] Added Crawler::innerText() method | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Fix #42294 | License | MIT | Doc PR | symfony/symfony-docs#... <!-- required for new features --> Adds a method to get the inner text that is directly descended from the current node only, ignoring text nodes in any child nodes. Commits ------- 4767694 [DomCrawler] Added Crawler::innerText() method
2 parents ca37eec + 4767694 commit 0ddeeff

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

src/Symfony/Component/DomCrawler/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.4
5+
---
6+
7+
* Add `Crawler::innerText` method.
8+
49
5.3
510
---
611

src/Symfony/Component/DomCrawler/Crawler.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,14 @@ public function text(string $default = null, bool $normalizeWhitespace = true)
634634
return $text;
635635
}
636636

637+
/**
638+
* Returns only the inner text that is the direct descendent of the current node, excluding any child nodes.
639+
*/
640+
public function innerText(): string
641+
{
642+
return $this->filterXPath('.//text()')->text();
643+
}
644+
637645
/**
638646
* Returns the first node of the list as HTML.
639647
*

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,18 @@ public function testText()
354354
$this->assertSame('my value', $this->createTestCrawler(null)->filterXPath('//ol')->text('my value'));
355355
}
356356

357+
/**
358+
* Tests that innerText() returns only text that is the direct descendent of the current node, in contrast to
359+
* text() that returns the text of all child nodes.
360+
*/
361+
public function testInnerText()
362+
{
363+
self::assertCount(1, $crawler = $this->createTestCrawler()->filterXPath('//*[@id="complex-element"]'));
364+
365+
self::assertSame('Parent text Child text', $crawler->text());
366+
self::assertSame('Parent text', $crawler->innerText());
367+
}
368+
357369
public function testHtml()
358370
{
359371
$this->assertEquals('<img alt="Bar">', $this->createTestCrawler()->filterXPath('//a[5]')->html());
@@ -1283,6 +1295,10 @@ public function createTestCrawler($uri = null)
12831295
<div id="child2" xmlns:foo="http://example.com"></div>
12841296
</div>
12851297
<div id="sibling"><img /></div>
1298+
<div id="complex-element">
1299+
Parent text
1300+
<span>Child text</span>
1301+
</div>
12861302
</body>
12871303
</html>
12881304
');

0 commit comments

Comments
 (0)
0