Skip to content

replaceAll() inserts empty copies: it clones the replacement without its children #135

Open

Description

@gravitypdf-bot

Description of the bug

replaceAll() copies each replacement node without its children, so the replacements arrive empty:

$doc = qp('<?xml version="1.0"?><root><p id="p1"/><p id="p2"/></root>')->get(0)->ownerDocument;

qp('<?xml version="1.0"?><w><x>hi<y/></x></w>', 'x')->replaceAll('p', $doc);

echo qp($doc)->xml();
// <root><x/><x/></root>
// expected <root><x>hi<y/></x><x>hi<y/></x></root>

Cause

src/Helpers/QueryMutators.php, in replaceAll():

$node = $replacement->cloneNode();
$node = $document->importNode($node);

Both calls default $deep to false. importNode($replacement, true) alone would do (it already copies the node), and attributes survive today only because a shallow element clone keeps them.

QueryPath version

main at 9d04ccf. Noticed while working on #127.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions