CSS Parsing ColdFusion Function
Here's a function I knocked up to retrieve a specified CSS property from within a specified CSS Selector:
<cfargument name="CSS" type="string" required="true" hint="CSS to Process">
<cfargument name="Selector" type="string" required="true" hint="Selector to locate">
<cfargument name="Property" type="string" required="true" hint="CSS Property to retrieve">
<cfargument name="DefaultValue" type="string" required="false" default="" hint="Default value if not found">
<cfscript>
var local = structnew();
local.MatchingSelectors = REMatch("[^}]*#arguments.Selector#[^{]*{[^}]*#arguments.Property#[\s]*:[\s]*([^;}]*[^}]*})",arguments.css);
if (arraylen(local.MatchingSelectors))
{
local.relevantSelector = trim(local.MatchingSelectors[arraylen(local.MatchingSelectors)]);
local.FindValue = refind("{[^}]*#arguments.Property#[\s]*:[\s]*([^;}]*)",local.relevantSelector, 1, true);
local.pos = local.FindValue.pos[2];
local.len = local.FindValue.len[2];
local.cssValue = trim(mid(local.relevantSelector,local.pos,local.len));
}
else
{
local.cssValue = arguments.DefaultValue;
}
return local.cssValue;
</cfscript>
</cffunction>
This function will attempt to return the specified property as defined in the last CSS definition which contains the specified selector. This does mean that it will potentially return false matches against more specific selectors - e.g. searching for font-size property within td.gridDataCell would return the value for the button (8px), even though more accurately the previous less specific value (10px) should have been returned.
td.gridDataCell button {font-size:8px;}
I have a particular use case for this functionality for parsing CSS used to control formatting of dynamically generated HTML emails, and convert it to HTML which is more compatible with Outlook 2007 (i.e. HTML Properties rather than CSS :-/) where I already know that no complex cascading rules have been defined - but if need be additional functionality could be added to parse the selectors within local.MatchingSelectors, and remove any matches which only match partially.

