* Scope an animation rule so that the keyframes mentioned in such rule * are scoped if defined in the component's css and left untouched otherwise. * * It can scope values of both the 'animation' and 'animation-name' properties. * * @param rule css rule to scope. * @param scopeSele
(
rule: CssRule,
scopeSelector: string,
unscopedKeyframesSet: ReadonlySet<string>,
)
| 356 | * @returns the updated css rule. |
| 357 | **/ |
| 358 | private _scopeAnimationRule( |
| 359 | rule: CssRule, |
| 360 | scopeSelector: string, |
| 361 | unscopedKeyframesSet: ReadonlySet<string>, |
| 362 | ): CssRule { |
| 363 | let content = rule.content.replace( |
| 364 | /((?:^|\s+|;)(?:-webkit-)?animation\s*:\s*)([^;]+)/g, |
| 365 | (_, start, animationDeclarations) => |
| 366 | start + |
| 367 | animationDeclarations.replace( |
| 368 | this._animationDeclarationKeyframesRe, |
| 369 | ( |
| 370 | original: string, |
| 371 | leadingSpaces: string, |
| 372 | quote = '', |
| 373 | quotedName: string, |
| 374 | nonQuotedName: string, |
| 375 | ) => { |
| 376 | if (quotedName) { |
| 377 | return `${leadingSpaces}${this._scopeAnimationKeyframe( |
| 378 | `${quote}${quotedName}${quote}`, |
| 379 | scopeSelector, |
| 380 | unscopedKeyframesSet, |
| 381 | )}`; |
| 382 | } else { |
| 383 | return animationKeywords.has(nonQuotedName) |
| 384 | ? original |
| 385 | : `${leadingSpaces}${this._scopeAnimationKeyframe( |
| 386 | nonQuotedName, |
| 387 | scopeSelector, |
| 388 | unscopedKeyframesSet, |
| 389 | )}`; |
| 390 | } |
| 391 | }, |
| 392 | ), |
| 393 | ); |
| 394 | content = content.replace( |
| 395 | /((?:^|\s+|;)(?:-webkit-)?animation-name(?:\s*):(?:\s*))([^;]+)/g, |
| 396 | (_match, start, commaSeparatedKeyframes) => |
| 397 | `${start}${commaSeparatedKeyframes |
| 398 | .split(',') |
| 399 | .map((keyframe: string) => |
| 400 | this._scopeAnimationKeyframe(keyframe, scopeSelector, unscopedKeyframesSet), |
| 401 | ) |
| 402 | .join(',')}`, |
| 403 | ); |
| 404 | return {...rule, content}; |
| 405 | } |
| 406 | |
| 407 | /* Ensure styles are scoped. Pseudo-scoping takes a rule like: |
| 408 | * |
no test coverage detected