Skip to content

Instantly share code, notes, and snippets.

@xDimGG
Last active July 7, 2017 02:24
Show Gist options
  • Save xDimGG/738658d954f18618ed82c7ebdd677885 to your computer and use it in GitHub Desktop.
Save xDimGG/738658d954f18618ed82c7ebdd677885 to your computer and use it in GitHub Desktop.
(function() {
// Minified CSS Beautifier : http://dpidudyah7i0b.cloudfront.net/codemirror/jsb/lib/beautify-css.js
function css_beautify(e,n){function i(){return(h=e.charAt(++_))||""}function s(n){var s=_;return n&&r(),result=e.charAt(_+1)||"",_=s-1,i(),result}function t(n){for(var s=_;i();)if("\\"===h)i();else{if(-1!==n.indexOf(h))break;if("\n"===h)break}return e.substring(s,_+1)}function r(){for(var e="";l.test(s());)i(),e+=h;return e}function u(n){var t=_,n="/"===s();for(i();i();){if(!n&&"*"===h&&"/"===s()){i();break}if(n&&"\n"===h)return e.substring(t,_)}return e.substring(t,_)+h}function a(n){return e.substring(_-n.length,_).toLowerCase()===n}var f=(n=n||{}).indent_size||4,c=n.indent_char||" ",p=void 0===n.selector_separator_newline||n.selector_separator_newline,o=void 0!==n.end_with_newline&&n.end_with_newline;"string"==typeof f&&(f=parseInt(f,10));var h,l=/^\s+$/,_=-1,g=e.match(/^[\t ]*/)[0],d=new Array(f+1).join(c),b=0,w=0,y={};y["{"]=function(e){y.singleSpace(),v.push(e),y.newLine()},y["}"]=function(e){y.newLine(),v.push(e),y.newLine()},y._lastCharWhitespace=function(){return l.test(v[v.length-1])},y.newLine=function(e){e||y.trim(),v.length&&v.push("\n"),g&&v.push(g)},y.singleSpace=function(){v.length&&!y._lastCharWhitespace()&&v.push(" ")},y.trim=function(){for(;y._lastCharWhitespace();)v.pop()};var v=[];g&&v.push(g);for(var L=!1,S=!1,m="",O="";;){var A=function(){var e="";for(h&&l.test(h)&&(e=h);l.test(i());)e+=h;return e}(),E=""!==A,C=-1!==A.indexOf("\n"),O=m,m=h;if(!h)break;if("/"===h&&"*"===s()){var N=a("");y.newLine(),v.push(u()),y.newLine(),N&&y.newLine(!0)}else if("/"===h&&"/"===s())C||"{"===O||y.trim(),y.singleSpace(),v.push(u()),y.newLine();else if("@"===h){E&&y.singleSpace(),v.push(h);var R=function(e){var n=_,s=t(e);return _=n-1,i(),s}(": ,;{}()[]/='\"").replace(/\s$/,"");R in css_beautify.NESTED_AT_RULE?(w+=1,R in css_beautify.CONDITIONAL_GROUP_RULE&&(S=!0)):": ".indexOf(R[R.length-1])>=0&&(i(),R=t(": ").replace(/\s$/,""),v.push(R),y.singleSpace())}else"{"===h?"}"===s(!0)?(r(),i(),y.singleSpace(),v.push("{}")):(!function(){b++,g+=d}(),y["{"](h),S?(S=!1,L=b>w):L=b>=w):"}"===h?(!function(){b--,g=g.slice(0,-f)}(),y["}"](h),L=!1,w&&w--):":"===h?(r(),!L&&!S||a("&")||function(){for(var n=_+1;n<e.length;n++){var i=e.charAt(n);if("{"===i)return!0;if(";"===i||"}"===i||")"===i)return!1}return!1}()?":"===s()?(i(),v.push("::")):v.push(":"):(v.push(":"),y.singleSpace())):'"'===h||"'"===h?(E&&y.singleSpace(),v.push(t(h))):";"===h?(v.push(h),y.newLine()):"("===h?a("url")?(v.push(h),r(),i()&&(")"!==h&&'"'!==h&&"'"!==h?v.push(t(")")):_--)):(E&&y.singleSpace(),v.push(h),r()):")"===h?v.push(h):","===h?(v.push(h),r(),!L&&p?y.newLine():y.singleSpace()):"]"===h?v.push(h):"["===h?(E&&y.singleSpace(),v.push(h)):"="===h?(r(),v.push(h)):(E&&y.singleSpace(),v.push(h))}var T=v.join("").replace(/[\r\n\t ]+$/,"");return o&&(T+="\n"),T}css_beautify.NESTED_AT_RULE={"@page":!0,"@font-face":!0,"@keyframes":!0,"@media":!0,"@supports":!0,"@document":!0},css_beautify.CONDITIONAL_GROUP_RULE={"@media":!0,"@supports":!0,"@document":!0},"function"==typeof define&&define.amd?define([],function(){return{css_beautify:css_beautify}}):"undefined"!=typeof exports?exports.css_beautify=css_beautify:"undefined"!=typeof window?window.css_beautify=css_beautify:"undefined"!=typeof global&&(global.css_beautify=css_beautify);
var l = document.getElementsByTagName('link'),
s = document.getElementsByTagName('style'),
la = [],
sa = [],
i;
for (i = 0; i < l.length; i++) {
if (l[i].rel === 'stylesheet') {
la.push(l[i].href);
}
}
for (i = 0; i < s.length; i++) {
sa.push(s[i].innerHTML);
}
with(window.open()) {
document.write('<textarea id="css" style="width:400px;height:200px"></textarea>');
for (i = 0; i < la.length; i++) {
document.getElementById('css').value += la[i] + '\n';
}
for (i = 0; i < sa.length; i++) {
document.getElementById('css').value += '\n-------\n\n' + css_beautify(sa[i]) + '\n';
}
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment