Created
February 25, 2010 13:38
-
-
Save edvakf/314541 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright (c) 2010, edvakf | |
// algorithm from http://www.ko-sw.com/Blog/post/An-Ultra-Fast-CSS-Minify-Algorithm.aspx | |
/* | |
* Copyright (c) 2010, KO Software ([email protected]) | |
* | |
* All rights reserved. | |
* | |
* Redistribution and use in source and binary forms, with or without | |
* modification, are permitted provided that the following conditions are met: | |
* | |
* * All original and modified versions of this source code must include the | |
* above copyright notice, this list of conditions and the following | |
* disclaimer. | |
* | |
* * This code may not be used with or within any modules or code that is | |
* licensed in any way that that compels or requires users or modifiers | |
* to release their source code or changes as a requirement for | |
* the use, modification or distribution of binary, object or source code | |
* based on the licensed source code. (ex: Cannot be used with GPL code.) | |
* | |
* * The name of KO Software may be used to endorse or promote products | |
* derived from this software without specific prior written permission. | |
* | |
* THIS SOFTWARE IS PROVIDED BY KO SOFTWARE ``AS IS'' AND ANY EXPRESS OR | |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO | |
* EVENT WILL KO SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; | |
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR | |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF | |
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
*/ | |
function minifyCSS(css) { | |
var | |
output = '', | |
len = css.length, | |
Punctuation = 1, Token = 2, StringD = 3, StringS = 4, | |
state = Punctuation, | |
oldpos = 0, pos = 0; | |
while (oldpos < len) { | |
var newstate = getState(); | |
if (pos > oldpos + 1) { | |
// if whitespace is found between two tokens, keep it compact | |
if (state === Token && newstate === Token) output += ' '; | |
} | |
if (pos === len) break; | |
oldpos = pos; | |
state = newstate; | |
// remove ";" as of "foo:bar;}" | |
if (state === Punctuation && css[pos] === '}' && output[output.length - 1] === ';') output = output.slice(0, -1); | |
output += css[pos++]; | |
} | |
return output; | |
// getState changes the value of pos and returns a state | |
function getState() { | |
var i = pos; | |
if (state === StringD) { | |
return (css[i] === '"' && !(pos > 0 && css[i-1] === '\\')) ? Token : StringD; | |
} else if (state === StringS) { | |
return (css[i] === "'" && !(pos > 0 && css[i-1] === '\\')) ? Token : StringS; | |
} | |
skip = true; | |
while (skip) { | |
// skip whitespaces | |
while (skip = (i < len && /[ \t\r\n]/.test(css[i]))) i++; | |
// skip comments | |
if (i < len - 1 && css[i] === '/' && css[i+1] === '*') { | |
skip = true; | |
while (i < len) { | |
i++; | |
if (css[i-1] === '*' && css[i] === '/') { | |
i++; | |
break; | |
} | |
} | |
} | |
} | |
pos = i; | |
if (/[a-zA-Z0-9#_@*.-]/.test(css[i])) { | |
return (state === StringD) ? StringD : Token; | |
} else if (css[i] === '"') { | |
return StringD; | |
} else if (css[i] === "'") { | |
return StringS; | |
} else { | |
return Punctuation; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment