* 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)
| 10623 | * @param {Node} newNode The new DOM node. |
| 10624 | */ |
| 10625 | function replaceWith($rootElement, elementsToRemove, newNode) { |
| 10626 | var firstElementToRemove = elementsToRemove[0], |
| 10627 | removeCount = elementsToRemove.length, |
| 10628 | parent = firstElementToRemove.parentNode, |
| 10629 | i, ii; |
| 10630 | |
| 10631 | if ($rootElement) { |
| 10632 | for (i = 0, ii = $rootElement.length; i < ii; i++) { |
| 10633 | if ($rootElement[i] === firstElementToRemove) { |
| 10634 | $rootElement[i++] = newNode; |
| 10635 | for (var j = i, j2 = j + removeCount - 1, |
| 10636 | jj = $rootElement.length; |
| 10637 | j < jj; j++, j2++) { |
| 10638 | if (j2 < jj) { |
| 10639 | $rootElement[j] = $rootElement[j2]; |
| 10640 | } else { |
| 10641 | delete $rootElement[j]; |
| 10642 | } |
| 10643 | } |
| 10644 | $rootElement.length -= removeCount - 1; |
| 10645 | |
| 10646 | // If the replaced element is also the jQuery .context then replace it |
| 10647 | // .context is a deprecated jQuery api, so we should set it only when jQuery set it |
| 10648 | // http://api.jquery.com/context/ |
| 10649 | if ($rootElement.context === firstElementToRemove) { |
| 10650 | $rootElement.context = newNode; |
| 10651 | } |
| 10652 | break; |
| 10653 | } |
| 10654 | } |
| 10655 | } |
| 10656 | |
| 10657 | if (parent) { |
| 10658 | parent.replaceChild(newNode, firstElementToRemove); |
| 10659 | } |
| 10660 | |
| 10661 | // Append all the `elementsToRemove` to a fragment. This will... |
| 10662 | // - remove them from the DOM |
| 10663 | // - allow them to still be traversed with .nextSibling |
| 10664 | // - allow a single fragment.qSA to fetch all elements being removed |
| 10665 | var fragment = window.document.createDocumentFragment(); |
| 10666 | for (i = 0; i < removeCount; i++) { |
| 10667 | fragment.appendChild(elementsToRemove[i]); |
| 10668 | } |
| 10669 | |
| 10670 | if (jqLite.hasData(firstElementToRemove)) { |
| 10671 | // Copy over user data (that includes AngularJS's $scope etc.). Don't copy private |
| 10672 | // data here because there's no public interface in jQuery to do that and copying over |
| 10673 | // event listeners (which is the main use of private data) wouldn't work anyway. |
| 10674 | jqLite.data(newNode, jqLite.data(firstElementToRemove)); |
| 10675 | |
| 10676 | // Remove $destroy event listeners from `firstElementToRemove` |
| 10677 | jqLite(firstElementToRemove).off('$destroy'); |
| 10678 | } |
| 10679 | |
| 10680 | // Cleanup any data/listeners on the elements and children. |
| 10681 | // This includes invoking the $destroy event on any elements with listeners. |
| 10682 | jqLite.cleanData(fragment.querySelectorAll('*')); |
no outgoing calls
no test coverage detected