Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit36068f1

Browse files
committed
Added Crawler::innerText() method.
1 parent350674e commit36068f1

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-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+
* Added`Crawler::innerText` method.
8+
49
5.3
510
---
611

‎src/Symfony/Component/DomCrawler/Crawler.php‎

Lines changed: 10 additions & 0 deletions
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+
publicfunctioninnerText():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‎

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+
publicfunctiontestInnerText():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
publicfunctiontestHtml()
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)

[8]ページ先頭

©2009-2025 Movatter.jp