fix(animations): set animations styles properly on platform-server (#24624)
Animations styles weren't getting properly set on platform-server because of erroneous checks and absence of reflection of style property to attribute on the server. The fix corrects the check for platform and explicitly reflects the style property to the attribute. PR Close #24624
This commit is contained in:

committed by
Miško Hevery

parent
1e139d4339
commit
6e20e0aac8
@ -8,6 +8,7 @@
|
||||
import {AnimateTimings, AnimationMetadata, AnimationMetadataType, AnimationOptions, sequence, ɵStyleData} from '@angular/animations';
|
||||
import {Ast as AnimationAst, AstVisitor as AnimationAstVisitor} from './dsl/animation_ast';
|
||||
import {AnimationDslVisitor} from './dsl/animation_dsl_visitor';
|
||||
import {isNode} from './render/shared';
|
||||
|
||||
export const ONE_SECOND = 1000;
|
||||
|
||||
@ -125,12 +126,47 @@ export function copyStyles(
|
||||
return destination;
|
||||
}
|
||||
|
||||
function getStyleAttributeString(element: any, key: string, value: string) {
|
||||
// Return the key-value pair string to be added to the style attribute for the
|
||||
// given CSS style key.
|
||||
if (value) {
|
||||
return key + ':' + value + ';';
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
function writeStyleAttribute(element: any) {
|
||||
// Read the style property of the element and manually reflect it to the
|
||||
// style attribute. This is needed because Domino on platform-server doesn't
|
||||
// understand the full set of allowed CSS properties and doesn't reflect some
|
||||
// of them automatically.
|
||||
let styleAttrValue = '';
|
||||
for (let i = 0; i < element.style.length; i++) {
|
||||
const key = element.style.item(i);
|
||||
styleAttrValue += getStyleAttributeString(element, key, element.style.getPropertyValue(key));
|
||||
}
|
||||
for (const key in element.style) {
|
||||
// Skip internal Domino properties that don't need to be reflected.
|
||||
if (!element.style.hasOwnProperty(key) || key.startsWith('_')) {
|
||||
continue;
|
||||
}
|
||||
const dashKey = camelCaseToDashCase(key);
|
||||
styleAttrValue += getStyleAttributeString(element, dashKey, element.style[key]);
|
||||
}
|
||||
element.setAttribute('style', styleAttrValue);
|
||||
}
|
||||
|
||||
export function setStyles(element: any, styles: ɵStyleData) {
|
||||
if (element['style']) {
|
||||
Object.keys(styles).forEach(prop => {
|
||||
const camelProp = dashCaseToCamelCase(prop);
|
||||
element.style[camelProp] = styles[prop];
|
||||
});
|
||||
// On the server set the 'style' attribute since it's not automatically reflected.
|
||||
if (isNode()) {
|
||||
writeStyleAttribute(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -140,6 +176,10 @@ export function eraseStyles(element: any, styles: ɵStyleData) {
|
||||
const camelProp = dashCaseToCamelCase(prop);
|
||||
element.style[camelProp] = '';
|
||||
});
|
||||
// On the server set the 'style' attribute since it's not automatically reflected.
|
||||
if (isNode()) {
|
||||
writeStyleAttribute(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -231,6 +271,10 @@ export function dashCaseToCamelCase(input: string): string {
|
||||
return input.replace(DASH_CASE_REGEXP, (...m: any[]) => m[1].toUpperCase());
|
||||
}
|
||||
|
||||
function camelCaseToDashCase(input: string): string {
|
||||
return input.replace(/([a-z])([A-Z])/g, '$1-$2').toLowerCase();
|
||||
}
|
||||
|
||||
export function allowPreviousPlayerStylesMerge(duration: number, delay: number) {
|
||||
return duration === 0 || delay === 0;
|
||||
}
|
||||
|
Reference in New Issue
Block a user