As you can see at the bottom of each blog post, I’ve added social bookmark links. Because I use dasBlog version 1.8 it was very difficult to do, mainly because there is no macro to print out the title in clear text of the individual blog posts. Michal showed us how to add the macro to dasBlog in a very easy way. The only problem is, that I’ve uninstalled Visual Studio 2003 and I don’t want to install it again just to add a macro to my blog.

I knew that the only way to do it was by JavaScript and I didn’t like it at all. But in the end, it turned out pretty good. This is the JavaScript that I added to the footer of the ItemTemplate:

<script type="text/javascript">

  var tn = document.createTextNode('<%ItemTitle%>');

  var div = document.createElement('div');

  div.appendChild(tn);

  var html = div.innerHTML;

  var index = html.indexOf('&gt;') +4;

  var title = html.substring(index, html.length - 10);

  var social = '<div style="float:left">';

  social += '<a href="http://digg.com/submit?phase=2&url=<%permalinkUrl%>&title=' + title + '">Digg it</a>&nbsp;|&nbsp;';

  social += '<a href="http://www.dzone.com/links/add.html?url=<%permalinkUrl%>&title=' + title + '">dzone it</a>&nbsp;|&nbsp;';

  social += '<a href="http://www.dotnetkicks.com/submit?url=<%permalinkUrl%>&title=' + title + '">Kick it</a>';

  social += '</div>';

  document.writeln(social);

</script>

You can fairly easy add other bookmarks than the ones I’ve implementet. If you use the new dasBlog 1.9 you don’t have these problems, because it come with the blog post title macro out of the box.

>

After reading a post on The Cafes about the lack of dynamics in the CSS language, I found myself inspired to do something about it. It became clear that something had to be done and that we couldn’t wait for the W3C to come up with a new standard and browser vendors to incorporate them.

There were two things I wanted to enrich the CSS language with:

  • Variables
  • Scripting

To make it work, I created an HttpHandler for ASP.NET that is able to parse these two new features and apply them to the stylesheet. It does not interfere with any CSS standards because everything is handled by the server.

Here is an example of a .css file that uses these new capabilities.

define BACKCOLOR = #00FFFF;

define FORECOLOR = pink;

define TOTALWIDTH = 550;

define RIGHTWIDTH = browser == "IE" & version >= 6 ? "150px" : "170px";

define LEFTWIDTH = TOTALWIDTH / 2 + "px";

 

body{

  background-color: BACKCOLOR;

  color: FORECOLOR;

  width: TOTALWIDTHpx;

}

 

#RightColumn{

  width: RIGHTWIDTH;

}

 

#LeftColumn{

  width: LEFTWIDTH;
}

This will then be outputted as this standard compliant stylesheet:

body{

  background-color: #00FFFF;

  color: pink;

  width: 550;

}

 

#RightColumn{

  width: 150px;

}

 

#LeftColumn{

  width: 275px;

}

All the lines beginning with "define" will be treated as a variable declaration, parsed and then deleted from the parsed stylesheet. Notice the two variable names "browser" and "version". Those two variables are built in properties that return the browser name and major version number respectively.

The scripting syntax is 100% C# because it is compiled by the CodeDom into C# code in-memory. No new syntax of any kind is invented. The reason to use C# and not VB.NET is that developers or designers writing stylesheets probably also know JavaScript which also uses the C style syntax like C#. So, if you are comfortable writing JavaScript, it should be no problem to add scripting to the new CSS variables.

Remember, like JavaScript, these features are case-sensitive. However, that is not why the variable names are upper cased, I just thought it makes sense to let them stick out from the rest of the styles.

Implementation

There are two versions of the HttpHandler – one to use if you can control the IIS and one if you can’t.

If you are able to let the IIS send .css files to the ASP.NET engine you should download the CssHandler.cs file below and add it to the App_Code folder and then add the following to the web.config:

<httpHandlers>

  <add type="CssHandler" verb="*" path="*.css" />
</httpHandlers>

If you can’t access the IIS, you need to download the CssHandler.ashx below and then call that file while passing the .css file as a query string in the URL like this:

<link rel="stylesheet" type="text/css" href="csshandler.ashx?path=style.css" />

Total feature list

This is the total feature list of the handler:

  • Add variables to CSS (feature)
  • Use scripting capabilities (feature)
  • Server and client side caching (performance)
  • Cache dependant of the source .css file (performance)
  • Whitespace and comment removal (performance)
  • Works only on .css files (security)
  • Blocks the System namespace (security)

Word of caution

Because the scripting language is pure C#, it also means that you have to take injection attacks into account. I’ve already made sure that any scripting using anything in the System namespace will be rejected. That means that you can’t use any System.File.IO commands or anything else that has the word “System.” in it. That eliminate probably 99% of the obvious attack surface, but it is not totally secure.

Download

CssHandler (.cs - for IIS access).zip (1,96 KB)
CssHandler (.ashx - for non IIS access).zip (1,91 KB)