Skip to content

DomCrawler component Crawler::innerText() doesn't get text after a child node #48682

Closed
@otsch

Description

@otsch

Symfony version(s) affected

5.4

Description

The test case for this method basically is:

<div id="complex-element">
    Parent text
    <span>Child text</span>
</div>

Getting $crawler->text() then returns Parent text Child text and $crawler->innerText() returns Parent text. So far, so good.

If I don't misunderstand anything, in case of:

<div id="complex-element">
    Parent text
    <span>Child text</span>
    More parent text
</div>

the return value of $crawler->innerText() should be Parent text More parent text. But it's still just Parent text.

I'll provide a pull request to fix this in a minute.

How to reproduce

In the Tests/AbstractCrawlerTest.php test file, adapt the HTML in the createTestCrawler() method from:

<div id="complex-element">
    Parent text
    <span>Child text</span>
</div>

to

<div id="complex-element">
    Parent text
    <span>Child text</span>
    More parent text
</div>

And the testInnerText() method accordingly to:

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

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

Possible Solution

#48684

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions