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

Make replacewith() work with multiple children of the same parent. #32

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
22 changes: 14 additions & 8 deletions src/Rct567/DomQuery/DomQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -1103,21 +1103,27 @@ public function replaceWith()
{
$removed_nodes = new self();

$this->importNodes(\func_get_args(), function ($node, $imported_node) use (&$removed_nodes) {
// Since are inserting each element before the replacement, the elements
// will end up in the reverse order in the dom. So reverse it first.
$args = \func_get_args();
$imported_nodes = new self();
$this->importNodes($args, function ($node, $imported_node) use (&$imported_nodes) {
$imported_nodes->addDomNode($imported_node);
});
$imported_nodes->nodes = \array_reverse($imported_nodes->nodes);

$this->importNodes($imported_nodes, function ($node, $imported_node) use (&$removed_nodes) {
if ($node->nextSibling) {
$node->parentNode->insertBefore($imported_node, $node->nextSibling);
} else { // node is last, so there is no next sibling to insert before
$node->parentNode->appendChild($imported_node);
}
$removed_nodes->addDomNode($node);
$node->parentNode->removeChild($node);
});

foreach (\func_get_args() as $new_content) {
if (!\is_string($new_content)) {
self::create($new_content)->remove();
}
}
// Remove after all nodes have been moved.
// If removed too early, multiple children with the same parent will
// cause a crash.
$this->remove();

return $removed_nodes;
}
Expand Down
19 changes: 19 additions & 0 deletions src/Rct567/DomQuery/DomQueryNodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,25 @@ public function loadDomNodeList(\DOMNodeList $dom_node_list)
*/
public function addDomNode(\DOMNode $dom_node, $prepend=false)
{
// If the node already exists, remove it so it be added again.
// Ideally the element order is document order after the duplicates are
// added, like real jQuery.
$this->nodes = call_user_func(
function (array $existing_nodes, \DOMNode $dom_node) {
foreach ($existing_nodes as $array_id => $existing_node) {
if ($existing_node->isSameNode($dom_node)) {
unset($existing_nodes[$array_id]);
// Reset array keys, optional.
$existing_nodes = array_values($existing_nodes);
// Break for performance.
break;
}
}
return $existing_nodes;
},
$this->nodes,
$dom_node
);
if ($prepend) {
array_unshift($this->nodes, $dom_node);
} else {
Expand Down