voku / simple_html_dom

📜 Modern Simple HTML DOM Parser for PHP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

delete function has bug

justdoitlah opened this issue · comments

What is this feature about (expected vs actual behaviour)?

delete function has bug

How can I reproduce it?

$dom = HtmlDomParser::str_get_html($html);
$body = $dom->findOne("body");
$body->findOne("img")->delete();
Image is not deleted.

Image is deleted is only delete if using: $dom->findOne("img")->delete();

Does it take minutes, hours or days to fix?

minutes

Any additional information?

Can you give a example html, so that we can create a unit test out of it, thx?

Can you give a example html, so that we can create a unit test out of it, thx?

I got same error. You can try this code

        $html = '
        <html lang="en">
        <head>
            <title>Document</title>
        </head>
        <body>
            <div class="home-filter">xxx<did>test</div></div>
        </body>
        </html>';
        $dom = HtmlDomParser::str_get_html($html);
        $body = $dom->findOne('body');

        $x = $body->findOne('.home-filter');
        $x->delete();
        print_r($dom->html());

        echo '----';

        $x2 = $dom->findOne('.home-filter');
        $x2->delete();
        print_r($dom->html());

@trananhmanh89 your HTML is broken <did>, do you want to parse it as it is in the example? 🤔

@trananhmanh89 your html is broken "", do you want to parse it as it is in the example? 🤔

Oh, sorry. You can try this code again

        $html = '
        <!DOCTYPE html>
        <html lang="en">
        <head>
            <title>Document</title>
        </head>
        <body>
            <div class="home-filter">
                <div>test</div>
            </div>
        </body>
        </html>
        ';
        $dom = HtmlDomParser::str_get_html($html);
        $body = $dom->findOne('body');

        $x = $body->findOne('.home-filter');
        $x->delete();
        echo $dom->html();

        echo "\n----\n";

        $x2 = $dom->findOne('.home-filter');
        $x2->delete();
        echo $dom->html();

        die;

@voku, i found out that, this issue because simple dom doesn't apply the changes to root dom if you switch context to child element. And also always create a new HtmlDomParser each time you find, findOne, or findMulti.

commented

I'm having the same issue:

$dom = HtmlDomParser::str_get_html('<html><form><input name="test"></form></html>');
$form = $dom->findOne('form');     // first findOne()
$input = $form->findOne('input');  // nested findOne()
$input->name = 'FOO';              // mutation
echo str($dom);                    // does not see the change!

@voku To be clear, what is the correct way to perform this change? I cannot avoid the two nested calls, because in my real code they are not findOne(), but loops over all results of find():

foreach ($dom->find('form') as $form) {
    // ... analyze form
    foreach ($form->find('input') as $input) {
        // ... mutate each input
    }
}
echo str($dom); // does not see mutations done to inputs!