Skip to content

Commit fcadeb2

Browse files
matskojasonaden
authored andcommitted
fix(animations): do not leak DOM nodes/styling for host triggered animations (#18853)
Closes #18606 PR Close #18853
1 parent fd701b0 commit fcadeb2

2 files changed

Lines changed: 55 additions & 8 deletions

File tree

‎packages/animations/browser/src/render/transition_animation_engine.ts‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -837,15 +837,15 @@ export class TransitionAnimationEngine {
837837
}
838838

839839
const allLeaveNodes: any[] = [];
840-
const leaveNodesWithoutAnimations: any[] = [];
840+
const leaveNodesWithoutAnimations = new Set<any>();
841841
for (let i = 0; i < this.collectedLeaveElements.length; i++) {
842842
const element = this.collectedLeaveElements[i];
843843
const details = element[REMOVAL_FLAG] as ElementAnimationState;
844844
if (details && details.setForRemoval) {
845845
addClass(element, LEAVE_CLASSNAME);
846846
allLeaveNodes.push(element);
847847
if (!details.hasAnimation) {
848-
leaveNodesWithoutAnimations.push(element);
848+
leaveNodesWithoutAnimations.add(element);
849849
}
850850
}
851851
}
@@ -937,12 +937,14 @@ export class TransitionAnimationEngine {
937937
}
938938

939939
// these can only be detected here since we have a map of all the elements
940-
// that have animations attached to them...
941-
const enterNodesWithoutAnimations: any[] = [];
940+
// that have animations attached to them... We use a set here in the event
941+
// multiple enter captures on the same element were caught in different
942+
// renderer namespaces (e.g. when a @trigger was on a host binding that had *ngIf)
943+
const enterNodesWithoutAnimations = new Set<any>();
942944
for (let i = 0; i < allEnterNodes.length; i++) {
943945
const element = allEnterNodes[i];
944946
if (!subTimelines.has(element)) {
945-
enterNodesWithoutAnimations.push(element);
947+
enterNodesWithoutAnimations.add(element);
946948
}
947949
}
948950

@@ -1435,9 +1437,11 @@ function cloakElement(element: any, value?: string) {
14351437
}
14361438

14371439
function cloakAndComputeStyles(
1438-
driver: AnimationDriver, elements: any[], elementPropsMap: Map<any, Set<string>>,
1440+
driver: AnimationDriver, elements: Set<any>, elementPropsMap: Map<any, Set<string>>,
14391441
defaultStyle: string): [Map<any, ɵStyleData>, any[]] {
1440-
const cloakVals = elements.map(element => cloakElement(element));
1442+
const cloakVals: string[] = [];
1443+
elements.forEach(element => cloakVals.push(cloakElement(element)));
1444+
14411445
const valuesMap = new Map<any, ɵStyleData>();
14421446
const failedElements: any[] = [];
14431447

@@ -1456,7 +1460,10 @@ function cloakAndComputeStyles(
14561460
valuesMap.set(element, styles);
14571461
});
14581462

1459-
elements.forEach((element, i) => cloakElement(element, cloakVals[i]));
1463+
// we use a index variable here since Set.forEach(a, i) does not return
1464+
// an index value for the closure (but instead just the value)
1465+
let i = 0;
1466+
elements.forEach(element => cloakElement(element, cloakVals[i++]));
14601467
return [valuesMap, failedElements];
14611468
}
14621469

‎packages/core/test/animation/animation_integration_spec.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,46 @@ export function main() {
733733
flushMicrotasks();
734734
expect(fixture.debugElement.nativeElement.children.length).toBe(0);
735735
}));
736+
737+
it('should properly evaluate pre/auto-style values when components are inserted/removed which contain host animations',
738+
fakeAsync(() => {
739+
@Component({
740+
selector: 'parent-cmp',
741+
template: `
742+
<child-cmp *ngFor="let item of items"></child-cmp>
743+
`
744+
})
745+
class ParentCmp {
746+
items: any[] = [1, 2, 3, 4, 5];
747+
}
748+
749+
@Component({
750+
selector: 'child-cmp',
751+
template: '... child ...',
752+
animations:
753+
[trigger('host', [transition(':leave', [animate(1000, style({opacity: 0}))])])]
754+
})
755+
class ChildCmp {
756+
@HostBinding('@host') public hostAnimation = 'a';
757+
}
758+
759+
TestBed.configureTestingModule({declarations: [ParentCmp, ChildCmp]});
760+
761+
const engine = TestBed.get(ɵAnimationEngine);
762+
const fixture = TestBed.createComponent(ParentCmp);
763+
const cmp = fixture.componentInstance;
764+
const element = fixture.nativeElement;
765+
fixture.detectChanges();
766+
767+
cmp.items = [0, 2, 4, 6]; // 1,3,5 get removed
768+
fixture.detectChanges();
769+
770+
const items = element.querySelectorAll('child-cmp');
771+
for (let i = 0; i < items.length; i++) {
772+
const item = items[i];
773+
expect(item.style['display']).toBeFalsy();
774+
}
775+
}));
736776
});
737777

738778
it('should cancel and merge in mid-animation styles into the follow-up animation, but only for animation keyframes that start right away',

0 commit comments

Comments
 (0)