* 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)
| 9415 | * @param {Node} newNode The new DOM node. |
| 9416 | */ |
| 9417 | function replaceWith($rootElement, elementsToRemove, newNode) { |
| 9418 | var firstElementToRemove = elementsToRemove[0], |
| 9419 | removeCount = elementsToRemove.length, |
| 9420 | parent = firstElementToRemove.parentNode, |
| 9421 | i, ii; |
| 9422 | |
| 9423 | if ($rootElement) { |
| 9424 | for (i = 0, ii = $rootElement.length; i < ii; i++) { |
| 9425 | if ($rootElement[i] == firstElementToRemove) { |
| 9426 | $rootElement[i++] = newNode; |
| 9427 | for (var j = i, j2 = j + removeCount - 1, |
| 9428 | jj = $rootElement.length; |
| 9429 | j < jj; j++, j2++) { |
| 9430 | if (j2 < jj) { |
| 9431 | $rootElement[j] = $rootElement[j2]; |
| 9432 | } else { |
| 9433 | delete $rootElement[j]; |
| 9434 | } |
| 9435 | } |
| 9436 | $rootElement.length -= removeCount - 1; |
| 9437 | |
| 9438 | // If the replaced element is also the jQuery .context then replace it |
| 9439 | // .context is a deprecated jQuery api, so we should set it only when jQuery set it |
| 9440 | // http://api.jquery.com/context/ |
| 9441 | if ($rootElement.context === firstElementToRemove) { |
| 9442 | $rootElement.context = newNode; |
| 9443 | } |
| 9444 | break; |
| 9445 | } |
| 9446 | } |
| 9447 | } |
| 9448 | |
| 9449 | if (parent) { |
| 9450 | parent.replaceChild(newNode, firstElementToRemove); |
| 9451 | } |
| 9452 | |
| 9453 | // Append all the `elementsToRemove` to a fragment. This will... |
| 9454 | // - remove them from the DOM |
| 9455 | // - allow them to still be traversed with .nextSibling |
| 9456 | // - allow a single fragment.qSA to fetch all elements being removed |
| 9457 | var fragment = document.createDocumentFragment(); |
| 9458 | for (i = 0; i < removeCount; i++) { |
| 9459 | fragment.appendChild(elementsToRemove[i]); |
| 9460 | } |
| 9461 | |
| 9462 | if (jqLite.hasData(firstElementToRemove)) { |
| 9463 | // Copy over user data (that includes Angular's $scope etc.). Don't copy private |
| 9464 | // data here because there's no public interface in jQuery to do that and copying over |
| 9465 | // event listeners (which is the main use of private data) wouldn't work anyway. |
| 9466 | jqLite.data(newNode, jqLite.data(firstElementToRemove)); |
| 9467 | |
| 9468 | // Remove $destroy event listeners from `firstElementToRemove` |
| 9469 | jqLite(firstElementToRemove).off('$destroy'); |
| 9470 | } |
| 9471 | |
| 9472 | // Cleanup any data/listeners on the elements and children. |
| 9473 | // This includes invoking the $destroy event on any elements with listeners. |
| 9474 | jqLite.cleanData(fragment.querySelectorAll('*')); |
no outgoing calls
no test coverage detected