* This is a special jqLite.replaceWith, which can replace items which * have no parents, provided that the containing jqLite collection is provided. * * @param {JqLite=} $rootElement The root of the compile tree. Used so that we can replace nodes * i
($rootElement, elementsToRemove, newNode)
| 10605 | * @param {Node} newNode The new DOM node. |
| 10606 | */ |
| 10607 | function replaceWith($rootElement, elementsToRemove, newNode) { |
| 10608 | var firstElementToRemove = elementsToRemove[0], |
| 10609 | removeCount = elementsToRemove.length, |
| 10610 | parent = firstElementToRemove.parentNode, |
| 10611 | i, ii; |
| 10612 | |
| 10613 | if ($rootElement) { |
| 10614 | for (i = 0, ii = $rootElement.length; i < ii; i++) { |
| 10615 | if ($rootElement[i] === firstElementToRemove) { |
| 10616 | $rootElement[i++] = newNode; |
| 10617 | for (var j = i, j2 = j + removeCount - 1, |
| 10618 | jj = $rootElement.length; |
| 10619 | j < jj; j++, j2++) { |
| 10620 | if (j2 < jj) { |
| 10621 | $rootElement[j] = $rootElement[j2]; |
| 10622 | } else { |
| 10623 | delete $rootElement[j]; |
| 10624 | } |
| 10625 | } |
| 10626 | $rootElement.length -= removeCount - 1; |
| 10627 | |
| 10628 | // If the replaced element is also the jQuery .context then replace it |
| 10629 | // .context is a deprecated jQuery api, so we should set it only when jQuery set it |
| 10630 | // http://api.jquery.com/context/ |
| 10631 | if ($rootElement.context === firstElementToRemove) { |
| 10632 | $rootElement.context = newNode; |
| 10633 | } |
| 10634 | break; |
| 10635 | } |
| 10636 | } |
| 10637 | } |
| 10638 | |
| 10639 | if (parent) { |
| 10640 | parent.replaceChild(newNode, firstElementToRemove); |
| 10641 | } |
| 10642 | |
| 10643 | // Append all the `elementsToRemove` to a fragment. This will... |
| 10644 | // - remove them from the DOM |
| 10645 | // - allow them to still be traversed with .nextSibling |
| 10646 | // - allow a single fragment.qSA to fetch all elements being removed |
| 10647 | var fragment = window.document.createDocumentFragment(); |
| 10648 | for (i = 0; i < removeCount; i++) { |
| 10649 | fragment.appendChild(elementsToRemove[i]); |
| 10650 | } |
| 10651 | |
| 10652 | if (jqLite.hasData(firstElementToRemove)) { |
| 10653 | // Copy over user data (that includes AngularJS's $scope etc.). Don't copy private |
| 10654 | // data here because there's no public interface in jQuery to do that and copying over |
| 10655 | // event listeners (which is the main use of private data) wouldn't work anyway. |
| 10656 | jqLite.data(newNode, jqLite.data(firstElementToRemove)); |
| 10657 | |
| 10658 | // Remove $destroy event listeners from `firstElementToRemove` |
| 10659 | jqLite(firstElementToRemove).off('$destroy'); |
| 10660 | } |
| 10661 | |
| 10662 | // Cleanup any data/listeners on the elements and children. |
| 10663 | // This includes invoking the $destroy event on any elements with listeners. |
| 10664 | jqLite.cleanData(fragment.querySelectorAll('*')); |
no outgoing calls
no test coverage detected