Skip to content

DOMCrawler: removing extra break lines and spaces? #21302

Closed
@jpetitcolas

Description

@jpetitcolas
Q A
Bug report? no
Feature request? yes
BC Break report? no
RFC? yes
Symfony version 3.1.7

We are currently implementing a functional test, testing content of the following code:

<p class="availability">
    <span class="available-from">
        {{ 'video.available.from'|trans }}
        {{ video.rights.begin|localizeddate('long', 'none') }}
    </span>

    <span class="available-to">
        {{ 'video.available.to'|trans }}
        {{ video.rights.end|localizeddate('long', 'none') }}
    </span>
</p>

We need to test content of the .availability element. Hence, we use something like:

$this->assertEquals($expectedAvailability, $crawler->filter('.availability')->text());

However, as there are some line breaks, this test fails as following:

-'Available from January 6, 2017 to June 14, 2017'
+'
+    
+        Available from
+        January 6, 2017
+    
+        to
+        June 14, 2017
+    
+'

We are used to fix this output using a code like:

$text = $element->text();
$text = preg_replace("/(\n|\t)/", '', $text);
$text = preg_replace("/ {2,}/", ' ', $text);
$this->assertEquals($expectedOutput, trim($text));

Yet, it may be a good idea to add a new $cleanedOutput = false parameter to the text() method?

Metadata

Metadata

Assignees

No one assigned

    Labels

    DXDX = Developer eXperience (anything that improves the experience of using Symfony)DomCrawlerFeature

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions