* 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)
| 11257 | * @param {Node} newNode The new DOM node. |
| 11258 | */ |
| 11259 | function replaceWith($rootElement, elementsToRemove, newNode) { |
| 11260 | var firstElementToRemove = elementsToRemove[0], |
| 11261 | removeCount = elementsToRemove.length, |
| 11262 | parent = firstElementToRemove.parentNode, |
| 11263 | i, ii; |
| 11264 | |
| 11265 | if ($rootElement) { |
| 11266 | for (i = 0, ii = $rootElement.length; i < ii; i++) { |
| 11267 | if ($rootElement[i] === firstElementToRemove) { |
| 11268 | $rootElement[i++] = newNode; |
| 11269 | for (var j = i, j2 = j + removeCount - 1, |
| 11270 | jj = $rootElement.length; |
| 11271 | j < jj; j++, j2++) { |
| 11272 | if (j2 < jj) { |
| 11273 | $rootElement[j] = $rootElement[j2]; |
| 11274 | } else { |
| 11275 | delete $rootElement[j]; |
| 11276 | } |
| 11277 | } |
| 11278 | $rootElement.length -= removeCount - 1; |
| 11279 | |
| 11280 | // If the replaced element is also the jQuery .context then replace it |
| 11281 | // .context is a deprecated jQuery api, so we should set it only when jQuery set it |
| 11282 | // http://api.jquery.com/context/ |
| 11283 | if ($rootElement.context === firstElementToRemove) { |
| 11284 | $rootElement.context = newNode; |
| 11285 | } |
| 11286 | break; |
| 11287 | } |
| 11288 | } |
| 11289 | } |
| 11290 | |
| 11291 | if (parent) { |
| 11292 | parent.replaceChild(newNode, firstElementToRemove); |
| 11293 | } |
| 11294 | |
| 11295 | // Append all the `elementsToRemove` to a fragment. This will... |
| 11296 | // - remove them from the DOM |
| 11297 | // - allow them to still be traversed with .nextSibling |
| 11298 | // - allow a single fragment.qSA to fetch all elements being removed |
| 11299 | var fragment = window.document.createDocumentFragment(); |
| 11300 | for (i = 0; i < removeCount; i++) { |
| 11301 | fragment.appendChild(elementsToRemove[i]); |
| 11302 | } |
| 11303 | |
| 11304 | if (jqLite.hasData(firstElementToRemove)) { |
| 11305 | // Copy over user data (that includes AngularJS's $scope etc.). Don't copy private |
| 11306 | // data here because there's no public interface in jQuery to do that and copying over |
| 11307 | // event listeners (which is the main use of private data) wouldn't work anyway. |
| 11308 | jqLite.data(newNode, jqLite.data(firstElementToRemove)); |
| 11309 | |
| 11310 | // Remove $destroy event listeners from `firstElementToRemove` |
| 11311 | jqLite(firstElementToRemove).off('$destroy'); |
| 11312 | } |
| 11313 | |
| 11314 | // Cleanup any data/listeners on the elements and children. |
| 11315 | // This includes invoking the $destroy event on any elements with listeners. |
| 11316 | jqLite.cleanData(fragment.querySelectorAll('*')); |
no outgoing calls
no test coverage detected