Skip to content

[DomCrawler] Added Crawler::innerText() method #42338

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

Merged
merged 1 commit into from
Sep 21, 2021
Merged
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
=========

5.4
---

* Add `Crawler::innerText` method.

5.3
---

Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/DomCrawler/Crawler.php
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,14 @@ public function text(string $default = null, bool $normalizeWhitespace = true)
return $text;
}

/**
* Returns only the inner text that is the direct descendent of the current node, excluding any child nodes.
*/
public function innerText(): string
{
return $this->filterXPath('.//text()')->text();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can also be expressed as child::node()/text(). I don't know which is "better".

}

/**
* Returns the first node of the list as HTML.
*
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/DomCrawler/Tests/AbstractCrawlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,18 @@ public function testText()
$this->assertSame('my value', $this->createTestCrawler(null)->filterXPath('//ol')->text('my value'));
}

/**
* Tests that innerText() returns only text that is the direct descendent of the current node, in contrast to
* text() that returns the text of all child nodes.
*/
Comment on lines +357 to +360
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment is not needed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A test scenario is not needed, in a technical sense, but it is needed in a maintainability sense. It conveys what the original intent of the test is, which helps greatly in determining how to fix it when it should break in future.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my POV we can keep it, but testInnerText() method name makes clear what's the point here 🤷‍♂️

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps, but I think writing test scenarios is a good habit to be into.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we use this anywhere else in the DomCrawler's test suite? If not, I am in favour of removing the comment and, if necessary, change the method name to something like testInnerTextReturnsContentOfDirectDescendantTextNodesExclusively().

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strongly disagree. Prose is much easier to read as space-separated words instead of stuffing entire sentences into camel-cased method names.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in this case testInnerText() is enough

public function testInnerText()
{
self::assertCount(1, $crawler = $this->createTestCrawler()->filterXPath('//*[@id="complex-element"]'));

self::assertSame('Parent text Child text', $crawler->text());
self::assertSame('Parent text', $crawler->innerText());
}

public function testHtml()
{
$this->assertEquals('<img alt="Bar">', $this->createTestCrawler()->filterXPath('//a[5]')->html());
Expand Down Expand Up @@ -1283,6 +1295,10 @@ public function createTestCrawler($uri = null)
<div id="child2" xmlns:foo="http://example.com"></div>
</div>
<div id="sibling"><img /></div>
<div id="complex-element">
Parent text
<span>Child text</span>
</div>
</body>
</html>
');
Expand Down