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

Updates to nodes that are nested more than one level deep aren't reflected when outputting the root or parents node's html #33

Open
jacobalvarez opened this issue May 7, 2019 · 1 comment

Comments

@jacobalvarez
Copy link

Please review the following test code. Am I doing something wrong here?

<?php
use Wa72\HtmlPageDom\HtmlPageCrawler;
require_once realpath($_SERVER['DOCUMENT_ROOT']) . '/support_files/external/composer_packages/vendor/autoload.php';
?>

<!DOCTYPE html>
<html>
<body>
<h2>This works</h2>
<?php
	$rootNode1 = HtmlPageCrawler::create('<div />');
	$testNode1 = HtmlPageCrawler::create('<p />');
	$rootNode1->append($testNode1);

	// Change test node text after node appended
	$testNode1->text('correct text');

	// Output root node html. Correct
	echo $rootNode1;

	// Output test node html. Correct
	echo $testNode1;
?>

<h2>This doesn't work when <code>$span</code> is nested deeper than one level?</h2>
<?php
	$rootNode2 = HtmlPageCrawler::create('<div />');
	$p = HtmlPageCrawler::create('<p />');
	$testNode2 = HtmlPageCrawler::create('<span />')->text('incorrect text');
	$p->append($testNode2);
	$rootNode2->append($p);

	// Change test node text after node appended
	$testNode2->text('correct text');

	// Output root or parent node html. Incorrect
	echo $rootNode2;
	echo $p;

	// Output node html. Correct
	echo $testNode2;
?>
</body>
</html>
@wasinger
Copy link
Owner

wasinger commented May 8, 2019

In general, you should not expect that you can modify a node after appending it to another this way.

That's because DOMDocument::importNode() which is used by append() internally always makes copies of the node objects. The Crawler object that is passed to append() gets updated with the cloned nodes (that's why it works in your first example) but any children of the nodes in the Crawler object are clones, too, and therefore not connected to Crawler objects that contained their originals any more.

In your example:
At line $rootNode1->append($testNode1); the p node inside $testNode1 is cloned when being appended to $rootNode1 but the $testNode1 object is updated to contain the cloned p node, so you can modify it afterwards.
In your second example, when calling $rootNode2->append($p); the $p variable will be updated with the cloned p node but the span child of it is not connected to the $testNode2 variable any more.

If someone has a suggestion how to fix this please contribute...

wasinger pushed a commit that referenced this issue Jun 28, 2019
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