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

replaceWith / before / after etc. are not working?! #43

Open
BernhardBaumrock opened this issue Apr 2, 2023 · 1 comment
Open

replaceWith / before / after etc. are not working?! #43

BernhardBaumrock opened this issue Apr 2, 2023 · 1 comment

Comments

@BernhardBaumrock
Copy link

Thx for the great library, really helpful! I'm using it to create docs for my open source modules where I'm parsing markdown to html and then adding some customisations.

This has worked very well so far, but now I'm hitting some roadblocks:

$crawler
  ->filter("p")
  ->each(function (HtmlPageCrawler $node) {
    // this works
    $node->addClass("foobar");

    // this does not work
    $node->replaceWith("<div>foobar</div>");

    // this works
    $node->append("<div>test</div>");
  });

append() works, but I'm actually trying to add some label markup like <div>my label</div> after every img and on image tags append() does obviously not work which is why I'm trying to use after() which unfortunately does also not work at all :(

Am I missing something?

@BernhardBaumrock
Copy link
Author

BernhardBaumrock commented Apr 2, 2023

Ok I just found the issue myself!

If you have this code:

<h1>some html</h1>
<p>foo bar</p>
<img src=/foo/bar.png>

And you try this:

$crawler
      ->filter("img")
      ->each(function (HtmlPageCrawler $node) {
          $node->after("<div class='uk-text-center'>image label</div>");
      });

It will not work because you don't have a parent node and therefore the parser can't add the new node after the image tag.

When wrapping everything in one single root div everything works as expected.

<div>
    <h1>some html</h1>
    <p>foo bar</p>
    <img src=/foo/bar.png>
</div>

Not sure if that is a bug and it should automatically wrap "flat" html code in a root element to make sure that after() before() etc. are working? Or at least it would be nice to mention that in the docs :)

Thx again for the brilliant library!

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

1 participant