CFLib.org – Common Function Library Project

cssToStruct(css_data)

Last updated January 9, 2009

Version: 0 | Requires: ColdFusion MX | Library: StrLib

 
Rated 25 time(s). Average Rating: 1.2

Description:
Pass css rules to the UDF and it will return a data-structure.

Return Values:
Returns a struct.

Example:

view plain print about
<cfsavecontent variable="strCSSData">
 
    /* This is my site header */
    #header {
        background-color: gold ;
        }
 
    /*
        These are the general style for my site. These
        will be applied everywhere and should have a
        standard look and feel.
    */

    ol,
    ul,
    p,
    form,
    h1, h2, h3, h4, h5 {
        font-family: verdana ;
        font-size: 11px ;
        margin-bottom: 14px ;
        margin-top: 0px ;
        }
 
</cfsavecontent>

<cfdump var="#cssToStruct(strCSSData)#">

Parameters:

Name Description Required
css_data CSS string. Yes

Full UDF Source:

view plain print about
<!---
 Convert CSS Rules to a ColdFusion struct.
 v2 fixes by Raymond Camden (wasn't trimming right)
 
 @param css_data      CSS string. (Required)
 @return Returns a struct. 
 @author Brandon Hansen (brandon@melissa-brandon.com) 
 @version 0, January 9, 2009 
--->

<cffunction name="cssToStruct" access="public" returntype="any" output="false">
    <cfargument name="css_data" type="string" required="yes">
    
    <!---Inspiration (and some code [all the regex]) from Ben Nadel http://www.bennadel.com/index.cfm?dax=blog:584.view--->
    
    <!---Create the local scope--->
    <cfset var local = {}>
    
    <!---This struct will hold all of the rules--->
    <cfset LOCAL.cssRules = {}>
     
    <!---
        Remove all line breaks. We are going to be doing some
        regular expressions and stripping out line breaks will
        make things slightly less complicated.
    --->

    <cfset LOCAL.strCSSData = reReplace(arguments.css_data,"[\r\n]+"" ","all") />
     
    <!---
        Strip out the CSS comments. These hold no value for us
        when we are getting the classes.
    --->

    <cfset LOCAL.strCSSData = reReplace(LOCAL.strCSSData,"/\*.*?\*/"" ""all" ) />
    
    <!--- Create an array to hold all of the class names. --->
    <cfset LOCAL.arrClasses = ArrayNew( 1 ) />
     
     
    <!---
        Loop over the rules. Remember, each rule is now sepparated
        by a pipe (when we stripped out the {..} stuff), so we
        can loop over the rules as a pipe-delimited list.
    --->

    <cfloop
        index="LOCAL.strRule"
        list="#LOCAL.strCSSData#"
        delimiters="}">

     
        <!---
            Check to see if we still have a length (name(s)) of
            CSS classes.
        --->

        <cfif Len( trim(LOCAL.strRule) )>
            <!---Add the item to the array--->
            <cfset arrayAppend(LOCAL.arrClasses,LOCAL.strRule) />
        </cfif>
     
    </cfloop>
    
    <cfloop array="#LOCAL.arrClasses#" index="LOCAL.each_class">
        <cfset LOCAL.cssRules[trim(listFirst(LOCAL.each_class,"{"))] = {}>
        <cfloop list="#trim(listLast(LOCAL.each_class,"{"))#" delimiters=";" index="LOCAL.each_rule">
            <cfset LOCAL.cssRules[trim(listFirst(LOCAL.each_class,"{"))][trim(listFirst(LOCAL.each_rule,":"))] = trim(listLast(LOCAL.each_rule,":"))>
        </cfloop>
    </cfloop>
    
    <cfreturn LOCAL.cssRules>
</cffunction>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson