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

Cannot replace root element when multiple root node #21

Open
shtse8 opened this issue Feb 28, 2017 · 3 comments
Open

Cannot replace root element when multiple root node #21

shtse8 opened this issue Feb 28, 2017 · 3 comments

Comments

@shtse8
Copy link

shtse8 commented Feb 28, 2017

Code:

$c = new HtmlPageCrawler('<p>Paragraph 1</p> <p>Paragraph 2</p><p>Paragraph 3</p>');
$c->filter('p')->first()->each(function($element) {
	$element->replaceWith('ff');
});
echo $c->saveHTML();

Expected:

ff <p>Paragraph 2</p><p>Paragraph 3</p>

Actual:

<p>Paragraph 1</p> <p>Paragraph 2</p><p>Paragraph 3</p>
@shtse8
Copy link
Author

shtse8 commented Mar 1, 2017

But it works with:

$c = new HtmlPageCrawler('<div><p>Paragraph 1</p> <p>Paragraph 2</p><p>Paragraph 3</p></div>');
$c->filter('p')->first()->each(function($element) {
	$element->replaceWith('ff');
});
echo $c->saveHTML();

Result:

<div>ff <p>Paragraph 2</p> <p>Paragraph 3</p></div>

@shtse8
Copy link
Author

shtse8 commented Mar 1, 2017

Error occurs in the following code:

$c = new HtmlPageCrawler('<p>Paragraph 1</p> <p>Paragraph 2</p><p>Paragraph 3</p>');
$c->filter('p')->first()->each(function($element) {
	$element->replaceWith('ff');
});
$c->filter('p')->first()->each(function($element) {
	$element->replaceWith('dd');
});
echo $c->saveHTML();

Result:

PHP Fatal error:  Uncaught Error: Call to a member function replaceChild() on null in /var/www/site/www/core/vendor/wa72/htmlpagedom/src/HtmlPageCrawler.php:652
Stack trace:
#0 /var/workspace/test10.php(13): Wa72\HtmlPageDom\HtmlPageCrawler->replaceWith(Object(Wa72\HtmlPageDom\HtmlPageCrawler))
#1 /var/www/site/www/core/vendor/symfony/dom-crawler/Crawler.php(371): {closure}(Object(Wa72\HtmlPageDom\HtmlPageCrawler), 0)
#2 /var/workspace/test10.php(14): Symfony\Component\DomCrawler\Crawler->each(Object(Closure))
#3 {main}
  thrown in /var/www/site/www/core/vendor/wa72/htmlpagedom/src/HtmlPageCrawler.php on line 652

@shtse8 shtse8 changed the title Cannot replace element Cannot replace root element when multiple root node Mar 1, 2017
@helariL
Copy link

helariL commented Jun 21, 2018

Any feedback for this? This is quite annoying....

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

2 participants