Skip to content

Instantly share code, notes, and snippets.

@joelambert
Created June 7, 2011 10:24

Revisions

  1. joelambert revised this gist Jun 17, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion parseCSS3AnimationShorthand.js
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    * Parses a CSS3 Animation statement into an object.
    * http://www.joelambert.co.uk
    *
    * Copyright 2011, Joe Lambert. All rights reserved
    * Copyright 2011, Joe Lambert.
    * Free to use under the MIT license.
    * http://www.opensource.org/licenses/mit-license.php
    *
  2. joelambert revised this gist Jun 17, 2011. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions parseCSS3AnimationShorthand.js
    Original file line number Diff line number Diff line change
    @@ -36,7 +36,7 @@ window.parseCSS3AnimationShorthand = function(statement) {
    props.delay = t.length > 1 ? ms(t[1]) : props.delay;

    // Remove the found properties from the string
    for(i=0; i<t.length; remainder = remainder.replace(t[i], ''), i++);
    remainder = remainder.replace(f[0], ''); // Replace the original found string not the cleansed one
    }

    /* -- Get timing function -- */
    @@ -69,9 +69,9 @@ window.parseCSS3AnimationShorthand = function(statement) {
    remainder = remainder.replace(d[0], '');
    }

    remainder = remainder.replace(/\s+/g, '');
    remainder = remainder.split(' ');
    if(remainder.length > 0)
    props.name = remainder;
    props.name = remainder[0];

    return props;
    };
  3. Joe Lambert created this gist Jun 7, 2011.
    77 changes: 77 additions & 0 deletions parseCSS3AnimationShorthand.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,77 @@
    /**
    * parseCSS3AnimationShorthand
    * Parses a CSS3 Animation statement into an object.
    * http://www.joelambert.co.uk
    *
    * Copyright 2011, Joe Lambert. All rights reserved
    * Free to use under the MIT license.
    * http://www.opensource.org/licenses/mit-license.php
    *
    * e.g. parseCSS3AnimationShorthand('boxRotate 600ms linear 2s');
    */
    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
    for(i=0; i<t.length; remainder = remainder.replace(t[i], ''), i++);
    }

    /* -- 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.replace(/\s+/g, '');
    if(remainder.length > 0)
    props.name = remainder;

    return props;
    };