-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparseCSS3AnimationShorthand.js
More file actions
61 lines (61 loc) · 2.1 KB
/
parseCSS3AnimationShorthand.js
File metadata and controls
61 lines (61 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
* parseCSS3AnimationShorthand
* Parses a CSS3 Animation statement into an object.
* http://www.joelambert.co.uk
*
* Copyright 2011, Joe Lambert.
* Free to use under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*
* e.g. parseCSS3AnimationShorthand('boxRotate 600ms linear 2s');
*/
"use strict";
window.parseCSS3AnimationShorthand = function (statement) {
var props = {
name: null,
duration: null,
timingFunction: 'ease',
delay: 0,
iterationCount: 1,
direction: 'normal'
},
remainder = statement,
ms, t, fn, r, f, i, d;
/* -- Get duration/delay -- */
// Convert strings times in s or ms to ms integers
ms = function (t) {
return t.indexOf('ms') > -1 ? parseInt(t, 10) : parseInt(t, 10) * 1000;
};
t = statement.match(/[0-9]+m?s/g);
if (t) {
props.duration = t.length > 0 ? ms(t[0]) : props.duration;
props.delay = t.length > 1 ? ms(t[1]) : props.delay;
// Remove the found properties from the string
remainder = remainder.replace(t[0], ''); // Replace the original found string not the cleansed one
}
/* -- Get timing function -- */
fn = ['linear', 'ease', 'ease-in', 'ease-out', 'ease-in-out'];
r = new RegExp('(' + fn.join('(\\s|$)|').replace('-', '\-') + '|cubic-bezier\\(.*?\\))');
f = statement.match(r);
if (f) {
props.timingFunction = f.length > 0 ? f[0].replace(/\s+/g, '') : props.timingFunction;
remainder = remainder.replace(props.timingFunction, '');
}
/* -- Get iteration count -- */
i = statement.match(/(infinite|\s[0-9]+(\s|$))/);
if (i) {
props.iterationCount = i[0].replace(/\s+/g, '');
remainder = remainder.replace(i[0], '');
}
/* -- Get direction -- */
d = statement.match(/(normal|alternate)\s*$/);
if (d) {
props.direction = d[0].replace(/\s+/g, '');
remainder = remainder.replace(d[0], '');
}
remainder = remainder.split(' ');
if (remainder.length > 0) {
props.name = remainder[0];
}
return props;
};