* 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)
| 8111 | * @param {Node} newNode The new DOM node. |
| 8112 | */ |
| 8113 | function replaceWith($rootElement, elementsToRemove, newNode) { |
| 8114 | var firstElementToRemove = elementsToRemove[0], |
| 8115 | removeCount = elementsToRemove.length, |
| 8116 | parent = firstElementToRemove.parentNode, |
| 8117 | i, ii; |
| 8118 | |
| 8119 | if ($rootElement) { |
| 8120 | for (i = 0, ii = $rootElement.length; i < ii; i++) { |
| 8121 | if ($rootElement[i] == firstElementToRemove) { |
| 8122 | $rootElement[i++] = newNode; |
| 8123 | for (var j = i, j2 = j + removeCount - 1, |
| 8124 | jj = $rootElement.length; |
| 8125 | j < jj; j++, j2++) { |
| 8126 | if (j2 < jj) { |
| 8127 | $rootElement[j] = $rootElement[j2]; |
| 8128 | } else { |
| 8129 | delete $rootElement[j]; |
| 8130 | } |
| 8131 | } |
| 8132 | $rootElement.length -= removeCount - 1; |
| 8133 | |
| 8134 | // If the replaced element is also the jQuery .context then replace it |
| 8135 | // .context is a deprecated jQuery api, so we should set it only when jQuery set it |
| 8136 | // http://api.jquery.com/context/ |
| 8137 | if ($rootElement.context === firstElementToRemove) { |
| 8138 | $rootElement.context = newNode; |
| 8139 | } |
| 8140 | break; |
| 8141 | } |
| 8142 | } |
| 8143 | } |
| 8144 | |
| 8145 | if (parent) { |
| 8146 | parent.replaceChild(newNode, firstElementToRemove); |
| 8147 | } |
| 8148 | |
| 8149 | // TODO(perf): what's this document fragment for? is it needed? can we at least reuse it? |
| 8150 | var fragment = document.createDocumentFragment(); |
| 8151 | fragment.appendChild(firstElementToRemove); |
| 8152 | |
| 8153 | // Copy over user data (that includes Angular's $scope etc.). Don't copy private |
| 8154 | // data here because there's no public interface in jQuery to do that and copying over |
| 8155 | // event listeners (which is the main use of private data) wouldn't work anyway. |
| 8156 | jqLite(newNode).data(jqLite(firstElementToRemove).data()); |
| 8157 | |
| 8158 | // Remove data of the replaced element. We cannot just call .remove() |
| 8159 | // on the element it since that would deallocate scope that is needed |
| 8160 | // for the new node. Instead, remove the data "manually". |
| 8161 | if (!jQuery) { |
| 8162 | delete jqLite.cache[firstElementToRemove[jqLite.expando]]; |
| 8163 | } else { |
| 8164 | // jQuery 2.x doesn't expose the data storage. Use jQuery.cleanData to clean up after |
| 8165 | // the replaced element. The cleanData version monkey-patched by Angular would cause |
| 8166 | // the scope to be trashed and we do need the very same scope to work with the new |
| 8167 | // element. However, we cannot just cache the non-patched version and use it here as |
| 8168 | // that would break if another library patches the method after Angular does (one |
| 8169 | // example is jQuery UI). Instead, set a flag indicating scope destroying should be |
| 8170 | // skipped this one time. |
no outgoing calls
no test coverage detected