MCPcopy Index your code
hub / github.com/angular-ui/ui-router / replaceWith

Function replaceWith

test/angular/1.4/angular.js:9016–9089  ·  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

9014 * @param {Node} newNode The new DOM node.
9015 */
9016 function replaceWith($rootElement, elementsToRemove, newNode) {
9017 var firstElementToRemove = elementsToRemove[0],
9018 removeCount = elementsToRemove.length,
9019 parent = firstElementToRemove.parentNode,
9020 i, ii;
9021
9022 if ($rootElement) {
9023 for (i = 0, ii = $rootElement.length; i < ii; i++) {
9024 if ($rootElement[i] == firstElementToRemove) {
9025 $rootElement[i++] = newNode;
9026 for (var j = i, j2 = j + removeCount - 1,
9027 jj = $rootElement.length;
9028 j < jj; j++, j2++) {
9029 if (j2 < jj) {
9030 $rootElement[j] = $rootElement[j2];
9031 } else {
9032 delete $rootElement[j];
9033 }
9034 }
9035 $rootElement.length -= removeCount - 1;
9036
9037 // If the replaced element is also the jQuery .context then replace it
9038 // .context is a deprecated jQuery api, so we should set it only when jQuery set it
9039 // http://api.jquery.com/context/
9040 if ($rootElement.context === firstElementToRemove) {
9041 $rootElement.context = newNode;
9042 }
9043 break;
9044 }
9045 }
9046 }
9047
9048 if (parent) {
9049 parent.replaceChild(newNode, firstElementToRemove);
9050 }
9051
9052 // TODO(perf): what's this document fragment for? is it needed? can we at least reuse it?
9053 var fragment = document.createDocumentFragment();
9054 fragment.appendChild(firstElementToRemove);
9055
9056 if (jqLite.hasData(firstElementToRemove)) {
9057 // Copy over user data (that includes Angular's $scope etc.). Don't copy private
9058 // data here because there's no public interface in jQuery to do that and copying over
9059 // event listeners (which is the main use of private data) wouldn't work anyway.
9060 jqLite.data(newNode, jqLite.data(firstElementToRemove));
9061
9062 // Remove data of the replaced element. We cannot just call .remove()
9063 // on the element it since that would deallocate scope that is needed
9064 // for the new node. Instead, remove the data "manually".
9065 if (!jQuery) {
9066 delete jqLite.cache[firstElementToRemove[jqLite.expando]];
9067 } else {
9068 // jQuery 2.x doesn't expose the data storage. Use jQuery.cleanData to clean up after
9069 // the replaced element. The cleanData version monkey-patched by Angular would cause
9070 // the scope to be trashed and we do need the very same scope to work with the new
9071 // element. However, we cannot just cache the non-patched version and use it here as
9072 // that would break if another library patches the method after Angular does (one
9073 // example is jQuery UI). Instead, set a flag indicating scope destroying should be

Callers 2

applyDirectivesToNodeFunction · 0.70
compileTemplateUrlFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected