MCPcopy
hub / github.com/angular-ui/ui-grid / replaceWith

Function replaceWith

lib/test/angular/1.6.7/angular.js:10536–10601  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

10534 * @param {Node} newNode The new DOM node.
10535 */
10536 function replaceWith($rootElement, elementsToRemove, newNode) {
10537 var firstElementToRemove = elementsToRemove[0],
10538 removeCount = elementsToRemove.length,
10539 parent = firstElementToRemove.parentNode,
10540 i, ii;
10541
10542 if ($rootElement) {
10543 for (i = 0, ii = $rootElement.length; i < ii; i++) {
10544 if ($rootElement[i] === firstElementToRemove) {
10545 $rootElement[i++] = newNode;
10546 for (var j = i, j2 = j + removeCount - 1,
10547 jj = $rootElement.length;
10548 j < jj; j++, j2++) {
10549 if (j2 < jj) {
10550 $rootElement[j] = $rootElement[j2];
10551 } else {
10552 delete $rootElement[j];
10553 }
10554 }
10555 $rootElement.length -= removeCount - 1;
10556
10557 // If the replaced element is also the jQuery .context then replace it
10558 // .context is a deprecated jQuery api, so we should set it only when jQuery set it
10559 // http://api.jquery.com/context/
10560 if ($rootElement.context === firstElementToRemove) {
10561 $rootElement.context = newNode;
10562 }
10563 break;
10564 }
10565 }
10566 }
10567
10568 if (parent) {
10569 parent.replaceChild(newNode, firstElementToRemove);
10570 }
10571
10572 // Append all the `elementsToRemove` to a fragment. This will...
10573 // - remove them from the DOM
10574 // - allow them to still be traversed with .nextSibling
10575 // - allow a single fragment.qSA to fetch all elements being removed
10576 var fragment = window.document.createDocumentFragment();
10577 for (i = 0; i < removeCount; i++) {
10578 fragment.appendChild(elementsToRemove[i]);
10579 }
10580
10581 if (jqLite.hasData(firstElementToRemove)) {
10582 // Copy over user data (that includes Angular's $scope etc.). Don't copy private
10583 // data here because there's no public interface in jQuery to do that and copying over
10584 // event listeners (which is the main use of private data) wouldn't work anyway.
10585 jqLite.data(newNode, jqLite.data(firstElementToRemove));
10586
10587 // Remove $destroy event listeners from `firstElementToRemove`
10588 jqLite(firstElementToRemove).off('$destroy');
10589 }
10590
10591 // Cleanup any data/listeners on the elements and children.
10592 // This includes invoking the $destroy event on any elements with listeners.
10593 jqLite.cleanData(fragment.querySelectorAll('*'));

Callers 2

applyDirectivesToNodeFunction · 0.70
compileTemplateUrlFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected