Skip to content

Commit

Permalink
Make the compiler also transform prefixed @Keyframes
Browse files Browse the repository at this point in the history
  • Loading branch information
kaisermann committed May 27, 2018
1 parent 5479973 commit abae281
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/css/Stylesheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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 Element from '../compile/nodes/Element';
import { Validator } from '../validate/index';
import { Node, Ast, Warning } from '../interfaces';
Expand All @@ -26,7 +27,7 @@ class Rule {
}

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

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

const attr = `.${id}`;

Expand Down Expand Up @@ -142,7 +143,7 @@ class Atrule {
});
}

else if (this.node.name === 'keyframes') {
else if (isKeyframesNode(this.node)) {
this.children.forEach((rule: Rule) => {
rule.selectors.forEach(selector => {
selector.used = true;
Expand All @@ -167,8 +168,8 @@ class Atrule {
});

code.remove(c, this.node.block.start);
} else if (this.node.name === 'keyframes') {
let c = this.node.start + 10;
} else if (isKeyframesNode(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;
if (this.node.block.start - c > 0) code.remove(c, this.node.block.start);
Expand Down Expand Up @@ -200,7 +201,7 @@ class Atrule {
}

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

if (node.name === 'keyframes') {
if (isKeyframesNode(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
8 changes: 8 additions & 0 deletions src/utils/isKeyframesNode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

import { Node } from '../interfaces';

export default function isKeyframesNode(node: Node): boolean {
return ['', '-webkit-','-moz-','-o-'].some(prefix =>
node.name === `${prefix}keyframes`
)
}
1 change: 1 addition & 0 deletions test/css/samples/keyframes-autoprefixed/expected.css

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

32 changes: 32 additions & 0 deletions test/css/samples/keyframes-autoprefixed/input.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<div class='animated'>animated</div>
<div class='also-animated'>also animated</div>

<style>
@keyframes why {
0% { color: red; }
100% { color: blue; }
}

@-webkit-keyframes why {
0% { color: red; }
100% { color: blue; }
}

@-moz-keyframes why {
0% { color: red; }
100% { color: blue; }
}

@-o-keyframes why {
0% { color: red; }
100% { color: blue; }
}

.animated {
animation: why 2s;
}

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

0 comments on commit abae281

Please sign in to comment.