Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove() doesn't work on root elements in an html part #25

Open
Defcon0 opened this issue Jul 31, 2017 · 3 comments
Open

remove() doesn't work on root elements in an html part #25

Defcon0 opened this issue Jul 31, 2017 · 3 comments

Comments

@Defcon0
Copy link

Defcon0 commented Jul 31, 2017

When doing the following, the div isn't removed from the html:

$objNode        = new HtmlPageCrawler('<div class="test"><p>Hallo</p></div>');
$objNode->filter('.test')->remove();

$x = $objNode->saveHTML();

When doing $objNode->filter('p')->remove(); the p-elements are correctly removed. Also when wrapping the div in another div it also works.

So it seems that I cannot remove root elements, can I? At least a hint in the comment would've been nice ;-) Maybe the bug can be fixed.

@Qclanton
Copy link

Qclanton commented Apr 11, 2018

I faced the same problem and simply wrapped my html into additional wrapper to work around it.
Example:

$content = "<div class="test"><p>Hallo</p></div>";
$objNode = new HtmlPageCrawler("<div id='crawled-root'>{$content}</div>");
$objNode->filter(".test")->remove();

$x = $objNode->filter("#crawled-root")->saveHTML();

@glensc
Copy link
Contributor

glensc commented Jun 11, 2018

the same problem with "replacewith" call:

<?php

use Wa72\HtmlPageDom\HtmlPageCrawler;

require __DIR__ . '/vendor/autoload.php';

$html = '<img width="50" height="50" src="about:blank">';
$crawler = new HtmlPageCrawler($html);

$crawler->filter('img')
    ->each(function (HtmlPageCrawler $node) {

        $node->replaceWith('<!-- Picture -->');
        $node->attr('test', 1);
    });

$text = (string)$crawler;
dump($text);
$ ./replace.php
"<img width="50" height="50" src="about:blank" test="1">"

@aust6512
Copy link

aust6512 commented Jul 2, 2019

Looking at the code the remove() function uses a call to parentNode->removeChild($node). I briefly looked for a way to directly remove a node, but I'm not sure there is a way within php Dom besides removeChild(). I just wrapped my whole document in a

to start and it does solve the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants