8000 Added Crawler::innerText() method. · symfony/symfony@36068f1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 36068f1

Browse files
committed
Added Crawler::innerText() method.
1 parent 350674e commit 36068f1

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/Symfony/Component/DomCrawler/CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

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

src/Symfony/Component/DomCrawler/Crawler.php

+10
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,16 @@ 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+
* @return string Inner text.
641+
*/
642+
public function innerText(): string
643+
{
644+
return $this->filterXPath('.//text()')->text();
645+
}
646+
637647
/**
638648
* Returns the first node of the list as HTML.
639649
*

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

+16
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(): void
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