* 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)
| 8198 | * @param {Node} newNode The new DOM node. |
| 8199 | */ |
| 8200 | function replaceWith($rootElement, elementsToRemove, newNode) { |
| 8201 | var firstElementToRemove = elementsToRemove[0], |
| 8202 | removeCount = elementsToRemove.length, |
| 8203 | parent = firstElementToRemove.parentNode, |
| 8204 | i, ii; |
| 8205 | |
| 8206 | if ($rootElement) { |
| 8207 | for (i = 0, ii = $rootElement.length; i < ii; i++) { |
| 8208 | if ($rootElement[i] == firstElementToRemove) { |
| 8209 | $rootElement[i++] = newNode; |
| 8210 | for (var j = i, j2 = j + removeCount - 1, |
| 8211 | jj = $rootElement.length; |
| 8212 | j < jj; j++, j2++) { |
| 8213 | if (j2 < jj) { |
| 8214 | $rootElement[j] = $rootElement[j2]; |
| 8215 | } else { |
| 8216 | delete $rootElement[j]; |
| 8217 | } |
| 8218 | } |
| 8219 | $rootElement.length -= removeCount - 1; |
| 8220 | |
| 8221 | // If the replaced element is also the jQuery .context then replace it |
| 8222 | // .context is a deprecated jQuery api, so we should set it only when jQuery set it |
| 8223 | // http://api.jquery.com/context/ |
| 8224 | if ($rootElement.context === firstElementToRemove) { |
| 8225 | $rootElement.context = newNode; |
| 8226 | } |
| 8227 | break; |
| 8228 | } |
| 8229 | } |
| 8230 | } |
| 8231 | |
| 8232 | if (parent) { |
| 8233 | parent.replaceChild(newNode, firstElementToRemove); |
| 8234 | } |
| 8235 | |
| 8236 | // TODO(perf): what's this document fragment for? is it needed? can we at least reuse it? |
| 8237 | var fragment = document.createDocumentFragment(); |
| 8238 | fragment.appendChild(firstElementToRemove); |
| 8239 | |
| 8240 | // Copy over user data (that includes Angular's $scope etc.). Don't copy private |
| 8241 | // data here because there's no public interface in jQuery to do that and copying over |
| 8242 | // event listeners (which is the main use of private data) wouldn't work anyway. |
| 8243 | jqLite(newNode).data(jqLite(firstElementToRemove).data()); |
| 8244 | |
| 8245 | // Remove data of the replaced element. We cannot just call .remove() |
| 8246 | // on the element it since that would deallocate scope that is needed |
| 8247 | // for the new node. Instead, remove the data "manually". |
| 8248 | if (!jQuery) { |
| 8249 | delete jqLite.cache[firstElementToRemove[jqLite.expando]]; |
| 8250 | } else { |
| 8251 | // jQuery 2.x doesn't expose the data storage. Use jQuery.cleanData to clean up after |
| 8252 | // the replaced element. The cleanData version monkey-patched by Angular would cause |
| 8253 | // the scope to be trashed and we do need the very same scope to work with the new |
| 8254 | // element. However, we cannot just cache the non-patched version and use it here as |
| 8255 | // that would break if another library patches the method after Angular does (one |
| 8256 | // example is jQuery UI). Instead, set a flag indicating scope destroying should be |
| 8257 | // skipped this one time. |
no outgoing calls
no test coverage detected