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

Conversation

Bilge
Copy link
Contributor

@Bilge Bilge commented Aug 1, 2021

Q A
Branch? 5.4
Bug fix? no
New feature? yes
Deprecations? no
Tickets Fix #42294
License MIT
Doc PR symfony/symfony-docs#...

Adds a method to get the inner text that is directly descended from the current node only, ignoring text nodes in any child nodes.

Comment on lines +357 to +360
/**
* 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.
*/
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

@carsonbot carsonbot changed the title Added Crawler::innerText() method [DomCrawler] Added Crawler::innerText() method Aug 1, 2021
@OskarStark OskarStark added this to the 5.4 milestone Aug 1, 2021
*/
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".

@fabpot
Copy link
Member

fabpot commented Aug 26, 2021

@Bilge Can you take suggestions into account?

@Bilge
Copy link
Contributor Author

Bilge commented Aug 26, 2021

@fabpot Suggestions have been taken into account.

@fabpot
Copy link
Member

fabpot commented Sep 21, 2021

Thank you @Bilge.

@fabpot fabpot merged commit 0ddeeff into symfony:5.4 Sep 21, 2021
@Bilge Bilge deleted the innerText branch October 2, 2021 09:54
This was referenced Nov 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Get direct descendent text only when calling text() on a node.
5 participants