public function unwrap()
{
// We do this in two loops in order to
// capture the case where two matches are
// under the same parent. Othwerwise we might
// remove a match before we can move it.
$parents = new \SplObjectStorage();
foreach ($this->matches as $m) {
// Cannot unwrap the root element.
if ($m->isSameNode($m->ownerDocument->documentElement)) {
throw new \QueryPath\Exception('Cannot unwrap the root element.');
}
// Move children to peer of parent.
$parent = $m->parentNode;
$old = $parent->removeChild($m);
$parent->parentNode->insertBefore($old, $parent);
$parents->attach($parent);
}
// Now that all the children are moved, we
// remove all of the parents.
foreach ($parents as $ele) {
$ele->parentNode->removeChild($ele);
}
return $this;
}