Rct567 / DomQuery

PHP library for easy 'jQuery like' DOM traversing and manipulation.
MIT License
130 stars 38 forks source link

replaceWith #6

Closed aikianapa closed 5 months ago

aikianapa commented 4 years ago

replaceWith work with DomNode but not work with DomQuery element

Rct567 commented 4 years ago

Can you give an example? Because it has been tested in: https://github.com/Rct567/DomQuery/blob/15208bd61f0b87d3befa795a38e5b47a3dbebb97/tests/Rct567/DomQuery/Tests/DomQueryManipulationTest.php#L372

aikianapa commented 4 years ago

It is specific to my problem. I need to replace root element. Example: `use Rct567\DomQuery\DomQuery;

$tpl = new DomQuery("<meta data-wb-role='include' data-wb-src='somefile.php'>");

$tpl->replaceWith(file_get_contents($tpl->attr("data-wb-src"))); // not work

$tpl->find("meta[data-wb-role=include]")->replaceWith(file_get_contents($tpl->find("meta[data-wb- role=include]")->attr("data-wb-src"))); //not work

echo $tpl->outerHtml();`

aikianapa commented 4 years ago

Test work with child element, but not work with root element.

Rct567 commented 9 months ago

Yeh manipulating the root element or its direct children is always troublesome. You could use a 'virtual' root element and get the content of that root element when done. I thought of making DomQuery do this transparently, but I have not yet explored this option and all its possible side effects.