2

When I get the content without DomCrawler, I get the html with custom tags like @click but when I use $this->crawler->filter('something')->html() DomCrawler is removing my @click tags.

Here an example without using DomCrawler:

enter image description here

And here is using DomCrawler:

enter image description here

As you can see, DomCrawler is removing all the @clicks, how can I stop this?

2 Answers2

0

Unfortunately, you can't. DomCrawler uses DOMDocument under the hood and will not allow the "@click". Also:

The DomCrawler will attempt to automatically fix your HTML to match the official specification.

The modifiers to disable this would be LIBXML_HTML_NOIMPLIED which is not used in the addHmlContent method of DomCrawler:

//... Symfony\Component\DomCrawler\Crawler.php
$dom->loadHTML($content);
// ...

and even calling @$dom->loadHTML($content, LIBXML_HTML_NOIMPLIED); would not work in your case.

Example:

$html = <<<TEST
   <html>
       <div class="test" @click="something"></div>
   </html>
TEST;
    dump($html);
    //<html>\n
    //    <div class="test" @click="something"></div>\n
    //</html>

    // Symfony Crawler
    $crawler = new \Symfony\Component\DomCrawler\Crawler();
    $crawler->addHtmlContent($html);
    dump($crawler->html());
    //<body>\n
    //    <div class="test"></div>\n
    //</body>

    // Custom crawler with LIBXML_HTML_NOIMPLIED
    $crawler = new \MyCrawler\Crawler();
    $crawler->addHtmlContent($html);
    dump($crawler->html());
    //  <div class="test"></div>
Michel
  • 950
  • 14
  • 23
0

You could replace the @click with x-on:click which has the exact same effect and is valid HTML at the same time.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
pacman
  • 156
  • 1
  • 4