Update (08 July 2009): Check file extension is CSS.
CssCompact is a simple WebHandler for sending smaller CSS files to the client. It simply removes linebreaks, tabs, spaces (before CSS properties) and comments. That way you can still have your commented CSS code, and also save bandwidth.
Use is simple, just pass on the stylesheet as a parameter when loading your stylesheet:
Add to <head>:
<link rel="stylesheet" type="text/css" href="/style/CssCompact.ashx?stylesheet=/style/style.css" />
or
<style type="text/css"> @import "/style/CssCompact.ashx?stylesheet=/style/style.css"; </style>
Create CssCompact.ashx:
<%@ WebHandler Language="C#" Class="CssCompact" %>
using System;
using System.Web;
using System.IO;
using System.Text.RegularExpressions;
public class CssCompact : IHttpHandler
{
public void ProcessRequest(HttpContext c)
{
string stylesheet = c.Request.QueryString["stylesheet"];
c.Response.ContentType = "text/css";
if (stylesheet != null)
{
string filename = c.Server.MapPath(stylesheet);
FileInfo f = new FileInfo(filename);
if(f.Extension == ".css" && f.Exists)
{
c.Response.AddHeader("Last-Modified", f.LastWriteTime.ToString("U"));
c.Response.Expires = 0;
if(c.Request.HttpMethod != "HEAD")
{
using (StreamReader cssStream = f.OpenText())
{
// remove linebreaks, whitespace and comments
Regex remove = new Regex(@"^\s+|/\*([^*\\\\]|\*(?!/))+\*/|\r|\n|\t",RegexOptions.Multiline);
string cssContent = cssStream.ReadToEnd();
cssContent = remove.Replace(cssContent, "");
c.Response.Write(cssContent);
c.Response.End();
}
}
}
}
c.Response.Write("/* No valid style sheet selected */");
c.Response.End();
}
public bool IsReusable
{
get
{
return false;
}
}
}



2 comments:
Thanks for a great post. I've improved your code, fixing the security issue, and support multiple stylesheets.
Your code allows the visitor to type in /CssCompact.ashx?stylesheet=/web.config and read the contents of your web.config file.
Great article though I missed a few things in it. I've fixed a few things (compiled the regex, added caching with a file dependency and a querystring to disable it all when debugging). My version can be seen here: http://blog.crazybeavers.se/index.php/archive/compacting-css-on-the-fly/
/ Karl
Post a Comment