* 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)
| 10075 | * @param {Node} newNode The new DOM node. |
| 10076 | */ |
| 10077 | function replaceWith($rootElement, elementsToRemove, newNode) { |
| 10078 | var firstElementToRemove = elementsToRemove[0], |
| 10079 | removeCount = elementsToRemove.length, |
| 10080 | parent = firstElementToRemove.parentNode, |
| 10081 | i, ii; |
| 10082 | |
| 10083 | if ($rootElement) { |
| 10084 | for (i = 0, ii = $rootElement.length; i < ii; i++) { |
| 10085 | if ($rootElement[i] === firstElementToRemove) { |
| 10086 | $rootElement[i++] = newNode; |
| 10087 | for (var j = i, j2 = j + removeCount - 1, |
| 10088 | jj = $rootElement.length; |
| 10089 | j < jj; j++, j2++) { |
| 10090 | if (j2 < jj) { |
| 10091 | $rootElement[j] = $rootElement[j2]; |
| 10092 | } else { |
| 10093 | delete $rootElement[j]; |
| 10094 | } |
| 10095 | } |
| 10096 | $rootElement.length -= removeCount - 1; |
| 10097 | |
| 10098 | // If the replaced element is also the jQuery .context then replace it |
| 10099 | // .context is a deprecated jQuery api, so we should set it only when jQuery set it |
| 10100 | // http://api.jquery.com/context/ |
| 10101 | if ($rootElement.context === firstElementToRemove) { |
| 10102 | $rootElement.context = newNode; |
| 10103 | } |
| 10104 | break; |
| 10105 | } |
| 10106 | } |
| 10107 | } |
| 10108 | |
| 10109 | if (parent) { |
| 10110 | parent.replaceChild(newNode, firstElementToRemove); |
| 10111 | } |
| 10112 | |
| 10113 | // Append all the `elementsToRemove` to a fragment. This will... |
| 10114 | // - remove them from the DOM |
| 10115 | // - allow them to still be traversed with .nextSibling |
| 10116 | // - allow a single fragment.qSA to fetch all elements being removed |
| 10117 | var fragment = window.document.createDocumentFragment(); |
| 10118 | for (i = 0; i < removeCount; i++) { |
| 10119 | fragment.appendChild(elementsToRemove[i]); |
| 10120 | } |
| 10121 | |
| 10122 | if (jqLite.hasData(firstElementToRemove)) { |
| 10123 | // Copy over user data (that includes Angular's $scope etc.). Don't copy private |
| 10124 | // data here because there's no public interface in jQuery to do that and copying over |
| 10125 | // event listeners (which is the main use of private data) wouldn't work anyway. |
| 10126 | jqLite.data(newNode, jqLite.data(firstElementToRemove)); |
| 10127 | |
| 10128 | // Remove $destroy event listeners from `firstElementToRemove` |
| 10129 | jqLite(firstElementToRemove).off('$destroy'); |
| 10130 | } |
| 10131 | |
| 10132 | // Cleanup any data/listeners on the elements and children. |
| 10133 | // This includes invoking the $destroy event on any elements with listeners. |
| 10134 | jqLite.cleanData(fragment.querySelectorAll('*')); |
no outgoing calls
no test coverage detected