Skip to content

Commit

Permalink
Fix prefixed animation name replacement
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisermann committed Jun 22, 2018
1 parent 17000e3 commit 478bb7a
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ indent_size = 2
charset = utf-8
trim_trailing_whitespace = true

[test/**/expected.css]
insert_final_newline = false

[{package.json,.travis.yml,.eslintrc.json}]
indent_style = space
indent_size = 2
18 changes: 10 additions & 8 deletions src/css/Stylesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import { getLocator } from 'locate-character';
import Selector from './Selector';
import getCodeFrame from '../utils/getCodeFrame';
import hash from '../utils/hash';
import isKeyframesNode from '../utils/isKeyframesNode';
import removeCssPrefix from '../utils/removeCSSPrefix';
import Element from '../compile/nodes/Element';
import { Validator } from '../validate/index';
import { Node, Ast, Warning } from '../interfaces';

const isKeyframeNode = (node: Node) => removeCssPrefix(node.name) === 'keyframes'

class Rule {
selectors: Selector[];
declarations: Declaration[];
Expand All @@ -27,7 +29,7 @@ class Rule {
}

isUsed(dev: boolean) {
if (this.parent && this.parent.node.type === 'Atrule' && isKeyframesNode(this.parent.node)) return true;
if (this.parent && this.parent.node.type === 'Atrule' && isKeyframeNode(this.parent.node)) return true;
if (this.declarations.length === 0) return dev;
return this.selectors.some(s => s.used);
}
Expand Down Expand Up @@ -68,7 +70,7 @@ class Rule {
}

transform(code: MagicString, id: string, keyframes: Map<string, string>) {
if (this.parent && this.parent.node.type === 'Atrule' && isKeyframesNode(this.parent.node)) return true;
if (this.parent && this.parent.node.type === 'Atrule' && isKeyframeNode(this.parent.node)) return true;

const attr = `.${id}`;

Expand Down Expand Up @@ -97,7 +99,7 @@ class Declaration {
}

transform(code: MagicString, keyframes: Map<string, string>) {
const property = this.node.property && this.node.property.toLowerCase();
const property = this.node.property && removeCssPrefix(this.node.property.toLowerCase());
if (property === 'animation' || property === 'animation-name') {
this.node.value.children.forEach((block: Node) => {
if (block.type === 'Identifier') {
Expand Down Expand Up @@ -143,7 +145,7 @@ class Atrule {
});
}

else if (isKeyframesNode(this.node)) {
else if (isKeyframeNode(this.node)) {
this.children.forEach((rule: Rule) => {
rule.selectors.forEach(selector => {
selector.used = true;
Expand All @@ -168,7 +170,7 @@ class Atrule {
});

code.remove(c, this.node.block.start);
} else if (isKeyframesNode(this.node)) {
} else if (isKeyframeNode(this.node)) {
let c = this.node.start + this.node.name.length + 1;
if (this.node.expression.start - c > 1) code.overwrite(c, this.node.expression.start, ' ');
c = this.node.expression.end;
Expand Down Expand Up @@ -201,7 +203,7 @@ class Atrule {
}

transform(code: MagicString, id: string, keyframes: Map<string, string>) {
if (isKeyframesNode(this.node)) {
if (isKeyframeNode(this.node)) {
this.node.expression.children.forEach(({ type, name, start, end }: Node) => {
if (type === 'Identifier') {
if (name.startsWith('-global-')) {
Expand Down Expand Up @@ -288,7 +290,7 @@ export default class Stylesheet {
this.children.push(atrule);
}

if (isKeyframesNode(node)) {
if (isKeyframeNode(node)) {
node.expression.children.forEach((expression: Node) => {
if (expression.type === 'Identifier' && !expression.name.startsWith('-global-')) {
this.keyframes.set(expression.name, `${this.id}-${expression.name}`);
Expand Down
7 changes: 0 additions & 7 deletions src/utils/isKeyframesNode.ts

This file was deleted.

3 changes: 3 additions & 0 deletions src/utils/removeCSSPrefix.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function(name: string): string {
return name.replace(/^-((webkit)|(moz)|(o)|(ms))-/, '');
}
2 changes: 1 addition & 1 deletion test/css/samples/keyframes-autoprefixed/expected.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions test/css/samples/keyframes-autoprefixed/input.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
}

.animated {
-webkit-animation: why 2s;
animation: why 2s;
}

.also-animated {
-webkit-animation: not-defined-here 2s;
animation: not-defined-here 2s;
}
</style>

0 comments on commit 478bb7a

Please sign in to comment.