Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ QueryPath Changelog
- Consolidate the sibling and ancestor walks behind `next()`, `prev()`, `nextAll()`, `prevAll()`, `nextUntil()`, `prevUntil()`, `parent()`, `parents()`, `parentsUntil()` and `closest()` into one helper, so a fix to one no longer has to be repeated in its mirror (issue #115). As a side effect, `parentsUntil()` no longer loops forever on an element that is not attached to the document, and `nextUntil('')` and `prevUntil('')` treat an empty selector as no selector, as the other methods already did, instead of throwing `QueryPath\CSS\ParseException`. `closest('')` returns an empty set, as in jQuery. `nextUntil()`, `prevUntil()` and `parentsUntil()` also stop at an element already collected from an earlier starting element, as `nextAll()` and `prevAll()` do, so `nextUntil()` on each of 2000 siblings falls from about 100 ms to 1 ms
- **Behaviour change.** `prevAll()`, `prevUntil()`, `nextAll()`, `nextUntil()`, `siblings()`, `parent()` and `closest()` now return the results for a multi-element set in jQuery's order, instead of grouped by starting element (issue #117). `prevAll()` and `prevUntil()` return reverse document order, as `parents()` and `parentsUntil()` already did, and the others return document order. `qp($xml, 'c')->add('d')->prevAll()` now returns `c, b, a` rather than `b, a, c`. `next()` and `prev()` keep the order of the starting elements, as in jQuery, and the results for a single starting element are unchanged
- **Behaviour change.** A positional pseudo-class (`:first`, `:eq()`, `:last`, ...) in the stop selector of `nextUntil()`, `prevUntil()` and `parentsUntil()` now counts across the whole document, as jQuery's does, instead of seeing each sibling or ancestor as a one-element set (issue #118). `nextUntil(':first')` used to stop at once and now walks to the end, and `nextUntil(':eq(3)')` stops at the fourth element in the document. `closest()` no longer matches a positional selector at all, as in jQuery, where `closest(':first')` used to return the element itself. `next()`, `prev()` and `parent()` still test each candidate on its own, which agrees with jQuery for a single starting element
- Fix `replaceAll()` inserting empty copies of the replacement. It copied the replacement node without its children, so only the element and its attributes arrived. It now copies the whole subtree (issue #135)

# 4.2.0

Expand Down
3 changes: 1 addition & 2 deletions src/Helpers/QueryMutators.php
Original file line number Diff line number Diff line change
Expand Up @@ -847,8 +847,7 @@ public function replaceAll($selector, DOMDocument $document): Query
}

foreach ((new DOMTraverser($scope))->find($selector)->matches() as $item) {
$node = $replacement->cloneNode();
$node = $document->importNode($node);
$node = $document->importNode($replacement, true);
$item->parentNode->replaceChild($node, $item);
}

Expand Down
47 changes: 47 additions & 0 deletions tests/Issues/Issue135Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
/**
* @file
*
* Regression tests for https://github.com/GravityPDF/querypath/issues/135
*
* replaceAll() inserts each replacement with its children.
*/

namespace QueryPathTests;

/**
* @ingroup querypath_tests
*/
class Issue135Test extends TestCase
{
public function testReplaceAllCopiesChildren()
{
$document = qp('<?xml version="1.0"?><root><p id="p1"/><p id="p2"/></root>')->get(0)->ownerDocument;

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

$copies = qp($document)->children();
$this->assertSame(['x', 'x'], array_map(function ($node) {
return $node->tagName;
}, $copies->get()));
foreach ($copies->get() as $copy) {
$this->assertSame('1', $copy->getAttribute('a'));
$this->assertSame('hi', $copy->textContent);
$this->assertSame(1, qp($copy)->children('y')->count());
}
}

public function testReplaceAllInsertsSeparateCopies()
{
$document = qp('<?xml version="1.0"?><root><p/><p/></root>')->get(0)->ownerDocument;
$source = qp('<?xml version="1.0"?><w><x><y/></x></w>', 'x');

$source->replaceAll('p', $document);

$copies = qp($document)->find('x')->get();
$this->assertCount(2, $copies);
$this->assertNotSame($copies[0], $copies[1]);
$this->assertNotSame($copies[0]->firstChild, $copies[1]->firstChild);
$this->assertSame(1, qp($source->get(0)->ownerDocument)->find('x > y')->count());
}
}
Loading