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

Function replaceWith

test/angular/1.7/angular.js:11194–11259  ·  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

11192 * @param {Node} newNode The new DOM node.
11193 */
11194 function replaceWith($rootElement, elementsToRemove, newNode) {
11195 var firstElementToRemove = elementsToRemove[0],
11196 removeCount = elementsToRemove.length,
11197 parent = firstElementToRemove.parentNode,
11198 i, ii;
11199
11200 if ($rootElement) {
11201 for (i = 0, ii = $rootElement.length; i < ii; i++) {
11202 if ($rootElement[i] === firstElementToRemove) {
11203 $rootElement[i++] = newNode;
11204 for (var j = i, j2 = j + removeCount - 1,
11205 jj = $rootElement.length;
11206 j < jj; j++, j2++) {
11207 if (j2 < jj) {
11208 $rootElement[j] = $rootElement[j2];
11209 } else {
11210 delete $rootElement[j];
11211 }
11212 }
11213 $rootElement.length -= removeCount - 1;
11214
11215 // If the replaced element is also the jQuery .context then replace it
11216 // .context is a deprecated jQuery api, so we should set it only when jQuery set it
11217 // http://api.jquery.com/context/
11218 if ($rootElement.context === firstElementToRemove) {
11219 $rootElement.context = newNode;
11220 }
11221 break;
11222 }
11223 }
11224 }
11225
11226 if (parent) {
11227 parent.replaceChild(newNode, firstElementToRemove);
11228 }
11229
11230 // Append all the `elementsToRemove` to a fragment. This will...
11231 // - remove them from the DOM
11232 // - allow them to still be traversed with .nextSibling
11233 // - allow a single fragment.qSA to fetch all elements being removed
11234 var fragment = window.document.createDocumentFragment();
11235 for (i = 0; i < removeCount; i++) {
11236 fragment.appendChild(elementsToRemove[i]);
11237 }
11238
11239 if (jqLite.hasData(firstElementToRemove)) {
11240 // Copy over user data (that includes AngularJS's $scope etc.). Don't copy private
11241 // data here because there's no public interface in jQuery to do that and copying over
11242 // event listeners (which is the main use of private data) wouldn't work anyway.
11243 jqLite.data(newNode, jqLite.data(firstElementToRemove));
11244
11245 // Remove $destroy event listeners from `firstElementToRemove`
11246 jqLite(firstElementToRemove).off('$destroy');
11247 }
11248
11249 // Cleanup any data/listeners on the elements and children.
11250 // This includes invoking the $destroy event on any elements with listeners.
11251 jqLite.cleanData(fragment.querySelectorAll('*'));

Callers 2

applyDirectivesToNodeFunction · 0.70
compileTemplateUrlFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected