Skip to content

Commit 862dd03

Browse files
dario-piotrowiczthePunderWoman
authored andcommitted
refactor(animations): refactor non-animatable check to be timeline based (#46666)
move the check for non-animatable properties from the animation building phase to the application of the animation's transition instead, in such a way we can check it against the keyframes of the transition's timeline in order to only provide warnings for properties which are being animated, thus not providing any warning for non-animatable properties being applied to elements via the style function this change has the benfit just mentioned above but it comes with two drawbacks: - the warning handling is not done in the building time so it is a bit inconsistent with other type of validations (such as the unsupported css properties one for example) - before the warning was being applied only when the animation's data was being parsed, so it happed only once but now since it is applied when the animation is actually being prepared to be played, it happens each time the animation runs resolves #46602 PR Close #46666
1 parent 99697da commit 862dd03

5 files changed

Lines changed: 146 additions & 77 deletions

File tree

‎packages/animations/browser/src/dsl/animation_ast_builder.ts‎

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {invalidDefinition, invalidKeyframes, invalidOffset, invalidParallelAnima
1111
import {AnimationDriver} from '../render/animation_driver';
1212
import {getOrSetDefaultValue} from '../render/shared';
1313
import {convertToMap, copyObj, extractStyleParams, iteratorToArray, NG_ANIMATING_SELECTOR, NG_TRIGGER_SELECTOR, normalizeAnimationEntry, resolveTiming, SUBSTITUTION_EXPR_START, validateStyleParams, visitDslNode} from '../util';
14-
import {pushNonAnimatablePropertiesWarning, pushUnrecognizedPropertiesWarning} from '../warning_helpers';
14+
import {pushUnrecognizedPropertiesWarning} from '../warning_helpers';
1515

1616
import {AnimateAst, AnimateChildAst, AnimateRefAst, Ast, DynamicTimingAst, GroupAst, KeyframesAst, QueryAst, ReferenceAst, SequenceAst, StaggerAst, StateAst, StyleAst, TimingAst, TransitionAst, TriggerAst} from './animation_ast';
1717
import {AnimationDslVisitor} from './animation_dsl_visitor';
@@ -81,13 +81,6 @@ export class AnimationAstBuilderVisitor implements AnimationDslVisitor {
8181
[...context.unsupportedCSSPropertiesFound.keys()],
8282
);
8383
}
84-
85-
if (context.nonAnimatableCSSPropertiesFound.size) {
86-
pushNonAnimatablePropertiesWarning(
87-
warnings,
88-
[...context.nonAnimatableCSSPropertiesFound.keys()],
89-
);
90-
}
9184
}
9285

9386
return ast;
@@ -321,15 +314,6 @@ export class AnimationAstBuilderVisitor implements AnimationDslVisitor {
321314
context.unsupportedCSSPropertiesFound.add(prop);
322315
return;
323316
}
324-
325-
if (this._driver.validateAnimatableStyleProperty) {
326-
if (!this._driver.validateAnimatableStyleProperty(prop)) {
327-
context.nonAnimatableCSSPropertiesFound.add(prop);
328-
// note: non animatable properties are not removed for the tuple just in case they are
329-
// categorized as non animatable but can actually be animated
330-
return;
331-
}
332-
}
333317
}
334318

335319
// This is guaranteed to have a defined Map at this querySelector location making it
@@ -535,7 +519,6 @@ export class AnimationAstBuilderContext {
535519
public collectedStyles = new Map<string, Map<string, StyleTimeTuple>>();
536520
public options: AnimationOptions|null = null;
537521
public unsupportedCSSPropertiesFound: Set<string> = new Set<string>();
538-
public readonly nonAnimatableCSSPropertiesFound: Set<string> = new Set<string>();
539522
constructor(public errors: Error[]) {}
540523
}
541524

‎packages/animations/browser/src/dsl/animation_transition_factory.ts‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {copyObj, interpolateParams, iteratorToArray} from '../util';
1313

1414
import {StyleAst, TransitionAst} from './animation_ast';
1515
import {buildAnimationTimelines} from './animation_timeline_builder';
16+
import {AnimationTimelineInstruction} from './animation_timeline_instruction';
1617
import {TransitionMatcherFn} from './animation_transition_expr';
1718
import {AnimationTransitionInstruction, createTransitionInstruction} from './animation_transition_instruction';
1819
import {ElementInstructionMap} from './element_instruction_map';
@@ -91,13 +92,65 @@ export class AnimationTransitionFactory {
9192
}
9293
});
9394

95+
if (typeof ngDevMode === 'undefined' || ngDevMode) {
96+
checkNonAnimatableInTimelines(timelines, this._triggerName, driver);
97+
}
98+
9499
const queriedElementsList = iteratorToArray(queriedElements.values());
95100
return createTransitionInstruction(
96101
element, this._triggerName, currentState, nextState, isRemoval, currentStateStyles,
97102
nextStateStyles, timelines, queriedElementsList, preStyleMap, postStyleMap, totalTime);
98103
}
99104
}
100105

106+
/**
107+
* Checks inside a set of timelines if they try to animate a css property which is not considered
108+
* animatable, in that case it prints a warning on the console.
109+
* Besides that the function doesn't have any other effect.
110+
*
111+
* Note: this check is done here after the timelines are built instead of doing on a lower level so
112+
* that we can make sure that the warning appears only once per instruction (we can aggregate here
113+
* all the issues instead of finding them separately).
114+
*
115+
* @param timelines The built timelines for the current instruction.
116+
* @param triggerName The name of the trigger for the current instruction.
117+
* @param driver Animation driver used to perform the check.
118+
*
119+
*/
120+
function checkNonAnimatableInTimelines(
121+
timelines: AnimationTimelineInstruction[], triggerName: string, driver: AnimationDriver): void {
122+
if (!driver.validateAnimatableStyleProperty) {
123+
return;
124+
}
125+
126+
const invalidNonAnimatableProps = new Set<string>();
127+
128+
timelines.forEach(({keyframes}) => {
129+
const nonAnimatablePropsInitialValues = new Map<string, string|number>();
130+
keyframes.forEach(keyframe => {
131+
for (const [prop, value] of keyframe.entries()) {
132+
if (!driver.validateAnimatableStyleProperty!(prop)) {
133+
if (nonAnimatablePropsInitialValues.has(prop) && !invalidNonAnimatableProps.has(prop)) {
134+
const propInitialValue = nonAnimatablePropsInitialValues.get(prop);
135+
if (propInitialValue !== value) {
136+
invalidNonAnimatableProps.add(prop);
137+
}
138+
} else {
139+
nonAnimatablePropsInitialValues.set(prop, value);
140+
}
141+
}
142+
}
143+
});
144+
});
145+
146+
if (invalidNonAnimatableProps.size > 0) {
147+
console.warn(
148+
`Warning: The animation trigger "${triggerName}" is attempting to animate the following` +
149+
' not animatable properties: ' + Array.from(invalidNonAnimatableProps).join(', ') + '\n' +
150+
'(to check the list of all animatable properties visit https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties)');
151+
}
152+
}
153+
101154
function oneOrMoreTransitionsMatch(
102155
matchFns: TransitionMatcherFn[], currentState: any, nextState: any, element: any,
103156
params: {[key: string]: any}): boolean {

‎packages/animations/browser/src/warning_helpers.ts‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,3 @@ export function pushUnrecognizedPropertiesWarning(warnings: string[], props: str
3939
warnings.push(`The following provided properties are not recognized: ${props.join(', ')}`);
4040
}
4141
}
42-
43-
export function pushNonAnimatablePropertiesWarning(warnings: string[], props: string[]): void {
44-
if (props.length) {
45-
warnings.push(`The following provided properties are not animatable: ${
46-
props.join(
47-
', ')}\n (see: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties)`);
48-
}
49-
}

‎packages/animations/browser/test/dsl/animation_spec.ts‎

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -244,56 +244,6 @@ function createDiv() {
244244
]);
245245
});
246246

247-
it('should provide a warning if a non-animatable CSS property is used in the animation',
248-
() => {
249-
const steps = [animate(1000, style({display: 'block'}))];
250-
251-
expect(getValidationWarningsForAnimationSequence(steps)).toEqual([
252-
'The following provided properties are not animatable: display' +
253-
'\n (see: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties)'
254-
]);
255-
});
256-
257-
it('should not provide a non-animatable warning if an animatable CSS property is used in the animation',
258-
() => {
259-
const steps = [animate('1.5s', style({borderRadius: '1rem'}))];
260-
261-
expect(getValidationWarningsForAnimationSequence(steps)).toEqual([]);
262-
});
263-
264-
it('should provide a warning if multiple non-animatable CSS properties are used in the animation',
265-
() => {
266-
const steps = [
267-
state('state', style({
268-
display: 'block',
269-
})),
270-
style({textAlign: 'right'}), animate(1000, style({'font-family': 'sans-serif'}))
271-
];
272-
273-
expect(getValidationWarningsForAnimationSequence(steps)).toEqual([
274-
'The following provided properties are not animatable: display, textAlign, font-family' +
275-
'\n (see: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties)'
276-
]);
277-
});
278-
279-
it('should provide a warnings if both invalid and non-animatable CSS properties are used in the animation',
280-
() => {
281-
const steps = [
282-
state('state', style({
283-
display: 'block',
284-
abc: 123,
285-
})),
286-
style({textAlign: 'right'}),
287-
animate(1000, style({'font-family': 'sans-serif', xyz: 456}))
288-
];
289-
290-
expect(getValidationWarningsForAnimationSequence(steps)).toEqual([
291-
'The following provided properties are not recognized: abc, xyz',
292-
'The following provided properties are not animatable: display, textAlign, font-family' +
293-
'\n (see: https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties)'
294-
]);
295-
});
296-
297247
it('should allow a vendor-prefixed property to be used in an animation sequence without throwing an error',
298248
() => {
299249
const steps = [

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

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Use of this source code is governed by an MIT-style license that can be
66
* found in the LICENSE file at https://angular.io/license
77
*/
8-
import {animate, animateChild, AnimationEvent, AnimationOptions, AUTO_STYLE, group, keyframes, query, state, style, transition, trigger, ɵPRE_STYLE as PRE_STYLE} from '@angular/animations';
8+
import {animate, animateChild, AnimationEvent, AnimationMetadata, AnimationOptions, AUTO_STYLE, group, keyframes, query, state, style, transition, trigger, ɵPRE_STYLE as PRE_STYLE} from '@angular/animations';
99
import {AnimationDriver, ɵAnimationEngine, ɵNoopAnimationDriver as NoopAnimationDriver} from '@angular/animations/browser';
1010
import {MockAnimationDriver, MockAnimationPlayer} from '@angular/animations/browser/testing';
1111
import {ChangeDetectorRef, Component, HostBinding, HostListener, Inject, RendererFactory2, ViewChild} from '@angular/core';
@@ -4049,6 +4049,97 @@ describe('animation tests', function() {
40494049
});
40504050
});
40514051
});
4052+
4053+
describe('non-animatable css props', () => {
4054+
function buildAndAnimateSimpleTestComponent(triggerAnimationData: AnimationMetadata[]) {
4055+
@Component({
4056+
selector: 'cmp',
4057+
template: `
4058+
<div *ngIf="exp" [@myAnimation]="exp">
4059+
<p *ngIf="exp"></p>
4060+
</div>
4061+
`,
4062+
animations: [trigger('myAnimation', [transition('void => *', triggerAnimationData)])]
4063+
})
4064+
class Cmp {
4065+
exp: any = false;
4066+
}
4067+
4068+
TestBed.configureTestingModule({declarations: [Cmp]});
4069+
const engine = TestBed.inject(ɵAnimationEngine);
4070+
const fixture = TestBed.createComponent(Cmp);
4071+
const cmp = fixture.componentInstance;
4072+
cmp.exp = true;
4073+
fixture.detectChanges();
4074+
engine.flush();
4075+
}
4076+
4077+
it('should show a warning if the animation tries to transition a non-animatable property', () => {
4078+
const spyWarn = spyOn(console, 'warn');
4079+
buildAndAnimateSimpleTestComponent(
4080+
[style({display: 'block'}), animate(500, style({display: 'inline'}))]);
4081+
expect(spyWarn).toHaveBeenCalledOnceWith(
4082+
'Warning: The animation trigger "myAnimation" is attempting to animate the following' +
4083+
' not animatable properties: display' +
4084+
'\n' +
4085+
'(to check the list of all animatable properties visit https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties)');
4086+
});
4087+
4088+
it('should not show a warning if the animation tries to transition an animatable property',
4089+
() => {
4090+
const spyWarn = spyOn(console, 'warn');
4091+
buildAndAnimateSimpleTestComponent(
4092+
[style({fontSize: 5}), animate(500, style({fontSize: 15}))]);
4093+
expect(spyWarn).not.toHaveBeenCalled();
4094+
});
4095+
4096+
it('should show a single warning if the animation tries to transition multiple non-animatable properties',
4097+
() => {
4098+
const spyWarn = spyOn(console, 'warn');
4099+
buildAndAnimateSimpleTestComponent([
4100+
style({display: 'block', fontStyle: 'normal'}),
4101+
animate(500, style({display: 'inline', fontStyle: 'italic'}))
4102+
]);
4103+
expect(spyWarn).toHaveBeenCalledOnceWith(
4104+
'Warning: The animation trigger "myAnimation" is attempting to animate the following' +
4105+
' not animatable properties: display, fontStyle' +
4106+
'\n' +
4107+
'(to check the list of all animatable properties visit https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties)');
4108+
});
4109+
4110+
it('should only warn on non-animatable properties if the animation tries to transition both animate and non-animatable properties',
4111+
() => {
4112+
const spyWarn = spyOn(console, 'warn');
4113+
buildAndAnimateSimpleTestComponent([
4114+
style({'flex-direction': 'column', fontSize: 5}),
4115+
animate(500, style({'flex-direction': 'row', fontSize: 10}))
4116+
]);
4117+
expect(spyWarn).toHaveBeenCalledOnceWith(
4118+
'Warning: The animation trigger "myAnimation" is attempting to animate the following' +
4119+
' not animatable properties: flex-direction' +
4120+
'\n' +
4121+
'(to check the list of all animatable properties visit https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_animated_properties)');
4122+
});
4123+
4124+
it('should not show a warning if the animation uses but does not animate a non-animatable property',
4125+
() => {
4126+
const spyWarn = spyOn(console, 'warn');
4127+
buildAndAnimateSimpleTestComponent([transition('void => *', [style({display: 'block'})])]);
4128+
expect(spyWarn).not.toHaveBeenCalled();
4129+
});
4130+
4131+
it('should not show a warning if the same non-animatable property is used (with different values) on different elements in the same transition',
4132+
() => {
4133+
const spyWarn = spyOn(console, 'warn');
4134+
buildAndAnimateSimpleTestComponent([
4135+
style({position: 'relative'}),
4136+
query(':enter', [style({
4137+
position: 'absolute',
4138+
})]),
4139+
]);
4140+
expect(spyWarn).not.toHaveBeenCalled();
4141+
});
4142+
});
40524143
});
40534144
})();
40544145

0 commit comments

Comments
 (0)