DomCrawler 正在删除 html 的一部分

DomCrawler is removing part of the html

当我在没有 DomCrawler 的情况下获取内容时,我得到的是带有自定义标签的 html,例如 @click,但是当我使用 $this->crawler->filter('something')->html() DomCrawler 时,它正在删除我的 @click 标签。

这里是一个不使用 DomCrawler 的例子:

这里使用的是 DomCrawler:

如您所见,DomCrawler 正在删除所有@clicks,我该如何阻止它?

很遗憾,你不能。 DomCrawler uses DOMDocument 在幕后,将不允许“@click”。还有:

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

禁用此功能的修饰符是 LIBXML_HTML_NOIMPLIED,它未在 DomCrawler 的 addHmlContent 方法中使用:

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

甚至调用 @$dom->loadHTML($content, LIBXML_HTML_NOIMPLIED); 在你的情况下都不起作用。

示例:

$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>