Skip to content

Instantly share code, notes, and snippets.

@xem
Forked from 140bytes/LICENSE.txt
Last active December 14, 2015 08:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xem/5059125 to your computer and use it in GitHub Desktop.
Save xem/5059125 to your computer and use it in GitHub Desktop.

Mini CSS Minifier 0.3

http://xem.github.com/miniMinifier/

  • Size: 140 characters.

  • Strong minification of CSS code (all the comments and the unnecessary spaces/tabs/line jumps and ";" are removed).

  • Includes a fix for IE that needs a space after some parenthesis (like url(...) no-repeat).

  • Fixes the two annoying 0.2 bugs

    • the function needed to be called twice on complex CSS files
    • and it removed spaces between classnames like ".class1 .class2"
function m(c){
return c
.replace(/\/\*(\s|.)*?\*\/|\s+/g, ' ') // replace comments, whitespaces, tabs, line breaks with a space
.replace(/^ *| *$|[ ;]*([{};,:!+>~\(\)]) */g, '$1') // trim spaces at the beginning and the end, trim spaces around [{};,:!+>~()] and trim ; before }
.replace(/\)([^;{}])/g, ') $1'); // IE bug: keep a space after ) unless it's followed by [;{}]
}
function m(c){return c.replace(/\/\*(\s|.)*?\*\/|\s+/g,' ').replace(/^ *| *$|[ ;]*([{};,:!+>~\(\)]) */g,'$1').replace(/\)([^;{}])/g,') $1')}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 YOUR_NAME_HERE <YOUR_URL_HERE>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "miniCssMinifier",
"description": "A mini CSS minifier.",
"keywords": [
"css",
"minify",
]
}
<!DOCTYPE html>
<title>Foo</title>
<div>Expected value: <b>*{margin:0;padding:0}html{background:#1F3D5B;font:normal 14px Calibri,Arial}</b></div>
<div>Actual value: <b id="ret"></b></div>
<script>
// write a small example that shows off the API for your example
// and tests it in one fell swoop.
var myFunction = function(c){return c.replace(/\/\*(\s|.)*?\*\/|\s+/g,' ').replace(/^ *| *$|[ ;]*([{};,:!+>~\(\)]) */g,'$1').replace(/\)([^;{}])/g,') $1')}
document.getElementById( "ret" ).innerHTML = myFunction(
"/* Style */
* { margin: 0; padding: 0; }
html { background: #1F3D5B; font: normal 14px Calibri, Arial; }
"
)
</script>
@xem
Copy link
Author

xem commented Apr 27, 2013

Hello, miniMinifier is now in v0.6, with lots of improvements and thanks to atk for his help.
There's a dedicated page: http://xem.github.io/miniMinifier/
And a new gist for it: https://gist.github.com/xem/5472247
Thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment