Thema: CSS Komprimierer
-
15.08.2012, 12:57 #1
CSS Komprimierer
Code:var COMMENTS = 1; var WHITESPACES = 2; var REPLACE_COLORS = 4; var EMPTY = 8; var ZEROVAL = 16; var DUPLICATED = 32; var METRICS = 64; var SEMICOLON = 128; function minimizeCSS (cssdata, options) { if (typeof options == 'undefined') var options = COMMENTS | WHITESPACES | REPLACE_COLORS | EMPTY | ZEROVAL | DUPLICATED | METRICS | SEMICOLON; var num = 'pt|pc|in|mm|cm|px|em|ex|%'; var cmap = new Array ( /*'#000000|black'*/'black|#000', '#FFFFFF|white', '#0000FF|blue', /*'#800000|maroon'*/'maroon|#800', /*'#008000|green'*/'green|#080', /*'#808000|olive'*/'olive|#880', '#000080|navy', /*'#800080|purple'*/'purple|#808', '#008080|teal', /*'#C0C0C0|silver'*/'silver|#CCC', '#808080|gray', '#FF0000|red', '#00FF00|lime', '#FFFF00|yellow', '#00FFFF|aqua' ); // (Geschachtelte-)Kommentare entfernen if (options & COMMENTS) while ((css2 = cssdata.replace (/\/\*+[^*]*\*+\//gi, "")) != cssdata) cssdata = css2; // Tabs, Newlines, Leerzeichen löschen if (options & WHITESPACES) { cssdata = cssdata.replace (/\n|\t|\r/g, ""); cssdata = cssdata.replace (/^\s*/g, ""); // Mehrere aufeinander folgende Whitespaces durch ein Einziges ersetzen cssdata = cssdata.replace (/\s+/g, " "); // Whitespaces am Anfang von Eigenschaften cssdata = cssdata.replace (/:\s+/g, ":"); // Leerzeichen zwischen den Regeln cssdata = cssdata.replace (/\s*({)\s*|\s*(})\s*/g, "$1$2"); // Leerzeichen vor und nach Semikolons cssdata = cssdata.replace (/\s*;\s*/g, ";"); // Kommata bei Trennung von Regeln ohne Whitespaces cssdata = cssdata.replace (/\s*,\s*/g, ","); } // Letztes Semikolon vor Ende einer Regel löschen cssdata = options & SEMICOLON ? cssdata.replace (/(;)(\s*)(})/g, "$2$3") : cssdata; if (options & ZEROVAL) { // Führende Nullen //cssdata = cssdata.replace (/(\s:)0+([0-9.]+[a-z]{2})/gi, "$1$2"); cssdata = cssdata.replace (/([\s:])0+([1-9][0-9]*)/gi, "$1$2"); // Abschließende Nullen bei Gleitkommazahlen cssdata = cssdata.replace (/([\s:])([1-9][0-9]*\.[0-9]*[^0])0+/gi, "$1$2"); // 0px entspricht 0 //cssdata = cssdata.replace (/([\s|:])0px([\s;])/gi, "$10$2"); nums = num.split ('|'); for (i = 0; i < nums.length; i++) cssdata = cssdata.replace (new RegExp ('([\\s|:])0+' + nums[i] + '([\\s;])', 'gi'), "$10$2"); } if (options & REPLACE_COLORS) { // Farbwerte verkürzen if (res = cssdata.match (/#([0-9a-f]0){3}/gi)) for (var i = 0; i < res.length; i++) cssdata = cssdata.replace (new RegExp (res[i], 'gi'), '#' + res[i][1] + res[i][3] + res[i][5]); // Farbwerte ersetzen for (i = 0; i < cmap.length; i++) { col = cmap[i].split ('|'); cssdata = cssdata.replace (new RegExp ('([\\s|:])' + col[0] + '([\\s|;|}])', 'gi'), "$1" + col[1] + "$2"); } } if (options & METRICS) { // Wiederholung von Maßangaben if (res = cssdata.match (/(([0-9.]+[a-z]{0,2}[ ]?){4})/gi)) for (var i = 0; i < res.length; i++) { var s = res[i].split (' '); if (s[0] == s[1] && s[2] == s[3] && s[0] == s[2]) cssdata = cssdata.replace (new RegExp (res[i], 'gi'), s[0]); else if (s[0] == s[2] && s[1] == s[3]) cssdata = cssdata.replace (new RegExp (res[i], 'gi'), s[0] + ' ' + s[1]); } } if (options & DUPLICATED) { // Dubletten entfernen und nur die letzte Regel einer Art übernehmen if (r = cssdata.match (/[^}]+{[^}]+}/g)) for (l = 0; l < r.length; l++) { if (res = r[l].match (/{([^}]+)}/)) for (i = 0; i < res.length; i++) { var res2 = res[i].replace (/{|}/g, ''); var res3 = res2.split (';'); for (var j = res3.length - 1; j > 0; j--) { var attr = res3[j].split (':')[0]; for (var k = 0; k < j; k++) if (res3[k].split (':')[0] == attr) res3[k] = ''; } var n = ''; for (j = 0; j < res3.length; j++) n += res3[j] != '' ? res3[j] + (j + 1 < res3.length ? ';' : '') : ''; } src = r[l].split ('{')[0].replace (/\+/g, '\\+').replace (/\*/g, '\\*') + '{' + r[l].split ('{')[1]; cssdata = cssdata.replace (new RegExp (src), r[l].split ('{')[0] + '{' + n + '}'); } } // Zusammengehörige Eigenschaften zusammenfassen folgt noch... vielleicht... // Leere Regeln komplett aus dem Stylesheet tilgen //return options & EMPTY ? cssdata.replace (/}[^{]+{}/g, "}") : cssdata; return options & EMPTY ? cssdata.replace (/[a-z0-9.,+>*\t\r\n\s]+{[\t\r\n\s]*}/g, "") : cssdata; } function do_compress (css) { if (css.length == 0) return false; var options = 0 | (document.getElementById ('comments').checked ? COMMENTS : 0) | (document.getElementById ('whitespaces').checked ? WHITESPACES : 0) | (document.getElementById ('replace_colors').checked ? REPLACE_COLORS : 0) | (document.getElementById ('empty').checked ? EMPTY : 0) | (document.getElementById ('zeroval').checked ? ZEROVAL : 0) | (document.getElementById ('duplicated').checked ? DUPLICATED : 0) | (document.getElementById ('metrics').checked ? METRICS : 0) | (document.getElementById ('semicolon').checked ? SEMICOLON : 0); var x = minimizeCSS (css, options); document.getElementById ('dest').value = x; document.getElementById ('cinfo').innerHTML = '<u>Statistik zur CSS Komprimierung:</u><br/>Gr\u00f6\u00dfenverh\u00e4ltnis (unkomprimiert:komprimiert): ' + css.length + ':' + x.length + ' Bytes<br/>Ratio: ' + Math.floor (x.length / css.length * 1000) / 1000 + ' (' + Math.floor (x.length / css.length * 1000) / 10 + '%)'; return false; }
Quelle: Online Stylesheet Kompression Tool
-
15.08.2012, 13:06 #2
AW: CSS Komprimierer
Leute die sich mit der Webentwicklung auseinander setzen, wissen schon wofür das da ist
/e ausserdem hat er extra den Link dazu gepackt! http://www.homepage-performance.de/s...cker-tool.htmlGeändert von Prinzessin Paat (15.08.2012 um 13:10 Uhr)
Diese Seite nutzt Cookies, um das Nutzererlebnis zu verbessern. Klicken Sie hier, um das Cookie-Tracking zu deaktivieren.