diff --git a/src/ng/directive/ngRepeat.js b/src/ng/directive/ngRepeat.js index 78b054ff995a..bce76411beea 100644 --- a/src/ng/directive/ngRepeat.js +++ b/src/ng/directive/ngRepeat.js @@ -279,7 +279,8 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) { trackByIdFn, collectionKeys, block, // last object information {scope, element, id} - nextBlockOrder = []; + nextBlockOrder = [], + elementsToRemove; if (isArrayLike(collection)) { @@ -331,8 +332,9 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) { // lastBlockMap is our own object so we don't need to use special hasOwnPropertyFn if (lastBlockMap.hasOwnProperty(key)) { block = lastBlockMap[key]; - $animate.leave(block.elements); - forEach(block.elements, function(element) { element[NG_REMOVED] = true}); + elementsToRemove = getBlockElements(block); + $animate.leave(elementsToRemove); + forEach(elementsToRemove, function(element) { element[NG_REMOVED] = true; }); block.scope.$destroy(); } } @@ -342,6 +344,7 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) { key = (collection === collectionKeys) ? index : collectionKeys[index]; value = collection[key]; block = nextBlockOrder[index]; + if (nextBlockOrder[index - 1]) previousNode = nextBlockOrder[index - 1].endNode; if (block.startNode) { // if we have already seen this object, then we need to reuse the @@ -357,7 +360,7 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) { // do nothing } else { // existing item which got moved - $animate.move(block.elements, null, jqLite(previousNode)); + $animate.move(getBlockElements(block), null, jqLite(previousNode)); } previousNode = block.endNode; } else { @@ -375,11 +378,11 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) { if (!block.startNode) { linker(childScope, function(clone) { + clone[clone.length++] = document.createComment(' end ngRepeat: ' + expression + ' '); $animate.enter(clone, null, jqLite(previousNode)); previousNode = clone; block.scope = childScope; - block.startNode = clone[0]; - block.elements = clone; + block.startNode = previousNode && previousNode.endNode ? previousNode.endNode : clone[0]; block.endNode = clone[clone.length - 1]; nextBlockMap[block.id] = block; }); @@ -390,5 +393,22 @@ var ngRepeatDirective = ['$parse', '$animate', function($parse, $animate) { }; } }; + + function getBlockElements(block) { + if (block.startNode === block.endNode) { + return jqLite(block.startNode); + } + + var element = block.startNode; + var elements = [element]; + + do { + element = element.nextSibling; + if (!element) break; + elements.push(element); + } while (element !== block.endNode); + + return jqLite(elements); + } }]; diff --git a/test/BinderSpec.js b/test/BinderSpec.js index 3c204b64c48b..b553c68dcfd0 100644 --- a/test/BinderSpec.js +++ b/test/BinderSpec.js @@ -96,7 +96,9 @@ describe('Binder', function() { ''); items.unshift({a: 'C'}); @@ -105,8 +107,11 @@ describe('Binder', function() { ''); items.shift(); @@ -115,7 +120,9 @@ describe('Binder', function() { ''); items.shift(); @@ -134,6 +141,7 @@ describe('Binder', function() { ''); })); @@ -148,15 +156,15 @@ describe('Binder', function() { $rootScope.items = items; $rootScope.$apply(); - expect(element[0].childNodes.length - 1).toEqual(0); + expect(element[0].childNodes.length).toEqual(1); items.name = 'misko'; $rootScope.$apply(); - expect(element[0].childNodes.length - 1).toEqual(1); + expect(element[0].childNodes.length).toEqual(3); delete items.name; $rootScope.$apply(); - expect(element[0].childNodes.length - 1).toEqual(0); + expect(element[0].childNodes.length).toEqual(1); })); it('IfTextBindingThrowsErrorDecorateTheSpan', function() { @@ -223,13 +231,19 @@ describe('Binder', function() { '
'+ '' + ''+ + '' + ''+ + '' + '
'+ + '' + '
'+ '' + ''+ + '' + ''+ + '' + '
' + + '' + ''); })); @@ -306,15 +320,18 @@ describe('Binder', function() { '
' + '')($rootScope); $rootScope.$apply(); + var d1 = jqLite(element[0].childNodes[1]); - var d2 = jqLite(element[0].childNodes[2]); + var d2 = jqLite(element[0].childNodes[3]); expect(d1.hasClass('o')).toBeTruthy(); expect(d2.hasClass('e')).toBeTruthy(); expect(sortedHtml(element)).toBe( '
' + '' + '
' + + '' + '
' + + '' + '
'); })); @@ -420,7 +437,9 @@ describe('Binder', function() { ''); })); diff --git a/test/helpers/testabilityPatch.js b/test/helpers/testabilityPatch.js index 514a5fdbbdbd..41b5042ab273 100644 --- a/test/helpers/testabilityPatch.js +++ b/test/helpers/testabilityPatch.js @@ -226,6 +226,17 @@ function sortedHtml(element, showNgClass) { } +function childrenTagsOf(element) { + var tags = []; + + forEach(jqLite(element).children(), function(child) { + tags.push(child.nodeName.toLowerCase()); + }); + + return tags; +} + + // TODO(vojta): migrate these helpers into jasmine matchers /**a * This method is a cheap way of testing if css for a given node is not set to 'none'. It doesn't diff --git a/test/ng/directive/ngClassSpec.js b/test/ng/directive/ngClassSpec.js index f0989f4a9531..a788e45255b5 100644 --- a/test/ng/directive/ngClassSpec.js +++ b/test/ng/directive/ngClassSpec.js @@ -166,7 +166,7 @@ describe('ngClass', function() { element = $compile('