MCPcopy Create free account
hub / github.com/cafebazaar/blacksmith / replaceWith

Function replaceWith

web/static/bower_components/angular/angular.js:8706–8779  ·  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

8704 * @param {Node} newNode The new DOM node.
8705 */
8706 function replaceWith($rootElement, elementsToRemove, newNode) {
8707 var firstElementToRemove = elementsToRemove[0],
8708 removeCount = elementsToRemove.length,
8709 parent = firstElementToRemove.parentNode,
8710 i, ii;
8711
8712 if ($rootElement) {
8713 for (i = 0, ii = $rootElement.length; i < ii; i++) {
8714 if ($rootElement[i] == firstElementToRemove) {
8715 $rootElement[i++] = newNode;
8716 for (var j = i, j2 = j + removeCount - 1,
8717 jj = $rootElement.length;
8718 j < jj; j++, j2++) {
8719 if (j2 < jj) {
8720 $rootElement[j] = $rootElement[j2];
8721 } else {
8722 delete $rootElement[j];
8723 }
8724 }
8725 $rootElement.length -= removeCount - 1;
8726
8727 // If the replaced element is also the jQuery .context then replace it
8728 // .context is a deprecated jQuery api, so we should set it only when jQuery set it
8729 // http://api.jquery.com/context/
8730 if ($rootElement.context === firstElementToRemove) {
8731 $rootElement.context = newNode;
8732 }
8733 break;
8734 }
8735 }
8736 }
8737
8738 if (parent) {
8739 parent.replaceChild(newNode, firstElementToRemove);
8740 }
8741
8742 // TODO(perf): what's this document fragment for? is it needed? can we at least reuse it?
8743 var fragment = document.createDocumentFragment();
8744 fragment.appendChild(firstElementToRemove);
8745
8746 if (jqLite.hasData(firstElementToRemove)) {
8747 // Copy over user data (that includes Angular's $scope etc.). Don't copy private
8748 // data here because there's no public interface in jQuery to do that and copying over
8749 // event listeners (which is the main use of private data) wouldn't work anyway.
8750 jqLite(newNode).data(jqLite(firstElementToRemove).data());
8751
8752 // Remove data of the replaced element. We cannot just call .remove()
8753 // on the element it since that would deallocate scope that is needed
8754 // for the new node. Instead, remove the data "manually".
8755 if (!jQuery) {
8756 delete jqLite.cache[firstElementToRemove[jqLite.expando]];
8757 } else {
8758 // jQuery 2.x doesn't expose the data storage. Use jQuery.cleanData to clean up after
8759 // the replaced element. The cleanData version monkey-patched by Angular would cause
8760 // the scope to be trashed and we do need the very same scope to work with the new
8761 // element. However, we cannot just cache the non-patched version and use it here as
8762 // that would break if another library patches the method after Angular does (one
8763 // example is jQuery UI). Instead, set a flag indicating scope destroying should be

Callers 2

applyDirectivesToNodeFunction · 0.85
compileTemplateUrlFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected