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

delete function has bug #88

Open
justdoitlah opened this issue Aug 18, 2022 · 7 comments
Open

delete function has bug #88

justdoitlah opened this issue Aug 18, 2022 · 7 comments
Assignees

Comments

@justdoitlah
Copy link

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?

@voku
Copy link
Owner

voku commented Aug 18, 2022

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

@trananhmanh89
Copy link

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());

@voku
Copy link
Owner

voku commented Nov 26, 2022

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

@trananhmanh89
Copy link

@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;

@trananhmanh89
Copy link

@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.

@voku voku self-assigned this Nov 28, 2022
@tobia
Copy link

tobia commented Jul 13, 2023

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!

@2gen
Copy link

2gen commented Aug 26, 2024

Anyone have a solution to this? I too am facing a similar issue as @tobia. I need to loop through and preform changes to child elements.

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

5 participants