Jimmix
Jimmix

Reputation: 6506

Symfony Dom Crawler Missing Node, Inconsistent Behaviour

With this code:

use Symfony\Component\DomCrawler\Crawler;
require_once(__DIR__ . '/../vendor/autoload.php');

$html = <<<'HTML'
<!DOCTYPE html>

<html>
    <body>
        <p class="message">Hello World!</p>
        <p>Hello Crawler!</p>
        <p>OUTSIDE
            <span>
                Child SPAN
            </span>
            <div>
                Child DIV
            </div>
            <p>
                Child PARAGRAPH
            </p>
        </p>
    </body>
</html>

HTML;

$crawler = new Crawler($html);
$crawlerFiltered = $crawler->filter('body > p');

$results = [];
$childResults = [];
for ($i=0; $i<count($crawlerFiltered); $i++) {
    $results[] = $crawlerFiltered->eq($i)->html();

    $children = $crawlerFiltered->eq($i)->children();
    if (count($children)) {
        for ($j=0; $j<count($children); $j++) {
            $childResults[] = $children->eq($j)->html();
        }
    }
}

echo 'Parent Nodes:' . PHP_EOL;
var_export($results);
echo PHP_EOL;
echo 'Child Nodes:' . PHP_EOL;
var_export($childResults);

I get result:

Parent Nodes:
array (
  0 => 'Hello World!',
  1 => 'Hello Crawler!',
  2 => 'OUTSIDE
            <span>
                Child SPAN
            </span>
            ',
  3 => '
                Child PARAGRAPH
            ',
)
Child Nodes:
array (
  0 => '
                Child SPAN
            ',
)

That represents following problems:

  1. Child result: no DIV or P (only inline tags)
  2. Parent result: PHARAGRAPH is without tag, inconsistent with SPAN
  3. Parent result: should contain only first p because second p (PHARAGRAPH) does not have body as parent but p

do you know why is that and how to fix problems as above?

Upvotes: 1

Views: 417

Answers (1)

miken32
miken32

Reputation: 42714

The documentation for this component states:

Note

The DomCrawler will attempt to automatically fix your HTML to match the official specification. For example, if you nest a <p> tag inside another <p> tag, it will be moved to be a sibling of the parent tag. This is expected and is part of the HTML5 spec.

You may have better luck using the built-in DomDocument classes. Most HTML parsers are designed to deal with "tag soup" and will try to correct perceived problems.

Upvotes: 1

Related Questions